0

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

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

这是我项目的结构

这是我的 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>

瓷砖.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-调度程序-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>

弹簧安全.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;
    }
}
4

3 回答 3

2

我不直接知道您的问题的答案,但这是我的处理方法:

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

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

这是相当复杂的,如果事情没有按照正确的顺序发生,你就会遇到问题。我要做的是去掉这些组件中的每一个,然后开始将它们一一添加回来。首先,取出除了欢迎文件配置之外的所有内容,看看是否可以通过转到您的 url 来访问 /index.jsp。然后重新添加 Spring,看看你是否仍然可以使用它。然后添加 URL 重定向,然后是安全性,然后是磁贴。这将帮助您缩小问题的范围。

于 2013-12-05T20:24:02.293 回答
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>

尝试编写代码 =>

于 2015-07-16T13:13:40.930 回答
0

无需编写此代码拼贴集成就足够了 /WEB-INF/pages/

    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
于 2015-07-16T13:17:45.033 回答