RequestMapping如何在春天工作

时间:2015-11-27 11:06:08

标签: java spring spring-mvc

弹簧servlet.xml中

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

控制器类

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

            ModelAndView model = new ModelAndView("AdmissionForm");

            return model;
        }
        @RequestMapping(value="/admissionForm.jsp", method = RequestMethod.GET)
        public ModelAndView getAdmissionForm2() {

            ModelAndView model = new ModelAndView("AdmissionForm");

            return model;
        }
        @RequestMapping(value="/admissionForm", method = RequestMethod.GET)
        public ModelAndView getAdmissionForm3() {

            ModelAndView model = new ModelAndView("AdmissionForm");

            return model;
        }

的web.xml

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

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

每当我通过/admissionForm.html和/ admissionForm访问网址时 我正在网页上收到回复,但是当我通过/admissionForm.jsp访问时,我找不到404页面,我的问题是这是什么原因以及我可以做些什么来使这项工作?

2 个答案:

答案 0 :(得分:2)

更改web.xml文件

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

答案 1 :(得分:1)

首先,我不确定它是否是例如你的真实代码,但是你可以写的是同样的方法

@RequestMapping(value={"/admissionForm","/admissionForm.htm","/admissionForm.html"}, method = RequestMethod.GET)

对于您的问题,您可以尝试将您的web.xml更改为

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

因此它会识别jsp扩展名