Spring MVC Controller被调用但获得了404渲染视图

时间:2014-09-21 19:36:51

标签: java spring jsp spring-mvc tomcat

如果在其他地方得到解答,我很抱歉,但是网站的一些广泛的谷歌搜索到目前为止没有任何帮助我,所以我会在这里问。

所以,我有一个Spring MVC应用程序,我试图使用注释进行映射,虽然对于某些请求它可以正常工作,但对于其他人我甚至在控制器之后得到了404&#s;已被援引。

所以,这是我的设置......

web.xml:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/classes/log4j.properties</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>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>INCLUDE</dispatcher>
</filter-mapping>

<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
    <filter-name>requiredVariableFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    <init-param>
        <param-name>targetBeanName</param-name>
        <param-value>requiredVariableFilter</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>requiredVariableFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

<servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

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

我的appServlet-servlet.xml:

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

我的控制器。它现在非常简单:

@Controller
@RequestMapping("/user")
public class UserController {

    static Logger log = Logger.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView getBaseUserInfo(HttpServletRequest request, HttpServletResponse response) {
        log.info("got into the user controller");
        ModelAndView mav = new ModelAndView("user/baseinfo");

        User user = this.userService.getCurrentUser();

        mav.addObject("user", user);
        log.info("passing off to view");
        return mav;
    }
}

我可以向你保证,我在/WEB-INF/pages/user/baseinfo.jsp上有相关的jsp,所以并不是说JSP不在那里。

此外,我的日志记录显示控制器中的日志消息被触发,因此它返回到视图,然后Tomcat给了我非常漂亮的404页面,而我的日志只有说/用户正在返回404。

或者,我有另一个控制器在这里工作得非常好:

@Controller
public class BaseController {

    static Logger log = Logger.getLogger(BaseController.class);

    @Autowired
    private UserService userService;

    @Autowired
    private MailChimpService mailChimpService;

    @RequestMapping(value="/", method=RequestMethod.GET)
    public ModelAndView getHomePage(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView mav = new ModelAndView("index");

        User user = this.userService.getCurrentUser();
        mav.addObject("welcomeMessage", "Hey There!");

        if(user != null) {
            mav.addObject("user", user);
        }

        mav.addObject("newsBlasts", this.mailChimpService.getEmailBlasts());
        return mav;
    }

    @RequestMapping(value="/login")
    public ModelAndView getLoginPage (HttpServletRequest request, 
            HttpServletResponse response,
            @RequestParam(value="error", required=false, defaultValue=StringUtils.EMPTY) String error) {
        ModelAndView mav = new ModelAndView("login");

        SavedRequest savedRequest = new HttpSessionRequestCache().getRequest(request, response);

        if(StringUtils.isNotEmpty(error)) {
            mav.addObject("badLogin", true);
        }
        mav.addObject("returnUrl", savedRequest.getRedirectUrl());
        return mav;
    }
}

这个控制器中的两个方法都返回完全正常并且是sitemeshed并且没有任何问题。我已经尝试了很多我在本网站上看到的不同建议,到目前为止似乎没有什么可以做到的。

我对待这些控制器的唯一区别是,第一个控制器最终将嵌套@RequestMapping,因此它将处理/ user模式下的任何内容(/ user / edit,/ user) / throwparty等)而第二个控制器只是处理一个特定的功能集(在这种情况下,主页和登录页面)的一次性URL。 / p>

有人有任何建议吗?

1 个答案:

答案 0 :(得分:1)

错误出现在视图中。不得不添加一个错误页面来查看异常,但是一旦我这样做了,就能解决这个问题。