Spring mvc在两个或多个正斜杠之后返回主页

时间:2014-11-15 13:15:50

标签: java spring jsp spring-mvc

我正在使用Sping MVC在JSP中学习/开发网站。我现在面临的问题是,当我尝试访问url“localhost / site / about ///”时,它会显示默认主页,如果我转到“localhost / site”,它会显示主页,如果我转到“localhost” /关于“它显示关于页面。因此,如果有人试图使用任何单个正斜杠访问页面,我不知道如何抛出错误页面。我的意思是,如果我尝试访问“localhost / site / about /”或“localhost / site / about ///”,我希望我的应用程序抛出404错误页面。

这是我的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>Test</display-name>

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

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

</web-app>

我的dispatcher-servlet.xml文件的代码是

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
                 http://www.springframework.org/schema/beans
                 http://www.springframework.org/schema/beans/spring-beans.xsd
                 http://www.springframework.org/schema/context
                 http://www.springframework.org/schema/context/spring-context.xsd
                 http://www.springframework.org/schema/mvc
                 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com.test.router" />
    <mvc:annotation-driven />

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

</beans>

我的路由器文件的代码是

package com.test.router;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;



@Controller
public class Router {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView homePage(){

        ModelAndView model = new ModelAndView("HomePage");

        model.addObject("title", "Welcome to home page");
        model.addObject("msg", "This is the home page.");

        return model;

    }

    @RequestMapping(value = "/about", method = RequestMethod.GET)
    public ModelAndView aboutPage(){

        ModelAndView model = new ModelAndView("AboutPage");

        model.addObject("title", "About Test website.");

        return model;

    }

    @RequestMapping(value = "/accounts/login", method = RequestMethod.GET)
    public ModelAndView loginPage(){

        ModelAndView model = new ModelAndView("accounts");

        model.addObject("title", "Login here to access Test Website.");
        model.addObject("pageTitle", "Login page.");

        return model;

    }

    @RequestMapping(value = "/accounts/signup", method = RequestMethod.GET)
    public ModelAndView signupPage(){

        ModelAndView model = new ModelAndView("accounts");

        model.addObject("title", "Signup here to join and explore Test Website.");
        model.addObject("pageTitle", "Signup page.");

        return model;

    }


    @RequestMapping(value = "/accounts/logout", method = RequestMethod.GET)
    public ModelAndView logoutPage(){

        ModelAndView model = new ModelAndView("accounts");

        model.addObject("title", "Logging you out securely.");
        model.addObject("pageTitle", "Logout page.");

        return model;

    }


}

所以有人能告诉我如何在单个或多个正斜杠后停止显示默认主页?谢谢你的帮助。

2 个答案:

答案 0 :(得分:0)

您可以使用后备方法显示错误页面,如下所示:

@RequestMapping(value = "*")
@ResponseBody
public String errorPage() {
    return "Error page";
}

编辑:

否则,您可以使用正则表达式在最后处理多个正斜杠:

@RequestMapping("{multipleForwardSlashes:[/]{2,}$}")
public String errorPage(@PathVariable String multipleForwardSlashes) {
    return "Error page";
}

答案 1 :(得分:0)

如果您在代码中遇到异常,那么对于特定的异常,您可以使用异常解析器来重新呈现特定的视图。