Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring 3.0 MVC mvc:view-controller tag

Here's a snippet of my mvc-config.xml:

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

<mvc:view-controller path="/index" view-name="welcome"/>    
<mvc:view-controller path="/static/login" view-name="/static/login"/>   
<mvc:view-controller path="/login" view-name="/static/login"/>

I have the welcome.jsp on /WEB-INF/view/ directory and login.jsp on /WEB-INF/view/static/.

This works for '/index' and '/login' paths. But I'm getting 404 response for '/static/login' when invoked from the browser. I'm expecting that '/static/login/' and '/login' should behave the same.

What could be wrong here?

Would appreciate any help.

Thanks!

Here's the web.xml:

<web-app version="2.5" 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">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value>
    </context-param>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <!-- Enables clean URLs with JSP views e.g. /welcome instead of /app/welcome -->
    <filter>
        <filter-name>UrlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>UrlRewriteFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <!-- Handles all requests into the application -->
    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring/*.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Maps all /app requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

</web-app>

and here is the urlrewrite.xml:

<urlrewrite default-match-type="wildcard">
    <rule>
        <from>/</from>
        <to>/app/welcome</to>
    </rule>
    <rule>
        <from>/static/**</from>
        <to last="true">/static/$1</to>
    </rule>

    <rule>
        <from>/**</from>
        <to last="true">/app/$1</to>
    </rule>
    <outbound-rule>
        <from>/app/**</from>
        <to>/$1</to>
    </outbound-rule>    
</urlrewrite>

Environment: I'm using SpringSource tc Server Dev Edition v2.0
Spring version: 3.0.3.RELEASE

like image 644
gouki Avatar asked Jun 17 '10 07:06

gouki


People also ask

Which tag in Spring MVC will be used to?

Spring Framework provides spring's form tag library for JSP views in Spring's Web MVC framework. In Spring Framework, we use Java Server Pages(JSP) as a view component to interact with the user. From version 2.0, Spring Framework provides a comprehensive set of data binding-aware tags.

Which package in Spring Framework architecture provides the dependency injection feature?

Core container Core (spring-core) is the core of the framework that power features such as Inversion of Control and dependency injection.

Which of the following statements about front controller in Spring MVC is true?

Answer: Front Controller is responsible to handle the entire incoming request of an application. In Spring MVC, dispatcher servlet acts as a front controller and handles the entire incoming requests.

Which of the following options can be used to add attributes in a model in spring boot?

@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view.


2 Answers

Request for /static/login can't get into your DispatcherServlet, because it matches the rewriting rule from /static/** to /static/$1 with last = "true", and therefore doesn't match the rule from /** to /app/$1, which leads to DispatcherServlet. See UrlRewriteFilter docs for more info.

like image 111
axtavt Avatar answered Sep 27 '22 02:09

axtavt


This is working fine for me, can you tell me what is your Dispatcher Servlet mappings? It would be nice if you can attach the entire web.xml content.

like image 24
Amit Goyal Avatar answered Sep 27 '22 02:09

Amit Goyal