(二)Struts2 核心知识
所有的学习我们必须先搭建好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、基本数据类型属性
基本数据类型属性,我们在"第一节:Struts2 get/set 自动获取/设置数据"中已经涉及过了,就这是基本数据类型属性的学习。
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>
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.Action; 4 5 public class HobbyAction implements Action{ 6 7 private String[] hobby; 8 9 10 public String[] getHobby() { 11 return hobby; 12 } 13 14 public void setHobby(String[] hobby) { 15 this.hobby = hobby; 16 } 17 18 19 @Override 20 public String execute() throws Exception { 21 System.out.println("执行了Action的默认方法"); 22 if(hobby!=null){ 23 for(String h:hobby){ 24 System.out.println(h); 25 } 26 } 27 return SUCCESS; 28 } 29 30 }
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 Ok! 11 </body> 12 </html>
2,处理数目不定的JavaBean 对象:
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="student" method="post"> 11 <table> 12 <tr> 13 <th>姓名</th> 14 <th>性别</th> 15 <th>年龄</th> 16 </tr> 17 <tr> 18 <td><input type="text" name="students[0].name"/></td> 19 <td><input type="text" name="students[0].sex"/></td> 20 <td><input type="text" name="students[0].age"/></td> 21 </tr> 22 <tr> 23 <td><input type="text" name="students[1].name"/></td> 24 <td><input type="text" name="students[1].sex"/></td> 25 <td><input type="text" name="students[1].age"/></td> 26 </tr> 27 <tr> 28 <td colspan="3"> 29 <input type="submit" value="提交"/> 30 </td> 31 </tr> 32 </table> 33 </form> 34 </body> 35 </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 10 <action name="student" class="com.wishwzp.action.StudentAction"> 11 <result name="success">success.jsp</result> 12 </action> 13 </package> 14 15 </struts>
1 package com.wishwzp.action; 2 3 import java.util.List; 4 5 import com.wishwzp.model.Student; 6 import com.opensymphony.xwork2.Action; 7 8 public class StudentAction implements Action{ 9 10 private List<Student> students; 11 12 public List<Student> getStudents() { 13 return students; 14 } 15 16 public void setStudents(List<Student> students) { 17 this.students = students; 18 } 19 20 21 @Override 22 public String execute() throws Exception { 23 System.out.println("执行了Action的默认方法"); 24 for(Student s:students){ 25 System.out.println(s); 26 } 27 return SUCCESS; 28 } 29 30 }
1 package com.wishwzp.model; 2 3 public class Student { 4 5 private String name; 6 private String sex; 7 private int age; 8 9 public String getName() { 10 return name; 11 } 12 public void setName(String name) { 13 this.name = name; 14 } 15 public String getSex() { 16 return sex; 17 } 18 public void setSex(String sex) { 19 this.sex = sex; 20 } 21 public int getAge() { 22 return age; 23 } 24 public void setAge(int age) { 25 this.age = age; 26 } 27 @Override 28 public String toString() { 29 return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]"; 30 } 31 32 33 34 }
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 Ok! 11 </body> 12 </html>
第五节:struts.xml 配置
一,pageckage 配置
name 包名(用来区分模块化的)
extends 继承
namespace 包命名空间(包命名空间命名后必须在前面加上命名,如下面访问的URL:http://localhost:8080/Struts2Chap02_03/fore/studentList
或http://localhost:8080/Struts2Chap02_03/back/studentList)
abstract 抽象包
二,action 配置
name action 名
class 处理类
method 方法(区分执行哪个方法的,默认情况下的话是execute()-----------像下面的例子中
<package name="background" namespace="/back" extends="struts-default">
<action name="studentList" class="com.wishwzp.action.BackStudent" method="show">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action>
</package>
这里我们执行的方法就是show()方法了
)
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="foreground" namespace="/fore" extends="struts-default"> 9 <action name="studentList" class="com.wishwzp.action.ForeStudent"> 10 <result name="success">${pageContext.request.contextPath}/success.jsp</result> 11 </action> 12 13 </package> 14 15 <package name="background" namespace="/back" extends="struts-default"> 16 <action name="studentList" class="com.wishwzp.action.BackStudent" method="show"> 17 <result name="success">${pageContext.request.contextPath}/success.jsp</result> 18 </action> 19 20 </package> 21 22 23 <!-- abstract抽象包,用来过滤一些关键字的 --> 24 <package name="infoFilter" abstract="true"></package> 25 26 </struts>
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class BackStudent extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 @Override 13 public String execute() throws Exception { 14 System.out.println("执行了BackStudent Action的默认方法"); 15 return SUCCESS; 16 } 17 18 public String show()throws Exception{ 19 System.out.println("执行了 BackStudent show方法"); 20 return SUCCESS; 21 } 22 23 }
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class ForeStudent extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 @Override 13 public String execute() throws Exception { 14 System.out.println("执行了ForeStudent Action的默认方法"); 15 return SUCCESS; 16 } 17 18 }
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 Ok! 11 </body> 12 </html>
三,分模块配置方法
<include file="" ></include>
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 <include file="cheliang.xml"></include> 9 <include file="zichan.xml"></include> 10 11 </struts>
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="cheliang" namespace="/cheliang" extends="struts-default"> 9 <action name="cheliang" class="com.wishwzp.action.CheLiangAction"> 10 <result name="success">${pageContext.request.contextPath}/success.jsp</result> 11 </action> 12 13 </package> 14 15 </struts>
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="zichan" namespace="/zichan" extends="struts-default"> 9 <action name="zichan" class="com.wishwzp.action.ZiChanAction"> 10 <result name="success">${pageContext.request.contextPath}/success.jsp</result> 11 </action> 12 13 </package> 14 15 </struts>
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class CheLiangAction extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 @Override 13 public String execute() throws Exception { 14 System.out.println("执行了CheLiangAction Action的默认方法"); 15 return SUCCESS; 16 } 17 18 19 20 }
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class ZiChanAction extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 @Override 13 public String execute() throws Exception { 14 System.out.println("执行了ZiChanAction Action的默认方法"); 15 return SUCCESS; 16 } 17 18 }
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 Ok! 11 </body> 12 </html>
四,使用通配符
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class GradeAction extends ActionSupport{ 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 public String list()throws Exception{ 20 System.out.println("班级查找"); 21 name="班级查找"; 22 return SUCCESS; 23 } 24 25 public String add()throws Exception{ 26 System.out.println("班级添加"); 27 name="班级添加"; 28 return SUCCESS; 29 } 30 31 public String update()throws Exception{ 32 System.out.println("班级修改"); 33 name="班级修改"; 34 return SUCCESS; 35 } 36 37 public String delete()throws Exception{ 38 System.out.println("班级删除"); 39 name="班级删除"; 40 return SUCCESS; 41 } 42 43 44 }
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class StudentAction extends ActionSupport{ 6 7 private String name; 8 9 10 11 public String getName() { 12 return name; 13 } 14 15 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 22 23 public String list()throws Exception{ 24 System.out.println("学生查找"); 25 name="学生查找"; 26 return SUCCESS; 27 } 28 29 public String add()throws Exception{ 30 System.out.println("学生添加"); 31 name="学生添加"; 32 return SUCCESS; 33 } 34 35 public String update()throws Exception{ 36 System.out.println("学生修改"); 37 name="学生修改"; 38 return SUCCESS; 39 } 40 41 public String delete()throws Exception{ 42 System.out.println("学生删除"); 43 name="学生删除"; 44 return SUCCESS; 45 } 46 47 48 }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <package name="manage" namespace="/" extends="struts-default"> <!-- 这里我们使用的单个去匹配 --> <!-- <action name="student_*" class="com.wishwzp.action.StudentAction" method="{1}"> <result name="success">success.jsp</result> </action> <action name="grade_*" class="com.wishwzp.action.GradeAction" method="{1}"> <result name="success">success.jsp</result> </action> --> <!-- 这里我们使用两个*号来匹配 --> <action name="*_*" class="com.wishwzp.action.{1}Action" method="{2}"> <result name="success">success.jsp</result> </action> </package> </struts>
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 <h1>通配符</h1> 11 <a href="Student_list" target="_blank">学生信息查询</a> 12 <a href="Student_add" target="_blank">学生信息添加</a> 13 <a href="Student_update" target="_blank">学生信息修改</a> 14 <a href="Student_delete" target="_blank">学生信息删除</a> 15 <br/><br/> 16 <a href="Grade_list" target="_blank">班级信息查询</a> 17 <a href="Grade_add" target="_blank">班级信息添加</a> 18 <a href="Grade_update" target="_blank">班级信息修改</a> 19 <a href="Grade_delete" target="_blank">班级信息删除</a> <br/> 20 </body> 21 </html>
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 Ok!${name } 11 </body> 12 </html>
第六节:动态方法调用
在struts.xml中
开启动态方法调用:<constant name="struts.enable.DynamicMethodInvocation" value="true" />
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 <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 9 10 <package name="manage" namespace="/" extends="struts-default"> 11 12 <action name="student" class="com.wishwzp.action.StudentAction"> 13 <result name="success">success.jsp</result> 14 </action> 15 16 <action name="grade" class="com.wishwzp.action.GradeAction"> 17 <result name="success">success.jsp</result> 18 </action> 19 20 </package> 21 22 </struts>
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class GradeAction extends ActionSupport{ 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 public String list()throws Exception{ 20 System.out.println("班级查找"); 21 name="班级查找"; 22 return SUCCESS; 23 } 24 25 public String add()throws Exception{ 26 System.out.println("班级添加"); 27 name="班级添加"; 28 return SUCCESS; 29 } 30 31 public String update()throws Exception{ 32 System.out.println("班级修改"); 33 name="班级修改"; 34 return SUCCESS; 35 } 36 37 public String delete()throws Exception{ 38 System.out.println("班级删除"); 39 name="班级删除"; 40 return SUCCESS; 41 } 42 43 44 }
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class StudentAction extends ActionSupport{ 6 7 private String name; 8 9 10 11 public String getName() { 12 return name; 13 } 14 15 16 17 public void setName(String name) { 18 this.name = name; 19 } 20 21 22 23 public String list()throws Exception{ 24 System.out.println("学生查找"); 25 name="学生查找"; 26 return SUCCESS; 27 } 28 29 public String add()throws Exception{ 30 System.out.println("学生添加"); 31 name="学生添加"; 32 return SUCCESS; 33 } 34 35 public String update()throws Exception{ 36 System.out.println("学生修改"); 37 name="学生修改"; 38 return SUCCESS; 39 } 40 41 public String delete()throws Exception{ 42 System.out.println("学生删除"); 43 name="学生删除"; 44 return SUCCESS; 45 } 46 47 48 }
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 <h1>动态方法调用</h1> 11 <a href="student!list" target="_blank">学生信息查询</a> 12 <a href="student!add" target="_blank">学生信息添加</a> 13 <a href="student!update" target="_blank">学生信息修改</a> 14 <a href="student!delete" target="_blank">学生信息删除</a> 15 <br/><br/> 16 <a href="grade!list" target="_blank">班级信息查询</a> 17 <a href="grade!add" target="_blank">班级信息添加</a> 18 <a href="grade!update" target="_blank">班级信息修改</a> 19 <a href="grade!delete" target="_blank">班级信息删除</a> <br/> 20 </body> 21 </html>
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 Ok!${name } 11 </body> 12 </html>
第七节:Action 生命周期
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 9 <package name="manage" namespace="/" extends="struts-default"> 10 11 <action name="hello" class="com.wishwzp.action.HelloAction"> 12 <result name="success">success.jsp</result> 13 </action> 14 </package> 15 16 </struts>
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class HelloAction extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 public HelloAction() { 13 System.out.println(this); 14 } 15 16 @Override 17 public String execute() throws Exception { 18 // TODO Auto-generated method stub 19 return SUCCESS; 20 } 21 }
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 Ok! 11 </body> 12 </html>
url地址:http://localhost:8080/项目名/hello
我们首先去寻找struts.xml里面的action等于hello的,然后看是哪个class类,进入对应的class类,默认情况下会先执行对应的构造方法,然后去执行execute()的方法,在execute()方法中最后返回SUCCESS,
再去struts.xml文件找对应的result的SUCCESS,最后跳转到对应的页面中去。
第八节:result 配置
1,type 默认是dispatcher 内部转发;
2,type 为redirect 重定向;
3,type 为chain 链条;
4,type 为redirectAction 重定向到action;
上面4 个常用,一定要掌握;
其他 freemarker freemarker 模版
httpheader 返回一个已配置好的HTTP 头信息响应
stream 将原始数据作为流传递回浏览器端,
velocity 呈现Velocity 模板
xslt 该XML 可以通过XSL 模板进行转换
plaintext 返回普通文本类容
result 全局配置
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 <a href="hello?name=Struts2" target="_blank">默认转发_dispatcher</a><br/><!-- 会带数据 --> 11 <a href="hello!r?name=Struts2" target="_blank">重定向_redirect</a><br/><!-- 不会带数据 --><!-- 这里有一个“!”是使用了动态方法调用 --> 12 <a href="hello!c?name=Struts2" target="_blank">链条_chain</a><br/><!-- 会到另一个action,同时会将之前的数据也带过来 --> 13 <a href="hello!ra?name=Struts2" target="_blank">重定向到Action_redirectAction</a><br/><!-- 会到另一个action,但不会将之前的数据带过来 --> 14 15 <a href="hello" target="_blank">全局result配置</a><br/><!-- 没有的话就在全局定义一个错误的输出跳转 --> 16 </body> 17 </html>
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 <constant name="struts.enable.DynamicMethodInvocation" value="true" /><!-- 动态方法调用 --> 9 10 <package name="manage" namespace="/" extends="struts-default"> 11 <global-results> 12 <result name="error">error.jsp</result><!-- 全局错误信息 --> 13 </global-results> 14 15 <action name="hello" class="com.wishwzp.action.HelloAction"> 16 <result name="success" type="dispatcher">success.jsp</result> 17 <result name="r" type="redirect">success.jsp</result> 18 <result name="c" type="chain">hello2</result> 19 <result name="ra" type="redirectAction">hello2</result> 20 </action> 21 22 <action name="hello2" class="com.wishwzp.action.HelloAction2"> 23 <result name="success" type="dispatcher">success.jsp</result> 24 </action> 25 </package> 26 27 </struts>
HelloAction.java
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class HelloAction extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 private String name; 13 private String error; 14 15 16 17 public String getName() { 18 return name; 19 } 20 21 public void setName(String name) { 22 this.name = name; 23 } 24 25 26 27 public String getError() { 28 return error; 29 } 30 31 public void setError(String error) { 32 this.error = error; 33 } 34 35 @Override 36 public String execute() throws Exception { 37 if(name==null || "".equals(name)){ 38 this.error="name是空"; 39 return ERROR; 40 } 41 return SUCCESS; 42 } 43 44 45 public String r()throws Exception{ 46 return "r"; 47 } 48 49 public String c()throws Exception{ 50 return "c"; 51 } 52 53 public String ra()throws Exception{ 54 return "ra"; 55 } 56 57 58 59 }
HelloAction2.java
1 package com.wishwzp.action; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class HelloAction2 extends ActionSupport{ 6 7 /** 8 * 9 */ 10 private static final long serialVersionUID = 1L; 11 12 private String name2; 13 14 public String getName2() { 15 return name2; 16 } 17 18 19 public void setName2(String name2) { 20 this.name2 = name2; 21 } 22 23 24 @Override 25 public String execute() throws Exception { 26 this.name2="你好啊"; 27 return SUCCESS; 28 } 29 30 31 }
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 错误信息:${error } 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 Name:${name }<br/> 11 Name2:${name2 } 12 </body> 13 </html>