【问题标题】:spring controller not found找不到弹簧控制器
【发布时间】:2016-12-23 01:17:34
【问题描述】:

我无法解决这个问题,我是 spring 新手,所以它可能很愚蠢,但我的 angular post 请求没有与 java spring 控制器通信...

我的角码..

function check($scope,$http){
 $scope.go = function (){
      var go = $http({
          method:'POST',
      url:'/addentry/pp',
  data : $scope.abc
  })  ;
};

我的添加入口类...

package all;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
* @author om
*/
@Controller
@RequestMapping("/addentry")
public class addentry {
@RequestMapping(value = "/addentry/pp" ,method = RequestMethod.POST)
public @ResponseBody String reply(@RequestParam("name") String        name,@RequestParam("pass") String pass){
    return "hello";
}     
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <context-param>
       <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
 <listener>
    <listener- class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   <servlet>
      <servlet-name>dispatcher</servlet-name>
       <servlet-    class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
   </servlet-mapping>
   <session-config>
      <session-timeout>
         30
    </session-timeout>
    </session-config>
    <welcome-file-list>
    <welcome-file>home/index.html</welcome-file>
    </welcome-file-list>
    </web-app>

servlet-dispatcher.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

  <!--
  Most controllers will use the ControllerClassNameHandlerMapping above, but
  for the index controller we are using ParameterizableViewController, so we must
  define an explicit mapping for it.
  -->
  <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="mappings">
        <props>
            <prop key="index.htm">indexController</prop>
        </props>
    </property>
</bean>

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />

<!--
The index controller.
-->
<bean name="indexController"
      class="org.springframework.web.servlet.mvc.ParameterizableViewController"
      p:viewName="index" />

但我收到 404 错误....(找不到附加条目/pp)

【问题讨论】:

  • 我也是stackoverflow的新手,如果我问错了请指导我!!!
  • 你能把你的代码上传到github吗?还提供更多详细信息:stacktrace 等。
  • 很多事情都错了。您是否同时学习多个教程?请选择一个并完全按照它所说的去做。您在这里的任何配置都没有任何意义。
  • 是的,在这里....github.com/mouse9/o.git
  • @SotiriosDelimanolis 嗯,是的,实际上我没有得到任何完美的教程,所以我混合了两个教程..

标签: java angularjs spring-mvc


【解决方案1】:

您的@RequestMapping 注释有误。你已经用@RequestMapping 注释了你的控制器和方法(这很好),但是/addentry 被定义了两次。 Spring会回复/addentry/addentry/pp

从您的控制器(类定义)或您的方法中删除 /addentry。

【讨论】:

  • 我没有看到你的控制器在 spring 文件中的声明或扫描位置。
猜你喜欢
  • 2014-11-30
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
  • 2014-08-27
  • 1970-01-01
相关资源
最近更新 更多