1.目录结构及导入的jar包
2.web.xml 配置
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
5 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
6
7 index.jsp
8
9
10
11 StrutsPrepareAndExecuteFilter
12 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
13
14
15
16 StrutsPrepareAndExecuteFilter
17 /*18 19
20
2.Action类编写
User JavaBean定义
1 packagecn.test.entity;2
3 public classUser {4
5 privateString name;6 privateString age;7 privateString password;8
9 publicString getAge() {10 returnage;11 }12
13 public voidsetAge(String age) {14 this.age =age;15 }16
17 publicString getName() {18 returnname;19 }20
21 public voidsetName(String name) {22 this.name =name;23 }24
25
26 publicString getPassword() {27 returnpassword;28 }29
30 public voidsetPassword(String password) {31 this.password =password;32 }33
34 @Override35 publicString toString() {36 return "User [age=" + age + ", name=" + name + ", password=" +password37 + "]";38 }39
40 }
通过继承ActionSupport类创建 Action控制器:
(1)不管是通过Javabean 还是直接通过属性字段获取请求数据,都需要在Action类设置setter getter方法
(2)ActionSupport类实现了Action接口,所以此处可以选择重写execute() 方法,处理请求
(3)此处通过ActionContext类访问Servlet API,获取session对象
1 packagecn.test.action;2
3 importcn.test.entity.User;4
5 importcom.opensymphony.xwork2.Action;6 importcom.opensymphony.xwork2.ActionContext;7 importcom.opensymphony.xwork2.ActionSupport;8
9 /**
10 * Action处理请求参数-属性驱动 strtus2中页面的请求数据和Action有两种基本的对应方式,分别是字段驱动和模型驱动方式。字段驱动也叫属性驱动11 * 属性驱动是指通字段进行数据传递,包括:与基本数据类型的属性对应,直接使用域对象(也就是javaBean)12 *13 *@authorAdministrator14 *15 */
16 public class UserLoginAction extendsActionSupport {17
18 private static final long serialVersionUID = 1L;19 private User user;//通过javaBean获取前端传递的数据;必须设置getter setter方法
20
21 publicUser getUser() {22 returnuser;23 }24
25 public voidsetUser(User user) {26 this.user =user;27 }28
29 @Override30 public String execute() throwsException { //继承ActionSupport接口的Action类中,必须要有重写的execute方法31 //获取Context对象: 通过ActionContext类访问Servlet API
32 ActionContext context =ActionContext.getContext();33 if("admin".equals(user.getName()) && "123".equals(user.getPassword())){34 //把用户名和密码放到session中
35 context.getSession().put("username", user.getName());36 context.getSession().put("password", user.getPassword());37 returnAction.SUCCESS;38 } else{39 context.getSession().put("error", "用户登陆失败");40 return ERROR;//Action接口中定义的常量
41 }42
43 }44
45
46
47 }
3. struts.xml中 action的配置
当Action类返回“success”逻辑视图名,请求转发到loginSuccess.jsp 页面,当返回error 逻辑视图名,请求转发到 loginError.jsp 页面。
红色部分是配置了一个默认的action,当请求一个不存在的action时,一般页面会报404 not found,当我们配置默认的action,如果请求一个不存在的aciton时,页面就会跳转到
我们指定的错误页面,而不是提示404, 此处跳转到error.jsp 页面
1 <?xml version="1.0" encoding="UTF-8"?>
2 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4 "http://struts.apache.org/dtds/struts-2.3.dtd">
5
6
7
8
9
10
11
12
13
14 /error.jsp
15
16
17
18
19
20 /loginSuccess.jsp
21 /loginError.jsp
22
23
24
4. JSP页面编写
登录页面userLogin.jsp(浏览器直接请求该页面,进入到登陆页面):
1
2
3
4 String path =request.getContextPath();5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";6 %>
7
8
9
10
11
12
userLogin page13
14
15
16
17 用户名:
18 密码:
19
20
21
22
23
登陆成功页面
1
2
3
4 String path =request.getContextPath();5 String basePath = request.getScheme() + "://"
6 + request.getServerName() + ":" +request.getServerPort()7 + path + "/";8 %>
9
10
11
12
13
14
My JSP 'success.jsp' starting page15
16
17
18 登陆成功,session中的登陆用户名是:
19
20 采用OGNL与Struts2标签输出Action类的属性值(Action类获取到的请求参数值)
21 name= password=
22 采用EL表达式 输出Action类的属性值(${对象名.属性名 }):
23 name=${user.name }24
25
登陆失败页面
1
2
3
4 String path =request.getContextPath();5 String basePath = request.getScheme() + "://"
6 + request.getServerName() + ":" +request.getServerPort()7 + path + "/";8 %>
9
10
11
12
13
14
登陆失败页面15
16
17
18 控制器返回的错误信息为:${error };
19 单击链接返回登陆页面
20
21
默认Action指定的跳转页面
1
2
3 String path =request.getContextPath();4 String basePath = request.getScheme() + "://"
5 + request.getServerName() + ":" +request.getServerPort()6 + path + "/";7 %>
8
9
10
11
12
13
My JSP 'success.jsp' starting page14
15
16 真不巧,您访问的页面走丢了
17 单击链接返回登陆页面
18
19
部署测试:
(1)输入一个访问不到的请求:http://localhost:8080/strutsstu3/user/userLogi ,此时处理请求的是默认Action
(2) 单击上一步的链接, 转到 http://localhost:8080/strutsstu3/userLogin.jsp
输入admin 和一个错误的密码,结果如下:
单击链接返回登陆页,输入admin 和123 提交,结果如下
注:
(1)在使用Javabean属性方式传值时,如果JSP页面时负责取值的,那么取值的格式必须为“对象.属性名”;如果JSP页面时负责传值的,那么传值的格式可以为“对象.属性名”,也可以直接是“属性名”;
(2)Struts2的Action没有与ServletAPI发生耦合,但是web开发中经常会用到ServletAPI中的对象,如用户登陆成功,则应该将用户信息保存到HttpSession中,而最常用的ServletAPI就是HttpSession, HttpServletRequest、ServletContext这三个接口,Strtus2访问ServletAPI,可以分为2类:与Servlet API 解耦的访问方式和与servlet API耦合的访问方式。
与Servlet API 解耦的访问方式:
(a)使用ActionContext类获取Servlet 对象对用的Map对象
例如:
ActionContext context = ActionContext.getContext();
Map request = (Map) context.get("request");//ActionContext类没有提供getRequest()这样的方法来获取HttpServletRequest对应的map对象,要想得到HttpServletRequest对象对应的map对象,需要为get()方法传递request参数
context.put("username", "admin"); //放到request作用域,相当于ServletAPI中国年的HttpServletRequest的setAttribute()方法
context.getSession().put("username", "admin"); // 把信息放到session作用域
context.getApplication().put("username", "admin"); // 把信息放到Application 作用域
(b) 实现特定接口获取Servlet API对象
与Servlet API 耦合的访问方式:
示例代码下载地址 https://github.com/liuch0228/Struts2SSH.git