没有为GET请求调用@Controller

时间:2012-12-17 08:37:16

标签: spring spring-mvc

我正在尝试在我的Web应用程序中使用Spring 3 MVC支持带注释的控制器。 我的配置如下:

1- web.xml:

 <servlet>  
    <servlet-name>dispatcher</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
</servlet>

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

2- applicationContext.xml:我的网页直接在webapp文件夹下

    <context:component-scan base-package="com.myapp" />
    <mvc:annotation-driven />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

3-控制器:

@Controller
@RequestMapping("/test.jsp")
public class Test{

    @RequestMapping(method = RequestMethod.GET)
    public String get() {
        System.out.println("######## GET METHOD FOR test.jsp ########");
        return "test.jsp";
    }

}

注意:我在 ServletContextListener 中加载 applicationContext ,如下所示:

ApplicationContext context = new ClassPathXmlApplicationContext(
                    "classpath:spring/config/applicationContext.xml");

请告知如何解决这个问题,谢谢。

我还有另一个问题,如果有可能使调度程序servlet调度特定的jsp页面而不是应用程序中的所有页面,因为并非所有的jsp页面都有控制器。

3 个答案:

答案 0 :(得分:3)

web.xml中的调度程序servlet名称为dispatcher。在这种情况下,Spring尝试加载dispatcher-servlet.xml(servlet_name-servlet.xml)。而您已在applicationContext.xml中定义了弹簧配置。将其重命名为dispatcher-servlet.xml

或者你也可以通过在servlet中设置init-param来让Spring读取applicationContext。例如:

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/springmvc/*</url-pattern>
</servlet-mapping>

关于第二个问题,您可以使用spring的view-controller映射直接呈现视图。

<mvc:view-controller path="demo/flot" view-name="demo/flot"/>

阅读:17.15.5 Configuring View Controllers

答案 1 :(得分:1)

return "test.jsp"更改为return test。您的返回字符串将添加prefixsuffix

答案 2 :(得分:0)

解决如下:

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

    <context-param>
        <param-name>contextConfigLocation</param-name>
            <param-value>
             classpath:spring/config/applicationContext.xml
        </param-value>
    </context-param>


    <servlet>  
        <servlet-name>spring</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/config/dispatcherServlet.xml</param-value>
       </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>  
        <servlet-name>spring</servlet-name>  
        <url-pattern>/myapp/*</url-pattern>  
    </servlet-mapping>
相关问题