本文介绍了如何在 Spring MVC 控制器中映射动态 url/prj/noticeOpen/2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用以下网址时遇到困难:

Hi I am having hard time with following url:

<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 和 @PathParam 中删除 {quesId},这一切正常(也从锚中删除 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} 创建了新的控制器,现在它获得了控制权,但我无法理解以下方法的行为.请在下面的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 03:18