2014-11-15 2 views
1

Sping MVC를 사용하여 JSP로 웹 사이트를 학습/개발하고 있습니다. 문제는 지금 내가 직면하고있는 것은 "localhost/site/about ///"URL을 방문하려고하면 "localhost/site"로 이동하면 홈 페이지가 표시되고 홈 페이지는 "localhost/about "은 페이지에 대해 보여줍니다. 그래서 누군가가 하나의 슬래시로 페이지에 액세스하려고하면 오류 페이지를 던지는 방법을 모르겠습니다. 내가 "localhost/site/about /"또는 "localhost/site/about ///"에 액세스하려고하면 404 오류 페이지를 내 응용 프로그램에 전달하려고합니다. 여기 스프링 mvc가 두 개 이상의 슬래시 뒤에 홈 페이지를 반환합니다.

내 web.xml의 코드

내 디스패처-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> 

있습니다

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

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; 

    } 


} 

그래서 사람은 말할 수 어떻게 하나 또는 여러 개의 슬래시 뒤에 기본 홈 페이지 표시를 중단 할 수 있습니까? 도와 줘서 고마워. 다음과 같이

답변

0

당신은 오류 페이지를 표시 할 수있는 대체 방법을 가질 수 있습니다

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

편집 : 그렇지

여러 앞으로 마지막에 슬래시 처리하기 위해 정규 표현식을 사용할 수 있습니다

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

해당 작동하지 않습니다. – JavaLearner

0

코드에서 예외가 발생하는 경우 예외 예외를 사용하여 특정보기를 렌더링 할 수 있습니다.

관련 문제