简体   繁体   中英

How can I map my Spring URL to a JSP file in /WEB-INF/views?

I'm having trouble doing a Spring (using 3.0.5.RELEASE) mapping. I want to map the URL http://mydomain/context-path/user/registrationform.jsp to my JSP page at

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

but I'm getting a 404. I have my controller setup like so …

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

here is my dispatcher-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>

and here is my 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> 

What else do I need to configure (or configure differently) to make this mapping work? This is harder than - Dave

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

The RequestMapping annotation at class level should be use for a common url pattern like "item/*" and all the links that contains "/item" followed by other pattern would be mapped it to the controller. "user/" in your case The RequestMapping annotation at method level is used for mapping the sub URL like "item/add" or "item/delete", "registrationform.jsp' in your case So try this:

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

This will map /user/registrationform.jsp

RequestMapping更改为:

@RequestMapping("/user/registrationform.jsp") 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM