SpringMVC giving 404 returning a view

2019-07-27 11:30发布

问题:

I've got the following controller...

@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";
    }

My servlet-context is setup with the following values...

<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>

Now if I go to http://localhost:8080/myapp/userManagement then I get the view userManagement.jsp which is exactly what I want...

But if I go to http://localhost:8080/myapp/userManagement/createUserView I get a 404 error.

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

What I don't see is why this would occur as I've set the requestMapping up exactly the same as above and in /WEB-INF/views I've got a createUser.jsp and userManagement.jsp

Is there anything I'm doing wrong with regards to serving up the views from spring mvc?

Thanks,

EDIT : web.xml added below...

<?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:

Also, if I go directly to the address in the browser rather than using Ajax (going to myapp/userManagement/createUserView I get the error...

HTTP Status 404 - /myapp/WEB-INF/views/userManagement/createUserView.jsp so it appears to be looking at a directory too high (although the wrong filename too).

EDIT3.

Ok, it appears to be even when I do the following..

    @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";
        }

I'm still presented with the userManagement.jsp page, so it appears as if this return is not firing correctly, but I have no idea why. The logger detail does still get fired to the console so it is actually reaching there, just something odd with the way springmvc is returning the JSP.

回答1:

Can you check for 2 things: 1) Add breakpoint to controller or look at server log, check if request enters the method. 2) If 1st step is fine, check if jsp is present at correct path or the name of jsp is correct

EDIT

Wait, if you are using Ajax for request, your controller should have following requestMapping:

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

Else your ajax request will not get mapped and you will get 404.

Side Note Can you change method return type from Object to String? See if that impacts anything?



回答2:

I think your web.xml mapping is incorrect, it should be:

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

(Note /* in the url-pattern)