如何将Spring URL映射到/WEB-INF/views中的JSP文件? [英] How can I map my Spring URL to a JSP file in /WEB-INF/views?

查看:90
本文介绍了如何将Spring URL映射到/WEB-INF/views中的JSP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做Spring(使用3.0.5.RELEASE)映射时遇到麻烦.我想映射URL http://mydomain/context-path/user/registrationform.jsp 到我的JSP页面

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

但是我得到了404.我的控制器设置是这样的……

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

这是我的dispatcher-servlet.xml…

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 >

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…

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> 

要使此映射正常工作,还需要配置什么(或进行其他配置)?这比-Dave

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 {

类级别的RequestMapping批注应用于常见的url模式,例如"item/*",并且所有包含"/item"后跟其他模式的链接都将其映射到控制器.您的情况下的用户/" 方法级别的RequestMapping批注用于映射子URL,例如"item/add"或"item/delete","registrationform.jsp" 所以试试这个:

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

这将映射/user/registrationform.jsp

This will map /user/registrationform.jsp

这篇关于如何将Spring URL映射到/WEB-INF/views中的JSP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆