问题描述
我遇到了以下网址:
<a href="/noticeOpen/2">dynamicLink</a>
使用以下控制器方法进行映射:
to map with following controller method:
@RequestMapping(value="/noticeOpen/{quesId}")
public ModelAndView noticeOpen(@ModelAttribute("NoticeForm") NoticeForm noticeForm,
ModelMap model,
@PathVariable("quesId") Integer quesId){
System.out.println(quesId);
return new ModelAndView("/noticeOpen","noticeForm",noticeForm);
}
当我单击锚点 dynamicLink 时问题开始,并且没有将控制权转移到我的控制器,而是在浏览器的地址栏中显示了以下内容:
Problem starts when i click on the anchor dynamicLink and it does not transfers control to my controller, instead it shows following in browser's address bar:
http://127.0.0.1:8080/prj/noticeOpen/2/WEB-INF/pages/noticeOpen.jsp
而且我在applicationContext.xml中有以下映射
Moreover I have following mapping in applicationContext.xml
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
如果我从控制器的@RequestMapping中删除{quesId},并从方法签名中删除@PathParam(也从锚点中删除QuestionId),那么一切都很好
This all works fine if I remove {quesId} from controller's @RequestMapping and @PathParam from method signature(also remove questionId from anchor )
http://127.0.0.1:8080/prj/noticeOpen
但这听起来并不符合我的要求.
But that does not sound dynamic and fulfill my requirement.
web.xml
<?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" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>Spring Web MVC Application</display-name>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
更新
我为/noticeOpen/{quesId}
创建了新的Controller,现在它获得了控件,但是我无法理解以下方法的行为.请在NoticeController上查看以下内容,然后得出以下结果:
I created new Controller for /noticeOpen/{quesId}
and its now getting the control, But I am not able to understand the behavior of the following methods. Please take a look below on NoticeController and then result under that:
@Controller
public class NoticeController {
@RequestMapping(value="/noticeOpen/{quesId}")
public ModelAndView noticeOpen(@ModelAttribute("NoticeForm") NoticeForm noticeForm,ModelMap model,@PathVariable("quesId") Integer quesId){
return new ModelAndView("noticeOpen","noticeForm",noticeForm);
}
@RequestMapping(value="/noticeOpen")
public ModelAndView noticeOpen(@ModelAttribute("NoticeForm") NoticeForm noticeForm,ModelMap model){
return new ModelAndView("noticeOpen","noticeForm",noticeForm);
}
@RequestMapping(value="/noticeOpen")
它将我重定向到正确的noticeOpen.jsp@RequestMapping(value="/noticeOpen/{quesId}")
它将我重定向到以下错误页面
@RequestMapping(value="/noticeOpen")
it redirects me to correct noticeOpen.jsp@RequestMapping(value="/noticeOpen/{quesId}")
it redirects me to following error Page
HTTP Status 404 - /prj/noticeOpen/WEB-INF/pages/noticeOpen.jsp
type Status report
message /prj/noticeOpen/WEB-INF/pages/noticeOpen.jsp
description The requested resource is not available.
Apache Tomcat/6.0.36
推荐答案
按如下所示更改applicationContext.xml中的前缀值
Change your prefix value in applicationContext.xml as follows
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
在WEB-INF之前加一个斜杠.它将起作用.
A slash before WEB-INF.It will work.
这篇关于如何在Spring MVC控制器中映射动态URL/prj/noticeOpen/2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!