我在以下网址上遇到了麻烦:
<a href="/noticeOpen/2">dynamicLink</a>
使用以下控制器方法进行映射:
@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时,问题开始了,它没有将控制权转移到我的控制器,而是在浏览器的地址栏中显示了以下内容:
http://127.0.0.1:8080/prj/noticeOpen/2/WEB-INF/pages/noticeOpen.jsp
而且我在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(也从锚中删除问题ID),这一切都很好
http://127.0.0.1:8080/prj/noticeOpen
但这听起来并不动态,无法满足我的要求。
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上查看以下内容,然后得出以下结果:
/noticeOpen/{quesId}
@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")
noticeOpen.jsp @RequestMapping(value="/noticeOpen/{quesId}")
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中的前缀值
<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之前的斜线将起作用。