2015-09-25 146 views
0

我试过下面的代码,用tomcat7运行样本spring rest应用程序。它工作正常,但它无法识别HelloController。调试程序无法访问Hellocontroller。 http://localhost:8080/Sample/welcome引发404错误。弹簧控制器无法识别

127.0.0.1 - - - 25/09/2015:23:21:27 +0530 "GET/HTTP/1.1" 404 - 
127.0.0.1 - - - 25/09/2015:23:21:28 +0530 "GET /Sample/ HTTP/1.1" 200 - 
127.0.0.1 - - - 25/09/2015:23:21:34 +0530 "GET /Sample/welcome HTTP/1.1" 404 - 

HelloController.java

package com.mkyong.web.controller; 


    @Controller 
public class HelloController { 

    @RequestMapping(value = { "/", "/welcome**" }, method = RequestMethod.GET) 
    public ModelAndView welcomePage() { 

     ModelAndView model = new ModelAndView(); 
     model.addObject("title", "Spring Security Hello World"); 
     model.addObject("message", "This is welcome page!"); 
     model.setViewName("hello"); 
     return model; 

    } 

    @RequestMapping(value = "/admin**", method = RequestMethod.GET) 
    public ModelAndView adminPage() { 

     ModelAndView model = new ModelAndView(); 
     model.addObject("title", "Spring Security Hello World"); 
     model.addObject("message", "This is protected page!"); 
     model.setViewName("admin"); 

     return model; 

    } 

} 

的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 
    <display-name>Sample</display-name> 
    <display-name>Spring MVC Application</display-name> 

    <!-- Spring MVC --> 
    <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>/</url-pattern> 
    </servlet-mapping> 

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

    <listener> 
     <listener-class>org.springframework.security.web.session.HttpSessionEventPublisher 
     </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.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
</web-app> 

弹簧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" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.2.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

    <context:annotation-config /> 
    <context:spring-configured /> 
    <mvc:annotation-driven /> 

<mvc:default-servlet-handler/> 

    <http auto-config="true"> 
     <intercept-url pattern="/admin**" access="ROLE_USER" /> 
    </http> 

    <authentication-manager> 
     <authentication-provider> 
     <user-service> 
     <user name="k" password="123" authorities="ROLE_USER" /> 
     </user-service> 
     </authentication-provider> 
    </authentication-manager> 

</beans:beans> 

这里是我的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" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans  
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<context:component-scan base-package="com.mkyong.web.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> 

</beans> 

回答

0

您好像缺少spring-servlet-config文件和内容位于spring-security文件中。在WEB-INF文件夹中创建一个文件mvc-dispatcher-servlet.xml,并添加以下代码行:

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

    <context:annotation-config /> 
    <context:spring-configured /> 
    <mvc:annotation-driven /> 

    <mvc:default-servlet-handler/> 
</beans:beans> 

你会删除从安全配置文件被移动的内容。