自己做的,都是稳定版本.利用jquery的ajax发送请求到Action.Spring负责组织Action与业务实现类.有一段时间没搞Java EE了,开始恢复功力.
- 首先来看下整个项目结构:需要导入的struts2和spring2.5的包.大致就是这些.
- 登录页面是Index.jsp:
代码1 < table class ="wwFormTable" >
2 < tr >
3 < td class ="tdLabel" >
4 < label for ="Login_username" class ="label" >
5 用户名:
6 </ label >
7 </ td >
8 < td >
9 < input type ="text" name ="username" value ="" id ="Login_username" />
10 </ td >
11 </ tr >
12 < tr >
13 < td class ="tdLabel" >
14 < label for ="Login_password" class ="label" >
15 密码:
16 </ label >
17 </ td >
18 < td >
19 < input type ="password" name ="password" id ="Login_password" />
20 </ td >
21 </ tr >
22 < tr >
23 < td colspan ="2" >
24 < div align ="right" >
25 < input type ="button" id ="login" value ="登录" />
26 </ div >
27 </ td >
28 </ tr >
29 </ table >
本来用的是struts2标签,为了看清楚标签生成的是什么样的html,俺就把生成后的html拷过来直接用了.免得影响我用jquery读取表单元素.
接下来就需要jquery来发送ajax请求到我的LoginAction:
2 $(document).ready(
3 function () {
4 $( " #login " ).click(
5 function () {
6 $.ajax( {
7 url : " Login.action " ,
8 data : " username= "
9 + $( " input[name='username'] " ).val()
10 + " &password= "
11 + $( " input[name='password'] " ).val(),
12 success : function (msg) {
13 $( " #message " ).html(msg);
14 }
15 });
16 });
17 });
18 < / script>
#message是一个层,用来显示成功返回的内容.
3.实体类,UserInfo类,用来保存登录表单数据:
2 /**
3 * 用户实体类
4 *
5 * @author Mr.King
6 *
7 */
8 public class UserInfo {
9
10 public UserInfo(){
11 }
12
13 private String username;
14 private String password;
15
16 public String getUsername() {
17 return username;
18 }
19
20 public void setUsername(String username) {
21 this .username = username;
22 }
23
24 public String getPassword() {
25 return password;
26 }
27
28 public void setPassword(String password) {
29 this .password = password;
30 }
31 }
4.UserInfoManager业务处理类,没有实现什么接口,仅做简单演示.
2
3 public class UserInfoManager {
4
5 public boolean checkUser(String name, String pass) {
6
7 return name.trim().equalsIgnoreCase( " admin " ) && pass.equals( " admin " ) ? true
8 : false ;
9 }
10 }
没有与数据库交互,直接判断了一下字符串.呵呵 .留作Hibernate来完成吧.
5.接下来当然要看下web.xml是怎么配置struts和spring的:
2 < web-app version ="2.5" xmlns ="http://java.sun.com/xml/ns/javaee"
3 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
5 <!-- 配置struts2拦截器 -->
6 < filter >
7 < filter-name > struts2 </ filter-name >
8 < filter-class > org.apache.struts2.dispatcher.FilterDispatcher </ filter-class >
9 </ filter >
10 < filter-mapping >
11 < filter-name > struts2 </ filter-name >
12 < url-pattern > /* </ url-pattern >
13 </ filter-mapping >
14 <!-- 配置默认首页 -->
15 < welcome-file-list >
16 < welcome-file > index.jsp </ welcome-file >
17 </ welcome-file-list >
18 < login-config >
19 < auth-method > BASIC </ auth-method >
20 </ login-config >
21 <!-- 配置spring监听 -->
22 < listener >
23 < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
24 </ listener >
25 < context-param >
26 < param-name > contextConfigLocation </ param-name >
27 < param-value > classpath:applicationContext.xml </ param-value >
28 </ context-param >
29 </ web-app >
tomcat根据web.xml的配置.来监听struts与spring.
6.接下来是struts.xml.需要对Action进行配置.
2 <! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" >
3 < struts >
4 < include file ="struts-default.xml" ></ include >
5 < package name ="default" extends ="struts-default" >
6 <!-- Action在applicationContext.xml里进行配置 -->
7 < action name ="Login" class ="userlogin" >
8 < result name ="success" > /welcome.jsp </ result >
9 < result name ="error" > /error.jsp </ result >
10 </ action >
11 </ package >
12 </ struts >
这里因为与Spring结合了,当然代码组织就需要靠spring来完成了.所以这里的class只指定了一个名称.会在spring配置文件里的bean id里见到.那里会指定登录处理的Action.
7.Spring配置文件applicationContext.xml:
2 < beans default-autowire ="byName"
3 xmlns ="http://www.springframework.org/schema/beans" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:p ="http://www.springframework.org/schema/p"
5 xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
6 <!-- 配置业务处理类 -->
7 < bean id ="userUtil" class ="com.mag.util.UserInfoManager" ></ bean >
8 <!-- 配置Action,织入业务类 -->
9 < bean id ="userlogin" class ="com.mag.str2.LoginAction" >
10 < property name ="userInfoManager" ref ="userUtil" ></ property >
11 </ bean >
12 </ beans >
这里将业务类与Action组织在一起了,所谓的注入吧.
8.最后就来看下我们的Action了:
2
3 import com.mag.beans.UserInfo;
4 import com.mag.util.UserInfoManager;
5 import com.opensymphony.xwork2.Action;
6 import com.opensymphony.xwork2.ActionContext;
7 import com.opensymphony.xwork2.ModelDriven;
8
9 public class LoginAction implements Action, ModelDriven < UserInfo > {
10
11 private UserInfo userInfo = new UserInfo();
12
13 public UserInfo getModel() {
14 return userInfo;
15 }
16
17 private UserInfoManager userInfoManager = null ;
18
19 public void setUserInfoManager(UserInfoManager userInfoManager) {
20 this .userInfoManager = userInfoManager;
21 }
22
23 public UserInfoManager getUserInfoManager() {
24 return userInfoManager;
25 }
26
27 public String execute() throws Exception {
28
29 if (userInfoManager.checkUser(userInfo.getUsername(), userInfo
30 .getPassword())) {
31 // 将用户名存入session
32 ActionContext.getContext().getSession().put( " user " ,
33 userInfo.getUsername());
34 return SUCCESS;
35 } else {
36 return ERROR;
37 }
38 }
39 }
40
这里实现了两个接口,Action主要用到了它的常量SUCCESS,ERROR.ModelDriven用于处理接收的表单元素与实体类的对应.这里有三种方法,我选择了这种,感觉最为方便.呵呵.execute()方法是必须的.感觉struts2的确不错.简化了很多.UserInfoManager就是我们的业务处理类,userInfoManager对象已经在spring配置文件里配置好了.对象的实例化就交给我们的框架来完成了.
根据返回的结果,在struts.xml配置的resutl就可以帮你进行跳转了.
9.welcome.jsp和error.jsp很简单,只返回两串文本.
welcome.jsp:
欢迎,${sessionScope.user}.您已经登录!
error.jsp:
2
3 < font color ="red" > 用户名或密码错误! </ font >
返回的结果jquery会处理,并显示在message层里.
看一下效果图:
和预期结果一样,提示登录成功.完整的过程就是这样,代码全贴出来了,没有做过多解释,仅仅是一步一步做出来的.可能写的不是很周到,也有可能有出入,欢迎指正.
今天再查与hibernate集成时,看到篇不错的文章.Struts2.1.x+Spring2.5.x+Hibernate3.2.x实例教程