如何将此URL映射到Spring控制器方法?

时间:2012-11-05 21:27:57

标签: spring controller httpwebrequest mapping

我正在使用Spring 3.1.1.RELEASE。我正在将URL映射到控制器方法。我想将URL“/ my-context-path / organizations / add”映射到下面的控制器方法。在我的控制器中,我有

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

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

在我的web.xml中我有

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

在我的dispathcer-servlet.xml中我有

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

但是对我所需上下文路径的请求导致“在DispatcherServlet中找不到具有URI [/myproject-1.0-SNAPSHOT/organizations/add]的HTTP请求的映射,其名称为”dispatcher“错误(使用JBoss 7)。如何正确映射这个东西?请注意,我的控制器中有多个方法,我想要“/ organizations”空间中的不同URL。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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>基本上定义了您的网站可以访问的“虚拟路径”。 您在控制器上执行的Al映射将附加到它,使其/organizations /whateverpagecomeshere.jsp

确保View文件存在!