spring mvc 3 (3.2.5) + tiles 3 error 404 not resource found

2.3k views Asked by At

when i load my app i get only 404 resource not found error.. no logs on tomcat at all..

here you can see my project configuration:

This is the structure of my project

this is my web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">

    <display-name>Spring Web MVC Application</display-name>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/pages/*</url-pattern>
        <!--url-pattern>*.jsp</url-pattern-->
    </servlet-mapping>

    <urlrewrite default-match-type="wildcard">
    <rule>
        <from>/</from>
        <to>/pages/</to>
    </rule>
    <rule>
        <from>/**</from>
        <to>/pages/$1</to>
    </rule>
    <outbound-rule>
        <from>/pages/**</from>
        <to>/$1</to>
    </outbound-rule>
    </urlrewrite>

    <filter>
        <filter-name>urlRewriteFilter</filter-name>
        <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    </filter>

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




    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml,
                     /WEB-INF/spring-security.xml,
                     /WEB-INF/spring-database.xml

        </param-value>

    </context-param>

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

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

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

    <welcome-file-list>
        <welcome-file>/index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

tiles.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE tiles-definitions PUBLIC
        "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN"
        "http://tiles.apache.org/dtds/tiles-config_2_0.dtd">

<tiles-definitions>
    <definition name="base.definition"
                template="/WEB-INF/pages/layout.jsp">
        <put-attribute name="title" value="" />
        <put-attribute name="header" value="/WEB-INF/pages/header.jsp" />
        <put-attribute name="menu" value="/WEB-INF/pages/menu.jsp" />
        <put-attribute name="body" value="" />
        <put-attribute name="footer" value="/WEB-INF/pages/footer.jsp" />
    </definition>

    <definition name="contact" extends="base.definition">
        <put-attribute name="title" value="Contact Manager" />
        <put-attribute name="body" value="/WEB-INF/pages/prueba2.jsp" />
    </definition>

</tiles-definitions>

mvc-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.mkyong.common.controller" />

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

        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

    <bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>mymessages</value>
            </list>
        </property>
    </bean>


    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass">
            <value>
                org.springframework.web.servlet.view.tiles3.TilesView
            </value>
        </property>
    </bean>
    <bean id="tilesConfigurer"
          class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/WEB-INF/tiles.xml</value>
            </list>
        </property>
    </bean>


    <mvc:annotation-driven />

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <context:annotation-config />

</beans>

spring-security.xml

<beans:beans xmlns="http://www.springframework.org/schema/security"
             xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.0.3.xsd">

    <!--import resource="../database/spring-database.xml"/-->


    <http auto-config="true" access-denied-page="/accessDenied">

        <intercept-url pattern="/login*" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <intercept-url pattern="/*" access="ROLE_USER" />
        <form-login login-page="/login" default-target-url="/prueba2"
                    authentication-failure-url="/loginfailed.jsp"/>
        <logout logout-success-url="/logout" />
    </http>

    <authentication-manager>
        <authentication-provider>

            <jdbc-user-service data-source-ref="dataSource"

                               users-by-username-query="
                    select username,password, enabled
                    from users where USERNAME=?"

                               authorities-by-username-query="
                    select u.username, ur.authority from users u, user_roles ur
                    where u.user_id = ur.user_id and u.username =?  "

                    />
        </authentication-provider>
    </authentication-manager>


</beans:beans>

this is my controller:

@Controller
@RequestMapping("/prueba2")
public class controller2 extends AbstractController {

    Stock stock=new Stock();
    List stockList=new ArrayList<Stock>();
    ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/config/BeanLocations.xml");
    StockBo stockBo = (StockBo)appContext.getBean("stockBo");


   /*@RequestMapping(method = RequestMethod.GET)
    public Stock returnCustomer(ModelMap model) {
        stock=stockBo.findByStockCode("7668");
        model.addAttribute("miStock", stock);
        return stock;


    }*/





    protected ModelAndView handleRequestInternal (HttpServletRequest req, HttpServletResponse res) throws Exception{
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = user.getUsername();
        Map<String, Object> model = new HashMap<String, Object>();
        stockList=stockBo.findAll();
        stock=stockBo.findByStockCode("7668");
        model.put("listaStock",stockList);
        model.put("miStock", stock);
        model.put("nombreUsuario", name);
        System.out.println("lista objetos--->" + stockList.toString());
        return new ModelAndView( "prueba2", "model", model );
    }

    public StockBo getStockBo() {
        return stockBo;
    }

    public void setStockBo(StockBo stockBo) {
        this.stockBo = stockBo;
    }
}
3

There are 3 answers

1
Mark On BEST ANSWER

I don't know the answer to your question directly, but here's how I'd approach it:

First, if I understand your problem correctly, you're expecting that going to the url http://localhost:8080/SpringExample will redirect you to the login page, but that's not happening (you're seeing a 404). I assume that you've checked that your application is actually deployed to the SpringExample context.

You've several things that could influence how URLs are interpreted and redirected. First, you've got Spring, which maps any request starting with /pages/ to the Spring dispatcher. Next, you've got some URL rewriting, which because it's done in a filter, should happen ahead of the spring dispatcher servlet. You've also got a listener that loads all of the spring context configs. Third, you've got Spring Security which is implemented in a filter. Lastly, you've got the tiles configuration, which could in theory also cause a 404 error if a resource isn't found, though it looks like yours is ok.

This is pretty complex, and if things don't happen in the correct order, you'll have a problem. What I would do is strip out each of these components, and then start adding them back in one by one. First, take everything out except the welcome-file configuration and see if you can get to /index.jsp by going to your url. Then add Spring back in and see if you can still get to it. Then add in URL redirection, then security, then tiles. This will help you to narrow down your problem.

0
Amreesh Verma On
 <bean id="viewResolver" class="org.springframework.web.servlet.view.tiles3.TilesViewResolver"/>
 <bean id="tilesConfigurer"
    class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/tiles.xml</value>
        </list>
    </property>
</bean>

Try to write the code =>

0
Amreesh Verma On

No need to write this code tiles integration is enough /WEB-INF/pages/

    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>