我的jsps正在查看中,

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
<display-name>Spring-MVC-Security</display-name>

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


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

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

<servlet>
    <servlet-name>servletDispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>servletDispatcher</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>login</welcome-file>
</welcome-file-list>
</web-app>


servletDispatcher-servlet.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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.2.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<mvc:annotation-driven/>
<!-- <mvc:default-servlet-handler/> -->

<context:component-scan base-package="com.spring.controller"></context:component-scan>
<context:component-scan base-package="com.spring.util"></context:component-scan>

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

</beans>


spring-security.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/security  http://www.springframework.org/schema/security/spring-security-3.2.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">

<security:http auto-config="true"  use-expressions="true">
    <security:intercept-url pattern="/login" access="permitAll" />
    <security:intercept-url pattern="/logout" access="permitAll" />
    <security:intercept-url pattern="/accessdenied" access="permitAll" />
    <security:intercept-url pattern="/**" access="hasRole('ROLE_USER')" />
    <security:form-login login-page="/login" default-target-url="/list" authentication-failure-url="/accessdenied" />
    <security:logout logout-success-url="/logout" />
</security:http>

<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider>
        <security:user-service>
            <security:user name="lokesh" password="password" authorities="ROLE_USER" />
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

</beans>


控制器:

package com.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class LoginController {

@RequestMapping({"/","/login"})
public String login()
{
    return "login";
}
}


当我试图击中

http://localhost:8080/Spring-MVC-Security/ ,
url is getting redirected to
http://localhost:8080/Spring-MVC-Security/login


但是我从tomcat收到404错误,并在控制台上收到以下警告:

Jan 13, 2014 12:03:41 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/Spring-MVC-Security/WEB-INF/views/login.jsp] in DispatcherServlet with name 'servletDispatcher'


解析视图后,似乎再次击中了ServletDispatcher。

请指教。谢谢

最佳答案

首先,您要加载servletDispatcher上下文两次。调度程序Servlet将根据约定自动发现/WEB-INF/servletDispatcher-servlet.xml文件-因此不需要在contextConfigLocation中指定

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
         <!-- Remove servletDispatcher-servlet.xml -->
         /WEB-INF/spring-security.xml
    </param-value>
</context-param>


尝试将调度程序servlet映射从/*更改为/

<servlet-mapping>
    <servlet-name>servletDispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


另外-由于您的登录控制器除了返回login视图外没有执行其他任何操作,因此可以将其替换为视图控制器。

<mvc:view-controller path="/login" />

09-27 20:11