开发者

The spring web mvc framework handle the jsp file dispatch as another request

开发者 https://www.devze.com 2023-04-13 05:25 出处:网络
I am new to spring web mvc framework,and I use struts 2 before. I create a new dynamic web project using eclipse EE,and add all the jars to the开发者_如何学JAVA /web-info/lib.

I am new to spring web mvc framework,and I use struts 2 before.

I create a new dynamic web project using eclipse EE,and add all the jars to the开发者_如何学JAVA /web-info/lib.

The whole hierarchy of the project is like this:

SpringMVCTest
    WEB-INF
        web.xml
        example-servlet.xml
        jsp
            hello.jsp
        lib
            xxxx.jars
            .....

This is the servlet definition:

<servlet>
    <servlet-name>example</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

This is the example-servlet.xml:

<context:component-scan base-package="com.kk.web.controllers" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

And the controller:

package com.kk.web.controllers;
@Controller("example")
@RequestMapping("/example")
public class ExampleController {
    @RequestMapping("/hello")
    @ResponseBody
    public String hello() {
        return "hello";
    }

    @RequestMapping("/hello_jsp")
    public ModelAndView hello_jsp(){
        ModelAndView mv=new ModelAndView("hello");
        mv.addObject("message", "welcome");
        return mv;
    }
}

It worked when I run:

http://localhost:8080/SpringMVCTest/example/hello

But when I run: http://localhost:8080/SpringMVCTest/example/hello_jsp I got the warn:

2011-10-17 10:36:15 org.springframework.web.servlet.DispatcherServlet noHandlerFound
Warn: No mapping found for HTTP request with URI [/SpringMVCTest/WEB-INF/jsp/hello.jsp] in DispatcherServlet with name 'example'

It seems that the ExampleController works,it dispatch the request "/example/hello_jsp" to the right view "jsp/hello.jsp".

But then the spring take the file dispatch "/jsp/hello.jsp" as another request,then it will not find the matched url mapping in the "example" controller.

Why?? IMO,a requst must come from the client to server,the controller receive only one request here "/exmaple/hello_jsp",isn't it?

And How to fix it?

BTW,I can set the url pattern to "/*.xxx",but I do not want the suffix in the url.

Any ideas?


Previous answer did not work...

This posting looks similar: http://forum.springsource.org/showthread.php?55982-No-mapping-found-for-HTTP-request-with-MVC-requests

Summary: change

<servlet-mapping>
    <servlet-name>example</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

to

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

and checking my latest Spring MVC app I use the latter pattern (no * on the end).

0

精彩评论

暂无评论...
验证码 换一张
取 消