Spring MVC自己渲染index.jsp

时间:2015-06-22 14:56:23

标签: spring-mvc spring-security

我正在尝试使用Spring MVC + Spring Security + Hibernate创建一个简单的应用程序。

我想要一个针对2个不同流的不同URI,如下所示: <context>/candidate/...<context>/admin/...

为了达到这个目的,我现在面临一个奇怪的问题,即MVC正在从哪个地方挑选index.jsp(我无法理解实际的位置)。这是我的文件:

web.xml

  <display-name>spring</display-name>
  <welcome-file-list>
    <welcome-file>WEB-INF/pages/login.jsp</welcome-file>
  </welcome-file-list>
  <!-- Enables 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>

    <!-- End of Spring Security Configuration -->

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>5</session-timeout>
    </session-config>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
        /WEB-INF/applicationContext.xml,
        /WEB-INF/spring-security.xml
    </param-value>
    </context-param>

</web-app>

的applicationContext.xml

     <context:annotation-config />
        <context:property-placeholder location="/config/appConfig.properties" />
        <context:component-scan base-package="com.ctlin">
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan>

        <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${db.driver}" />
            <property name="url" value="${db.jdbcurl}" />
            <property name="username" value="${db.username}" />
            <property name="password" value="${db.password}" />
            <property name="initialSize" value="${db.minPoolSize}" />
        </bean>

        <!-- Hibernate 4 SessionFactory Bean definition -->
        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="annotatedClasses">
                <list>
                    <value>com.ctlin.bean.CandidateDetailsBean</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <!-- <prop key="hibernate.default_schema">${db.default_schema}</prop> -->
                    <prop key="hibernate.dialect">${db.dialect}</prop>
                    <prop key="hibernate.format_sql">${db.format_sql}</prop>
                    <prop key="hibernate.show_sql">${db.show_sql}</prop>
                    <prop key="hibernate.hbm2ddl.auto">${hbm2ddl.auto}</prop>
                </props>
            </property>
        </bean>

        <tx:annotation-driven transaction-manager="transactionManager"/>
        <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>

        <bean id="authenticateDao" class="com.ctlin.dao.impl.AuthenticateUserDAOImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
        <bean id="authenticateService" class="com.ctlin.service.impl.AuthenticateUserServiceImpl">
            <property name="authenticateUserDao" ref="authenticateDao" />
    </bean>
</beans>

MVC-调度-servlet.xml中

<context:annotation-config />
<context:component-scan base-package="com.ctlin.web.controller" />

<mvc:annotation-driven/>
<mvc:resources mapping="/**assets**" location="/assets/" />

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

弹簧security.xml文件

<http auto-config="true" use-expressions="true">
    <!-- <access-denied-handler error-page="AccessDenied"/> -->

    <intercept-url pattern="/candidate**" access="hasRole('ROLE_USER')" />
    <intercept-url pattern="/**/assets/**" access="permitAll" />
    <intercept-url pattern="/**/login" access="permitAll" />
    <!-- <intercept-url pattern="/admin/**" access="hasRole('ROLE_ADMIN')" /> -->
    <intercept-url pattern="/**" access="isFullyAuthenticated()" />
    <csrf/>
    <session-management invalid-session-url="/login" 
                        session-authentication-error-url="/login?sessionexpired"
                        session-fixation-protection="migrateSession">
        <concurrency-control error-if-maximum-exceeded="true" expired-url="/login" max-sessions="1"/>
    </session-management>
    <form-login login-page="/candidate/login" authentication-failure-url="/candidate/login?error" 
                username-parameter="userId" password-parameter="password" 
                default-target-url="/candidate/registerDetails"
                login-processing-url="/j_spring_security_check"/>
    <logout delete-cookies="JSESSIONID" logout-success-url="/candidate/login?logout" 
                invalidate-session="true" />
</http>

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

控制器

@Controller
@RequestMapping("/candidate")
public class CandidateLoginController {

private static final Logger logger = LoggerFactory.getLogger(CandidateLoginController.class);

    private HttpSession httpSession;

    @Autowired(required=true)
    private IAuthenticateUserSvc authenticateService;

    public final void setAuthenticateService(IAuthenticateUserSvc authenticateService) {
        this.authenticateService = authenticateService;
    }



/**
     * @param null
     * @return the name of the login view
     */
    @RequestMapping(value = { "/", "/login" }, method = RequestMethod.GET)
    protected ModelAndView displayLoginPage(@RequestParam(value = "error", required = false) String error,
            @RequestParam(value = "sessionexpired", required = false) String sessionExpired,
            @RequestParam(value = "logout", required = false) String logout) {

        ModelAndView mav = new ModelAndView("login");
        logger.debug("Inside displayLoginPage method");

        if(error!=null) {

            mav.addObject("error", "Either username or password is incorrect");
        }
        else if(sessionExpired != null) {
            mav.addObject("error", "Your session has expired. Kindly login again.");
        }
        else if (logout != null) {

            mav.addObject("msg", "You've been logged out successfully.");
        }

        return mav;
    }

    @RequestMapping(value = "/registerDetails")
    public ModelAndView registerCandidateDetails() {

        System.out.println("candidate get method");
        ModelAndView model = new ModelAndView();
        model.setViewName("registerDetails");

        return model;

    }
}

的login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page session="true"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Candidate Selection Tool</title>
    <link rel="stylesheet" href="assets/css/bootstrap.min.css" />
    <link rel="stylesheet" href="assets/css/normalize.css" />
    <link rel="stylesheet" href="assets/css/style.css" />
</head>

<body onload='document.loginForm.userId.focus();'>

    <div class="container">
        <!-- start of main body -->
        <div class="row page-header text-center">
            <h1>Candidate Selection Tool</h1>
        </div>

        <div class="col-md-6">
            <form name="loginForm" action="<c:url value='j_spring_security_check' />"
                class="form-horizontal" method="post" role="login">
                <fieldset class="col-md-9 scheduler-border">
                    <legend class="scheduler-border">Login</legend>
                    <c:if test="${not empty error}">
                        <div class="row">
                            <div class="col-md-10 col-md-offset-1">
                                <p class="bg-danger text-center lead">${error}</p>
                            </div>
                        </div>
                    </c:if>
                    <c:if test="${not empty msg}">
                        <div class="row">
                            <div class="col-md-10 col-md-offset-1">
                                <p class="bg-info text-center lead">${msg}</p>
                            </div>
                        </div>
                    </c:if>
                    <div class="form-group has-feedback">
                        <label for="cuid" class="col-md-3 control-label">User ID</label>
                        <div class="col-sm-9">
                            <input id="cuid" type="text"
                                placeholder="Please enter your user id" name="userId"
                                class="form-control" /> <i
                                class="form-control-feedback glyphicon glyphicon-user"></i>
                        </div>
                    </div>
                    <div class="form-group has-feedback">
                        <label for="password" class="col-md-3 control-label">Password</label>
                        <div class="col-sm-9">
                            <input id="password" type="password"
                                placeholder="Please enter your password" name="password"
                                class="form-control" /> <i
                                class="form-control-feedback glyphicon glyphicon-lock"></i>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-4 col-md-2">
                            <button type="submit" class="btn btn-success" name="Login">Login</button>
                        </div>
                        <div class="col-md-offset-2 col-md-4">
                            <button type="reset" class="btn btn-default">Reset</button>
                        </div>
                    </div>
                    <input type="hidden" name="${_csrf.parameterName}"
                        value="${_csrf.token}" />
                </fieldset>
            </form>
        </div>
    </div>
    <!-- end of main body -->

    <script src="assets/js/bootstrap.min.js"></script>
</body>
</html>

网络应用的结构:

────webapp
    ├───assets
    │   ├───css
    │   ├───fonts
    │   ├───img
    │   │   └───background
    │   └───js
    ├───config
    └───WEB-INF
        └───pages
            └───error

现在当我访问此网址时:http://localhost:8282/spring/login,我得到 HTTP状态404 - /spring/WEB-INF/pages/index.jsp (请求的资源不可用。) 为什么要寻找index.jsp

然而,当我访问:http://localhost:8282/spring/candidate/login时,我得到在DispatcherServlet中找不到带有URI [/spring/candidate/assets/css/normalize.css]的HTTP请求的映射,名称为&#39; mvc-dispatcher&#39; 甚至在输入正确的凭据后,它仍然保留在相同的URL上,即循环。

请帮我确定问题。

2 个答案:

答案 0 :(得分:0)

尝试将servlet映射表单更改为*

<servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>*</url-pattern>
</servlet-mapping>

答案 1 :(得分:0)

添加@Paul答案以访问css,您可以尝试使用以下内容:

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