spring mvc 3(3.2.5)+ tiles 3错误404找不到资源

时间:2013-12-05 19:19:33

标签: spring spring-mvc spring-3 tiles2 tiles-3

当我加载我的应用程序时,我只得到404资源未找到错误..根本没有登录tomcat ...

在这里你可以看到我的项目配置:

This is the structure of my project

这是我的web.xml文件:

<?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文件

<?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>

弹簧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>

这是我的控制者:

@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 个答案:

答案 0 :(得分:1)

我不直接知道你问题的答案,但这是我接近它的方法:

首先,如果我正确理解您的问题,您希望转到网址http://localhost:8080/SpringExample会将您重定向到登录页面,但这不会发生(您看到的是404)。我假设您已经检查过您的应用程序实际部署到SpringExample上下文。

您可能会遇到几个可能影响URL解释和重定向的事情。首先,你有Spring,它将以/ pages /开头的任何请求映射到Spring调度程序。接下来,你有一些URL重写,因为它是在过滤器中完成的,应该在spring dispatcher servlet之前进行。你还有一个加载所有弹簧上下文配置的监听器。第三,你有一个在过滤器中实现的Spring Security。最后,您已经获得了tile配置,如果找不到资源,理论上也可能导致404错误,尽管看起来你的确定没问题。

这非常复杂,如果事情没有以正确的顺序发生,你就会遇到问题。我要做的是去除每个组件,然后开始逐个添加它们。首先,除欢迎文件配置外,取出所有内容,看看是否可以通过转到您的网址到达/index.jsp。然后重新添加Spring,看看你是否仍然可以使用它。然后添加URL重定向,然后添加安全性,然后添加切片。这有助于您缩小问题范围。

答案 1 :(得分:0)

 <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>

尝试编写代码=&gt;

答案 2 :(得分:0)

无需编写此代码瓷砖集成就足够了                      / WEB-INF /页/

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