Jeong SangCheol
Jeong SangCheol

Reputation: 113

Spring: servlet-mapping -> url-pattern : /* working but can't display

web.xml

<servlet>
    <servlet-name>dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

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

<welcome-file-list>
    <welcome-file>/</welcome-file>
</welcome-file-list>

/WEB-INF/spring/webmvc-config.xml

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="mediaTypes">
        <map>
            <entry key="atom" value="application/atom+xml" />
            <entry key="html" value="text/html" />
            <entry key="json" value="application/json" />
        </map>
    </property>
    <property name="viewResolvers">
        <list>
            <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
            <bean
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="prefix" value="/WEB-INF/views/" />
                <property name="suffix" value=".jsp" />
            </bean>
        </list>
    </property>
    <property name="defaultViews">
        <list>
            <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
        </list>
    </property>
</bean>

Controller

@Controller
@RequestMapping ( "/" )
public class IndexController extends BaseController
{
    @RequestMapping ( "/" )
    public String index ( Model model ){
        System.out.println("AA");
        return index2(model);
    }

    @RequestMapping ( "/index" )
    public String index2 ( Model model ){
        System.out.println("BB");
        return "index";
    }
}

And exist index.jsp File

enter image description here

I guess that is very good working

BBBBBBBBBBBUUUUUUUUUTTTTTTTTT, BUT!

enter image description here

WHY???? WHY???? WHY???? WHY????

And More strange

enter image description here

??????????????????????????????????????????????????????????????????

Controller work it!! but don't display browser

What's going on?

Please help me.


And Log

DispatcherServlet with name 'dispatcher' processing GET request for [/WEB-INF/views/index.jsp]

No mapping found for HTTP request with URI [/WEB-INF/views/index.jsp] in DispatcherServlet with name 'dispatcher'

Upvotes: 1

Views: 13898

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279870

Servlet containers have rules for how they map and handle URI requests. These can be found in the Servlet Specification. It's also important to note that most Servlet containers have a Servlet to handle JSPs, mapped to *.jsp, which is an extension mapping. Tomcat has a JspServlet to do this.

You've mapped your DispatcherServlet to

<url-pattern>/*</url-pattern>

which is a path mapping. Path mappings take precedence over extension mappings. So when you submit your view name

return "index";

Spring will use the ViewResolver

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
</bean>

to resolve a path to use with a RequestDispatcher's forward method. That path will be /WEB-INF/views/index.jsp. Now the Servlet container will receive that path and attempt to find a Servlet to handle it. Since you have a Servlet mapped to /* it will use it, but your DispatcherServlet doesn't have a mapping for that path and therefore responds with a 404.

The simple solution is to change your mapping to /, which is the default handler if no other matches are found. In this case, when you submit your view and the container must find a mapped Servlet, it will find the JspServlet and use it.

Upvotes: 12

Related Questions