我正在嘗試使用Spring MVC + Spring Security + Hibernate創建一個簡單的應用程序。Spring MVC自身渲染index.jsp
我希望有一個不同的URI爲2個不同的流量,這將是這樣的: - <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 - /春/ WEB-INF /頁/ index.jsp的(所請求的資源不可用。) 爲什麼它尋找index.jsp
?
然而,當我訪問:http://localhost:8282/spring/candidate/login,我得到沒有映射發現HTTP請求的URI與在DispatcherServlet的名稱爲「MVC-調度」甚至後[/spring/candidate/assets/css/normalize.css]輸入正確的憑證,它仍然在同一個URL,循環類型。
請幫我確定問題。
當我從'改變了URL模式/'到'*',tomcat未能以URL無效的錯誤開始。 –
什麼錯誤?,你可以粘貼堆棧跟蹤。 – paul
這裏是stackstrace的代碼片段: '由...引起:java.lang。IllegalArgumentException:無效 *在servlet映射中# –