2012-03-07 14 views
0

我在做一個Spring(使用3.0.5.RELEASE)映射時遇到了麻煩。我想要的網址http://mydomain/context-path/user/registrationform.jsp如何將我的Spring URL映射到/ WEB-INF/views中的JSP文件?

/WEB-INF/views/user/registrationform.jsp 

映射到我的JSP頁面,但我得到一個404。我有我的控制器設置是這樣的...

@Controller 
@RequestMapping("registrationform.jsp") 
public class RegistrationController { 

    private static Logger LOG = Logger.getLogger(RegistrationController.class); 
    … 
    public void setRegistrationValidation(
     RegistrationValidation registrationValidation) { 
     this.registrationValidation = registrationValidation; 
    } 

    // Display the form on the get request 
    @RequestMapping(method = RequestMethod.GET) 
    public String showRegistration(Map model) { 
     final Registration registration = new Registration(); 
     model.put("registration", registration); 
     return "user/registrationform"; 
    } 

這裏是我的調度員servlet.xml中...

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

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd「>

<!-- Enable annotation driven controllers, validation etc... --> 
<mvc:annotation-driven /> 

<context:component-scan base-package="com.burrobuie.eventmaven" /> 

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

<bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="/messages" /> 
</bean> 

</beans> 

這裏是我的web.xml ...

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

還有什麼我需要配置(或配置不同)來使這個映射工作?這是難度比 - 戴夫

回答

0

更改RequestMapping到:

@RequestMapping("/user/registrationform.jsp") 
1
@Controller 
@RequestMapping("registrationform.jsp") 
public class RegistrationController { 

在一流水平的RequestMapping註釋應該是使用了一個共同的URL模式,如「項目/ *」和所有的鏈接,包含「/ item」後跟其他模式將被映射到控制器。 「user /」 方法級別的RequestMapping註釋用於映射子項URL,如「item/add」或「item/delete」,「registrationform.jsp」 因此請試試這個:

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

    private static Logger LOG = Logger.getLogger(RegistrationController.class); 
    … 
    public void setRegistrationValidation(
     RegistrationValidation registrationValidation) { 
     this.registrationValidation = registrationValidation; 
    } 

    // Display the form on the get request 
    @RequestMapping(value="/registrationform.jsp",method = RequestMethod.GET) 
    public String showRegistration(Map model) { 
     final Registration registration = new Registration(); 
     model.put("registration", registration); 
     return "user/registrationform"; 
    } 

這將映射/user/registrationform.jsp

+0

我改變了我的代碼,你的建議,但我仍然得到404我通過調試,我的「showRegistration」是從來沒有所謂的證實。是視圖 - 「dispatcher-servlet.xml」中的解析器和web.xml聲明是否正確? – Dave 2012-03-07 20:59:53

+0

沒關係!檢查你的調度器-servlet中是否有這個: 並在類級別嘗試使用@RequestMapping(「/ user/*」)而不是@ @RequestMapping(「/ user」) – ChuyAMS 2012-03-08 00:16:25

+0

我在我的問題中包含了我的完整dispatcher-servlet.xml,其中包括您提到的「mvc:annotation-drive」標籤。另外,我將類級別映射更改爲「/ user/*」,但即使進行了這些更改,我仍然得到了404。是否有更小的測試可以運行,以查看映射是否可以運行? – Dave 2012-03-08 17:35:55