(二)Struts2 核心知识

时间:2024-11-18 10:36:14

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

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

 action代码:
1 package com.wishwzp.action; import com.opensymphony.xwork2.Action; public class HelloWorldAction implements Action{ private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String execute() throws Exception {
System.out.println("执行了Action的默认方法");
return SUCCESS;
} }
 struts.xml配置:
1 <?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> <package name="helloWorld" extends="struts-default">
<action name="hello" class="com.wishwzp.action.HelloWorldAction">
<result name="success">helloWorld.jsp</result>
</action>
</package> </struts>
helloWorld.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${name }你好!
</body>
</html>

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

第二节:ActionSupport 类引入

extends ActionSupport

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

HelloWorldAction2.java 
1 package com.wishwzp.action; import com.opensymphony.xwork2.ActionSupport; public class HelloWorldAction2 extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L;
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String execute() throws Exception {
System.out.println("执行了HelloWorldAction2 Action的默认方法");
return SUCCESS;
} }
 struts.xml
1 <?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> <package name="helloWorld" extends="struts-default">
<action name="hello" class="com.wishwzp.action.HelloWorldAction2">
<result name="success">helloWorld.jsp</result>
</action>
</package> </struts>
 index.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="hello" method="post">
name:<input type="text" name="name"/><input type="submit" value="Submit"/>
</form>
</body>
</html>

第三节:Action 设置数据

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

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

A、基本数据类型属性

基本数据类型属性,我们在"第一节:Struts2 get/set 自动获取/设置数据"中已经涉及过了,就这是基本数据类型属性的学习。

B、JavaBean 类型属性

 User.java
1 package com.wishwzp.model; public class User { private String userName;
private String password; 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;
}
}
 UserService.java
1 package com.wishwzp.service; import com.wishwzp.model.User; public class UserService { public boolean login(User user){
if("java".equals(user.getUserName())&&"123".equals(user.getPassword())){
return true;
}else{
return false;
}
}
}
 UserAction.java
1 package com.wishwzp.action; import com.wishwzp.model.User;
import com.wishwzp.service.UserService;
import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L; private UserService userService=new UserService(); private String userName;
private String password; 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;
} @Override
public String execute() throws Exception {
System.out.println("执行了UserAction的默认方法");
User user=new User();
user.setUserName(userName);
user.setPassword(password);
if(userService.login(user)){
return SUCCESS;
}else{
return ERROR;
}
} }
 <?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> <package name="helloWorld" extends="struts-default">
<action name="user" class="com.wishwzp.action.UserAction">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action> </package> </struts>
 login.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user" method="post">
用户名:<input type="text" name="userName"/>
密码:<input type="text" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
 success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录成功!
</body>
</html>
 error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录失败!
</body>
</html>
我们还可以在进行优化一下:
 login2.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user2" method="post">
用户名:<input type="text" name="user.userName"/>
密码:<input type="text" name="user.password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
 UserAction2.java
1 package com.wishwzp.action; import com.wishwzp.model.User;
import com.wishwzp.service.UserService;
import com.opensymphony.xwork2.ActionSupport; public class UserAction2 extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L; private UserService userService=new UserService(); private User user; public User getUser() {
return user;
} public void setUser(User user) {
this.user = user;
} @Override
public String execute() throws Exception {
System.out.println("执行了UserAction的默认方法");
if(userService.login(user)){
return SUCCESS;
}else{
return ERROR;
}
} }
 User.java
1 package com.wishwzp.model; public class User { private String userName;
private String password; 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;
}
}
 UserService.java
1 package com.wishwzp.service; import com.wishwzp.model.User; public class UserService { public boolean login(User user){
if("java".equals(user.getUserName())&&"123".equals(user.getPassword())){
return true;
}else{
return false;
}
}
}
 <?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> <package name="helloWorld" extends="struts-default"> <action name="user2" class="com.wishwzp.action.UserAction2">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action> </package> </struts>
 error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录失败!
</body>
</html>
 success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录成功!
</body>
</html>

模型驱动(ModelDriven)

 login3.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="user3" method="post">
用户名:<input type="text" name="userName"/>
密码:<input type="text" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
 error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录失败!
</body>
</html>
 success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
登录成功!
</body>
</html>
 package com.wishwzp.action;

 import com.wishwzp.model.User;
import com.wishwzp.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; public class UserAction3 extends ActionSupport implements ModelDriven<User>{ /**
*
*/
private static final long serialVersionUID = 1L; private UserService userService=new UserService(); private User user=new User(); @Override
public String execute() throws Exception {
System.out.println("执行了UserAction3的默认方法");
if(userService.login(user)){
return SUCCESS;
}else{
return ERROR;
}
} @Override
public User getModel() {
// TODO Auto-generated method stub
return user;
} }
 package com.wishwzp.model;

 public class User {

     private String userName;
private String password; 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;
}
}
 package com.wishwzp.service;

 import com.wishwzp.model.User;

 public class UserService {

     public boolean login(User user){
if("java".equals(user.getUserName())&&"123".equals(user.getPassword())){
return true;
}else{
return false;
}
}
}
 <?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> <package name="helloWorld" extends="struts-default"> <action name="user3" class="com.wishwzp.action.UserAction3">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action> </package> </struts>

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

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

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

 hobby.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="hobby" method="post">
爱好:
<input type="checkbox" name="hobby" value="唱歌"/>唱歌
<input type="checkbox" name="hobby" value="跳舞"/>跳舞
<input type="checkbox" name="hobby" value="睡觉"/>睡觉
<input type="checkbox" name="hobby" value="玩CF"/>玩CF
<input type="submit" value="提交"/>
</form>
</body>
</html>
 <?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> <package name="helloWorld" extends="struts-default">
<action name="hobby" class="com.wishwzp.action.HobbyAction">
<result name="success">success.jsp</result>
</action> </struts>

struts.xml

 package com.wishwzp.action;

 import com.opensymphony.xwork2.Action;

 public class HobbyAction implements Action{

     private String[] hobby;

     public String[] getHobby() {
return hobby;
} public void setHobby(String[] hobby) {
this.hobby = hobby;
} @Override
public String execute() throws Exception {
System.out.println("执行了Action的默认方法");
if(hobby!=null){
for(String h:hobby){
System.out.println(h);
}
}
return SUCCESS;
} }

HobbyAction.java

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Ok!
</body>
</html>

success.jsp

2,处理数目不定的JavaBean 对象:

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="student" method="post">
<table>
<tr>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
</tr>
<tr>
<td><input type="text" name="students[0].name"/></td>
<td><input type="text" name="students[0].sex"/></td>
<td><input type="text" name="students[0].age"/></td>
</tr>
<tr>
<td><input type="text" name="students[1].name"/></td>
<td><input type="text" name="students[1].sex"/></td>
<td><input type="text" name="students[1].age"/></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" value="提交"/>
</td>
</tr>
</table>
</form>
</body>
</html>

addstudents.jsp

 <?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> <package name="helloWorld" extends="struts-default"> <action name="student" class="com.wishwzp.action.StudentAction">
<result name="success">success.jsp</result>
</action>
</package> </struts>

struts.xml

 package com.wishwzp.action;

 import java.util.List;

 import com.wishwzp.model.Student;
import com.opensymphony.xwork2.Action; public class StudentAction implements Action{ private List<Student> students; public List<Student> getStudents() {
return students;
} public void setStudents(List<Student> students) {
this.students = students;
} @Override
public String execute() throws Exception {
System.out.println("执行了Action的默认方法");
for(Student s:students){
System.out.println(s);
}
return SUCCESS;
} }

StudentAction.java

 package com.wishwzp.model;

 public class Student {

     private String name;
private String sex;
private int age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]";
} }

Student.java

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Ok!
</body>
</html>

success.jsp

第五节: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()方法了

            )

 <?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> <package name="foreground" namespace="/fore" extends="struts-default">
<action name="studentList" class="com.wishwzp.action.ForeStudent">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action> </package> <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> <!-- abstract抽象包,用来过滤一些关键字的 -->
<package name="infoFilter" abstract="true"></package> </struts>

struts.xml

 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class BackStudent extends ActionSupport{

     /**
*
*/
private static final long serialVersionUID = 1L; @Override
public String execute() throws Exception {
System.out.println("执行了BackStudent Action的默认方法");
return SUCCESS;
} public String show()throws Exception{
System.out.println("执行了 BackStudent show方法");
return SUCCESS;
} }

BackStudent.java

 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class ForeStudent extends ActionSupport{

     /**
*
*/
private static final long serialVersionUID = 1L; @Override
public String execute() throws Exception {
System.out.println("执行了ForeStudent Action的默认方法");
return SUCCESS;
} }

ForeStudent.java

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Ok!
</body>
</html>

success.jsp

三,分模块配置方法
  <include file="" ></include>

 <?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> <include file="cheliang.xml"></include>
<include file="zichan.xml"></include> </struts>

struts.xml

 <?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> <package name="cheliang" namespace="/cheliang" extends="struts-default">
<action name="cheliang" class="com.wishwzp.action.CheLiangAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action> </package> </struts>

cheliang.xml

 <?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> <package name="zichan" namespace="/zichan" extends="struts-default">
<action name="zichan" class="com.wishwzp.action.ZiChanAction">
<result name="success">${pageContext.request.contextPath}/success.jsp</result>
</action> </package> </struts>

zichan.xml

 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class CheLiangAction extends ActionSupport{

     /**
*
*/
private static final long serialVersionUID = 1L; @Override
public String execute() throws Exception {
System.out.println("执行了CheLiangAction Action的默认方法");
return SUCCESS;
} }

CheLiangAction.java

 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class ZiChanAction extends ActionSupport{

     /**
*
*/
private static final long serialVersionUID = 1L; @Override
public String execute() throws Exception {
System.out.println("执行了ZiChanAction Action的默认方法");
return SUCCESS;
} }

ZiChanAction.java

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Ok!
</body>
</html>

success.jsp

四,使用通配符

 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class GradeAction extends ActionSupport{

     private String name;

     public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String list()throws Exception{
System.out.println("班级查找");
name="班级查找";
return SUCCESS;
} public String add()throws Exception{
System.out.println("班级添加");
name="班级添加";
return SUCCESS;
} public String update()throws Exception{
System.out.println("班级修改");
name="班级修改";
return SUCCESS;
} public String delete()throws Exception{
System.out.println("班级删除");
name="班级删除";
return SUCCESS;
} }

GradeAction.java

 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class StudentAction extends ActionSupport{

     private String name;

     public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String list()throws Exception{
System.out.println("学生查找");
name="学生查找";
return SUCCESS;
} public String add()throws Exception{
System.out.println("学生添加");
name="学生添加";
return SUCCESS;
} public String update()throws Exception{
System.out.println("学生修改");
name="学生修改";
return SUCCESS;
} public String delete()throws Exception{
System.out.println("学生删除");
name="学生删除";
return SUCCESS;
} }

StudentAction.java

<?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>
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>通配符</h1>
<a href="Student_list" target="_blank">学生信息查询</a>&nbsp;
<a href="Student_add" target="_blank">学生信息添加</a>&nbsp;
<a href="Student_update" target="_blank">学生信息修改</a>&nbsp;
<a href="Student_delete" target="_blank">学生信息删除</a>&nbsp;
<br/><br/>
<a href="Grade_list" target="_blank">班级信息查询</a>&nbsp;
<a href="Grade_add" target="_blank">班级信息添加</a>&nbsp;
<a href="Grade_update" target="_blank">班级信息修改</a>&nbsp;
<a href="Grade_delete" target="_blank">班级信息删除</a>&nbsp;<br/>
</body>
</html>

index.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Ok!${name }
</body>
</html>

success.jsp

第六节:动态方法调用

    在struts.xml中

    开启动态方法调用:<constant name="struts.enable.DynamicMethodInvocation" value="true" />

 <?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">
<result name="success">success.jsp</result>
</action> <action name="grade" class="com.wishwzp.action.GradeAction">
<result name="success">success.jsp</result>
</action> </package> </struts>

struts.xml

 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class GradeAction extends ActionSupport{

     private String name;

     public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String list()throws Exception{
System.out.println("班级查找");
name="班级查找";
return SUCCESS;
} public String add()throws Exception{
System.out.println("班级添加");
name="班级添加";
return SUCCESS;
} public String update()throws Exception{
System.out.println("班级修改");
name="班级修改";
return SUCCESS;
} public String delete()throws Exception{
System.out.println("班级删除");
name="班级删除";
return SUCCESS;
} }

GradeAction.java

 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class StudentAction extends ActionSupport{

     private String name;

     public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String list()throws Exception{
System.out.println("学生查找");
name="学生查找";
return SUCCESS;
} public String add()throws Exception{
System.out.println("学生添加");
name="学生添加";
return SUCCESS;
} public String update()throws Exception{
System.out.println("学生修改");
name="学生修改";
return SUCCESS;
} public String delete()throws Exception{
System.out.println("学生删除");
name="学生删除";
return SUCCESS;
} }

StudentAction.java

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>动态方法调用</h1>
<a href="student!list" target="_blank">学生信息查询</a>&nbsp;
<a href="student!add" target="_blank">学生信息添加</a>&nbsp;
<a href="student!update" target="_blank">学生信息修改</a>&nbsp;
<a href="student!delete" target="_blank">学生信息删除</a>&nbsp;
<br/><br/>
<a href="grade!list" target="_blank">班级信息查询</a>&nbsp;
<a href="grade!add" target="_blank">班级信息添加</a>&nbsp;
<a href="grade!update" target="_blank">班级信息修改</a>&nbsp;
<a href="grade!delete" target="_blank">班级信息删除</a>&nbsp;<br/>
</body>
</html>

index.jsp

 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Ok!${name }
</body>
</html>

success.jsp

第七节:Action 生命周期

 <?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> <package name="manage" namespace="/" extends="struts-default"> <action name="hello" class="com.wishwzp.action.HelloAction">
<result name="success">success.jsp</result>
</action>
</package> </struts>
 package com.wishwzp.action;

 import com.opensymphony.xwork2.ActionSupport;

 public class HelloAction extends ActionSupport{

     /**
*
*/
private static final long serialVersionUID = 1L; public HelloAction() {
System.out.println(this);
} @Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return SUCCESS;
}
}
 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Ok!
</body>
</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"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="hello?name=Struts2" target="_blank">默认转发_dispatcher</a><br/><!-- 会带数据 -->
<a href="hello!r?name=Struts2" target="_blank">重定向_redirect</a><br/><!-- 不会带数据 --><!-- 这里有一个“!”是使用了动态方法调用 -->
<a href="hello!c?name=Struts2" target="_blank">链条_chain</a><br/><!-- 会到另一个action,同时会将之前的数据也带过来 -->
<a href="hello!ra?name=Struts2" target="_blank">重定向到Action_redirectAction</a><br/><!-- 会到另一个action,但不会将之前的数据带过来 --> <a href="hello" target="_blank">全局result配置</a><br/><!-- 没有的话就在全局定义一个错误的输出跳转 -->
</body>
</html>
 struts.xml
1 <?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">
<global-results>
<result name="error">error.jsp</result><!-- 全局错误信息 -->
</global-results> <action name="hello" class="com.wishwzp.action.HelloAction">
<result name="success" type="dispatcher">success.jsp</result>
<result name="r" type="redirect">success.jsp</result>
<result name="c" type="chain">hello2</result>
<result name="ra" type="redirectAction">hello2</result>
</action> <action name="hello2" class="com.wishwzp.action.HelloAction2">
<result name="success" type="dispatcher">success.jsp</result>
</action>
</package> </struts>
 HelloAction.java
1 package com.wishwzp.action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L; private String name;
private String error; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getError() {
return error;
} public void setError(String error) {
this.error = error;
} @Override
public String execute() throws Exception {
if(name==null || "".equals(name)){
this.error="name是空";
return ERROR;
}
return SUCCESS;
} public String r()throws Exception{
return "r";
} public String c()throws Exception{
return "c";
} public String ra()throws Exception{
return "ra";
} }
 HelloAction2.java
1 package com.wishwzp.action; import com.opensymphony.xwork2.ActionSupport; public class HelloAction2 extends ActionSupport{ /**
*
*/
private static final long serialVersionUID = 1L; private String name2; public String getName2() {
return name2;
} public void setName2(String name2) {
this.name2 = name2;
} @Override
public String execute() throws Exception {
this.name2="你好啊";
return SUCCESS;
} }
 error.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
错误信息:${error }
</body>
</html>
 success.jsp
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
Name:${name }<br/>
Name2:${name2 }
</body>
</html>