1,先引进相应的包Spring 3.0 Core(Spring的核心包) /Spring 3.0 Web/Spring 3.0 J2EE/Spring 3.0 AOP(Spring代理)/Spring 3.0 Persistence Core。这里的3.0是在此只是版本编号,最好是3.0及3.0以上的版本。
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="cn.owntt.service.impl,cn.owntt.web.controller"/>
<mvc:annotation-driven/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"></bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/view/" p:suffix=".jsp">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
</bean>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/bak/*"/>
<bean class="cn.owntt.web.interceptor.BakInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
?3,配置实体类
cn.owntt.entity包
package cn.owntt.entity;
public class Account {
Integer id;
String username;
String password;
public Account() {
}
public Account(String username, String password) {
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4,Service层声明接口
package cn.owntt.service;
import cn.owntt.entity.Account;
public interface IAccountService {
public Account checkLogin(Account account);
}
package cn.owntt.service.impl;
import org.springframework.stereotype.Service;
import cn.owntt.entity.Account;
import cn.owntt.service.IAccountService;
@Service(value="accountService")
public class AccountServiceImpl implements IAccountService {
public Account checkLogin(Account account) {
if(account.getUsername().equals("admin")&&account.getPassword().equals("123")){
account.setPassword(null);
return account;
}
return null;
}
}
5,配置拦截器
package cn.owntt.web.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
public class BakInterceptor implements HandlerInterceptor{
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception ex)
throws Exception {
}
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView modelAndView) throws Exception {
}
/**
* 在请求进入controller之前调用
* handler下一个拦截器
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse httpServletResponse,
Object handler) throws Exception {
HttpSession session=request.getSession(false);
if(session!=null&&session.getAttribute("account")!=null){
return true;
}
return false;
}
}
6,配置SpringMVC中的Controller,与struts2中的Action注解使用很类似
package cn.owntt.web.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.owntt.entity.Account;
import cn.owntt.service.IAccountService;
@Controller(value="accountController")
@RequestMapping("/account")
public class AccountController {
@Autowired
IAccountService accountService;
@RequestMapping("/login")
public String login(Account account,String xx,HttpServletRequest request,ModelMap modelMap){
System.out.println(xx);
Account _account=accountService.checkLogin(account);
if(_account!=null){
request.getSession().setAttribute("account", _account);
return "bak/index";
}else{
modelMap.put("info","账号密码错误!");
return "login";
}}}
7,写出?相应的页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>登陆</title>
</head>
<body>
<h1>登陆</h1>
<hr/>
<font color="red">${info}</font>
<form action="account/login.do" method="post">
账户:<input type="text" name="username" value=""/><br/>
密码:<input type="password" name="password" value=""/><br/>
<input type="hidden" name="xx" value="秘密"/>
<input type="submit" value="登陆"/>
</form>
</body>
</html>
?<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>后台主页</title>
</head>
<body>
<h1>后台主页</h1>${sessionScope.account.username}
</body>
</html>
?成功后后台主页会显示admin
?