所有的学习我们必须先搭建好Struts2的环境(1、导入对应的jar包,2、web.xml,3、struts.xml)

第一节:Struts2 get/set 自动获取/设置数据

 action代码:
1
package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.Action; 4 5 public class HelloWorldAction implements Action{ 6 7 private String name; 8 9 10 public String getName() { 11 return name; 12 } 13 14 public void setName(String name) { 15 this.name = name; 16 } 17 18 19 @Override 20 public String execute() throws Exception { 21 System.out.println("执行了Action的默认方法"); 22 return SUCCESS; 23 } 24 25 }

 

 struts.xml配置:
1
<?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="helloWorld" extends="struts-default"> 9 <action name="hello" class="com.wishwzp.action.HelloWorldAction"> 10 <result name="success">helloWorld.jsp</result> 11 </action> 12 </package> 13 14 </struts>

 

helloWorld.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 ${name }你好! 11 </body> 12 </html>

 

url访问:http://localhost:8080/Struts2Chap02/hello?name=Struts2(会自动设置数据,输出出来)

第二节:ActionSupport 类引入

extends ActionSupport

以后我们都使用ActionSupport了,不再去implements Action了

 

HelloWorldAction2.java 
1
package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class HelloWorldAction2 extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 private String name; 12 13 14 public String getName() { 15 return name; 16 } 17 18 public void setName(String name) { 19 this.name = name; 20 } 21 22 23 @Override 24 public String execute() throws Exception { 25 System.out.println("执行了HelloWorldAction2 Action的默认方法"); 26 return SUCCESS; 27 } 28 29 }

 

 

 

 struts.xml
1
<?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 8 <package name="helloWorld" extends="struts-default"> 9 <action name="hello" class="com.wishwzp.action.HelloWorldAction2"> 10 <result name="success">helloWorld.jsp</result> 11 </action> 12 </package> 13 14 </struts>

 

 index.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="hello" method="post"> 11 name:<input type="text" name="name"/><input type="submit" value="Submit"/> 12 </form> 13 </body> 14 </html>

 

第三节:Action 设置数据

第一种方式:属性驱动(FieldDriven) A、基本数据类型属性 B、JavaBean 类型属性

第二种方式:模型驱动(ModelDriven)

A、基本数据类型属性

基本数据类型属性,我们在"

B、JavaBean 类型属性

 User.java
1
package com.wishwzp.model; 2 3 public class User { 4 5 private String userName; 6 private String password; 7 8 public String getUserName() { 9 return userName; 10 } 11 public void setUserName(String userName) { 12 this.userName = userName; 13 } 14 public String getPassword() { 15 return password; 16 } 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 }

 

 

 UserService.java
1
package com.wishwzp.service; 2 3 import com.wishwzp.model.User; 4 5 public class UserService { 6 7 public boolean login(User user){ 8 if("java".equals(user.getUserName())&&"123".equals(user.getPassword())){ 9 return true; 10 }else{ 11 return false; 12 } 13 } 14 }

 

 UserAction.java
1
package com.wishwzp.action; 2 3 4 import com.wishwzp.model.User; 5 import com.wishwzp.service.UserService; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 public class UserAction extends ActionSupport{ 9 10 /** 11 * 12 */ 13 private static final long serialVersionUID = 1L; 14 15 private UserService userService=new UserService(); 16 17 private String userName; 18 private String password; 19 20 21 22 public String getUserName() { 23 return userName; 24 } 25 26 27 28 public void setUserName(String userName) { 29 this.userName = userName; 30 } 31 32 33 34 public String getPassword() { 35 return password; 36 } 37 38 39 40 public void setPassword(String password) { 41 this.password = password; 42 } 43 44 45 46 @Override 47 public String execute() throws Exception { 48 System.out.println("执行了UserAction的默认方法"); 49 User user=new User(); 50 user.setUserName(userName); 51 user.setPassword(password); 52 if(userService.login(user)){ 53 return SUCCESS; 54 }else{ 55 return ERROR; 56 } 57 } 58 59 }

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8   <package name="helloWorld" extends="struts-default">
 9       <action name="user" class="com.wishwzp.action.UserAction">
10           <result name="success">success.jsp</result>
11           <result name="error">error.jsp</result>
12       </action>
13  
14   </package>
15 
16 </struts>

 

 login.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="user" method="post"> 11 用户名:<input type="text" name="userName"/> 12 密码:<input type="text" name="password"/> 13 <input type="submit" value="登录"/> 14 </form> 15 </body> 16 </html>

 

 success.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 登录成功! 11 </body> 12 </html>

 

 error.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 登录失败! 11 </body> 12 </html>

 

我们还可以在进行优化一下:
 login2.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="user2" method="post"> 11 用户名:<input type="text" name="user.userName"/> 12 密码:<input type="text" name="user.password"/> 13 <input type="submit" value="登录"/> 14 </form> 15 </body> 16 </html>

 

 UserAction2.java
1
package com.wishwzp.action; 2 3 4 import com.wishwzp.model.User; 5 import com.wishwzp.service.UserService; 6 import com.opensymphony.xwork2.ActionSupport; 7 8 public class UserAction2 extends ActionSupport{ 9 10 /** 11 * 12 */ 13 private static final long serialVersionUID = 1L; 14 15 private UserService userService=new UserService(); 16 17 private User user; 18 19 20 public User getUser() { 21 return user; 22 } 23 24 public void setUser(User user) { 25 this.user = user; 26 } 27 28 29 30 @Override 31 public String execute() throws Exception { 32 System.out.println("执行了UserAction的默认方法"); 33 if(userService.login(user)){ 34 return SUCCESS; 35 }else{ 36 return ERROR; 37 } 38 } 39 40 }

 

 User.java
1
package com.wishwzp.model; 2 3 public class User { 4 5 private String userName; 6 private String password; 7 8 public String getUserName() { 9 return userName; 10 } 11 public void setUserName(String userName) { 12 this.userName = userName; 13 } 14 public String getPassword() { 15 return password; 16 } 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 }

 

 UserService.java
1
package com.wishwzp.service; 2 3 import com.wishwzp.model.User; 4 5 public class UserService { 6 7 public boolean login(User user){ 8 if("java".equals(user.getUserName())&&"123".equals(user.getPassword())){ 9 return true; 10 }else{ 11 return false; 12 } 13 } 14 }

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8   <package name="helloWorld" extends="struts-default">
 9       
10       <action name="user2" class="com.wishwzp.action.UserAction2">
11           <result name="success">success.jsp</result>
12           <result name="error">error.jsp</result>
13       </action>
14 
15   </package>
16 
17 </struts>

 

 error.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 登录失败! 11 </body> 12 </html>

 

 success.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 登录成功! 11 </body> 12 </html>

 

模型驱动(ModelDriven)

 login3.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="user3" method="post"> 11 用户名:<input type="text" name="userName"/> 12 密码:<input type="text" name="password"/> 13 <input type="submit" value="登录"/> 14 </form> 15 </body> 16 </html>

 

 error.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 登录失败! 11 </body> 12 </html>

 

 success.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 登录成功! 11 </body> 12 </html>

 

 1 package com.wishwzp.action;
 2 
 3 
 4 import com.wishwzp.model.User;
 5 import com.wishwzp.service.UserService;
 6 import com.opensymphony.xwork2.ActionSupport;
 7 import com.opensymphony.xwork2.ModelDriven;
 8 
 9 public class UserAction3 extends ActionSupport implements ModelDriven<User>{
10 
11     /**
12      * 
13      */
14     private static final long serialVersionUID = 1L;
15     
16     private UserService userService=new UserService();
17     
18     private User user=new User();
19     
20     @Override
21     public String execute() throws Exception {
22         System.out.println("执行了UserAction3的默认方法");
23         if(userService.login(user)){
24             return SUCCESS;
25         }else{
26             return ERROR;
27         }
28     }
29 
30     @Override
31     public User getModel() {
32         // TODO Auto-generated method stub
33         return user;
34     }
35 
36 }

 

 1 package com.wishwzp.model;
 2 
 3 public class User {
 4 
 5     private String userName;
 6     private String password;
 7     
 8     public String getUserName() {
 9         return userName;
10     }
11     public void setUserName(String userName) {
12         this.userName = userName;
13     }
14     public String getPassword() {
15         return password;
16     }
17     public void setPassword(String password) {
18         this.password = password;
19     }
20 }

 

 1 package com.wishwzp.service;
 2 
 3 import com.wishwzp.model.User;
 4 
 5 public class UserService {
 6 
 7     public boolean login(User user){
 8         if("java".equals(user.getUserName())&&"123".equals(user.getPassword())){
 9             return true;
10         }else{
11             return false;
12         }
13     }
14 }

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8   <package name="helloWorld" extends="struts-default">
 9       
10       <action name="user3" class="com.wishwzp.action.UserAction3">
11           <result name="success">success.jsp</result>
12           <result name="error">error.jsp</result>
13       </action>
14 
15   </package>
16 
17 </struts>

 

第四节:Struts2 处理传入多个值

  1,处理数目不定的字符串;
  2,处理数目不定的JavaBean 对象;

1,处理数目不定的字符串:

 hobby.jsp
1
<%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <form action="hobby" method="post"> 11 爱好: 12 <input type="checkbox" name="hobby" value="唱歌"/>唱歌 13 <input type="checkbox" name="hobby" value="跳舞"/>跳舞 14 <input type="checkbox" name="hobby" value="睡觉"/>睡觉 15 <input type="checkbox" name="hobby" value="玩CF"/>玩CF 16 <input type="submit" value="提交"/> 17 </form> 18 </body> 19 </html>

 

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7     
 8  <package name="helloWorld" extends="struts-default">
 9       <action name="hobby" class="com.wishwzp.action.HobbyAction">
10           <result name="success">success.jsp</result>
11       </action>
12 
13 </struts>
struts.xml

相关文章: