SpringMVC给404返回一个视图

时间:2012-07-10 20:16:36

标签: java spring spring-mvc

我有以下控制器......

@Controller
@RequestMapping(value = "/userManagement")
public class UserManagementController {

@RequestMapping(value = "/", method = RequestMethod.GET)
    public Object home(Locale locale, Model model) {

        logger.info("User management view controller loaded...");

        return "userManagement";
    }

@RequestMapping(value = "/createUserView", method = RequestMethod.GET)
    public Object createUser(Locale locale, Model model) {
        logger.info("create controller loaded...");
        return "createUser";
    }

我的servlet-context设置为以下值...

<beans:bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <beans:property name="prefix" value="/WEB-INF/views/" />
    <beans:property name="suffix" value=".jsp" />
</beans:bean>

现在,如果我转到http://localhost:8080/myapp/userManagement,那么我会得到视图userManagement.jsp,这正是我想要的......

但如果我去http://localhost:8080/myapp/userManagement/createUserView我会收到404错误。

NetworkError: 404 Not Found - http://localhost:8080/myapp/userManagement/createUserView"

我没看到的是为什么会发生这种情况,因为我将requestMapping设置为与上面完全相同,并且在/ WEB-INF / views中我有一个createUser.jsp和userManagement.jsp

关于从spring mvc提供意见,我有什么问题吗?

谢谢,

编辑:下面添加了web.xml ...

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/root-context.xml 
    /WEB-INF/spring/security-app-context.xml
    /WEB-INF/spring/appServlet/servlet-context.xml</param-value>
  </context-param>
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
  <filter-name>springSecurityFilterChain</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

EDIT2:

另外,如果我直接转到浏览器中的地址而不是使用Ajax(转到myapp / userManagement / createUserView我会收到错误...

HTTP状态404 - /myapp/WEB-INF/views/userManagement/createUserView.jsp因此它看起来看得太高(虽然文件名也错误)。

EDIT3。

好吧,即使我做以下事情,它似乎也是如此..

    @Controller
@RequestMapping(value = "/userManagement")
public class UserManagementController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
        public Object home(Locale locale, Model model) {

            logger.info("User management view controller loaded...");

            return "createUser";
        }

我仍然看到userManagement.jsp页面,因此看起来好像这个返回没有正确触发,但我不知道为什么。记录器的详细信息仍然会被触发到控制台,所以实际上它已到达那里,就像springmvc返回JSP一样奇怪。

2 个答案:

答案 0 :(得分:1)

你能检查两件事: 1)向控制器添加断点或查看服务器日志,检查请求是否输入方法。 2)如果第1步没问题,检查jsp是否存在于正确的路径或jsp的名称是否正确

修改

等等,如果您使用Ajax请求,您的控制器应该有以下requestMapping:

  @RequestMapping(value = "/url", method = RequestMethod.GET, 
  headers = "X-Requested-With=XMLHttpRequest")

否则您的ajax请求将无法映射,您将获得404。

旁注 你可以将方法返回类型从Object更改为String吗?看看这是否有影响?

答案 1 :(得分:0)

我认为您的web.xml映射不正确,应该是:

  <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

(注意url-pattern中的/*