2

I'm using Spring 3.1.1.RELEASE. I'm having fits mapping URLs to controller methods. I would like to map the URL "/my-context-path/organizations/add" to the controller method below. In my controller, I have

@Controller
@RequestMapping("/organizations")
public class OrganizationController
{

    …
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView doGetadd()
    {
        … do some stuff …
        return new ModelAndView("organizations/add");
    }   // doGetadd 

In my web.xml I have

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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>SB Admin</display-name>

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

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

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
    </context-param>

and in my dispathcer-servlet.xml I have

...
<!-- Enable annotation driven controllers, validation etc... -->
<mvc:annotation-driven />

<context:component-scan base-package="org.myco.subco" />

    <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>

but requests for my desired context-path result in "No mapping found for HTTP request with URI [/myproject-1.0-SNAPSHOT/organizations/add] in DispatcherServlet with name 'dispatcher" errors (using JBoss 7). How do I map this thing properly? Note that I have multiple methods in my controller that I want to different URLs within the "/organizations" space.

4

2 回答 2

3

尝试这个。

将调度程序 servlet 映射更改为:

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

对于 OrganizationController,映射将是

    @Controller
    @RequestMapping("/organizations")
    public class OrganizationController

而对于 ContractsController 的映射将是

    @Controller
    @RequestMapping("/contracts")
    public class OrganizationController
于 2012-11-06T00:40:47.547 回答
1

根据Spring Doc,ModelAndView 构造函数参数是视图文件的名称。

所以那个文件可能是addView.jsp.

以及您(就我的 Spring 知识而言)实际上将其映射到 /Application-Name/organizations/organizations/add 的事实,因为:

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

@Controller
@RequestMapping("/organizations")
public class OrganizationController

我建议将请求映射从控制器更改为

@Controller
@RequestMapping("/")
public class OrganizationController

<url-pattern>/organizations/</url-pattern>基本上定义了可以访问您的站点的“虚拟路径” 。您在控制器上执行的所有映射都将附加到它,使其成为 /organizations/whateverpagecomeshere.jsp

并确保查看文件存在!

于 2012-11-05T21:35:25.157 回答