所有的学习我们必须先搭建好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>

(二)Struts2 核心知识的更多相关文章

  1. struts2学习(2)struts2核心知识

    一.Struts2 get/set 自动获取/设置数据 根据上一章.中的源码继续. HelloWorldAction.java中private String name,自动获取/设置name: pac ...

  2. struts2学习(3)struts2核心知识II

    一.struts.xml配置: 1.分模块配置方法: 比如某个系统多个模块,我们把资产管理模块和车辆管理模块,分开,在总的struts.xml配置文件中include他们: 工程结构: struts. ...

  3. struts2学习(4)struts2核心知识III

    一.result配置: result - name 就是前面返回的值,success,error等. type: dispatcher. 默认是这个,底层是jsp的forward: redirect: ...

  4. c++11-17 模板核心知识(十二)—— 模板的模板参数 Template Template Parameters

    概念 举例 模板的模板参数的参数匹配 Template Template Argument Matching 解决办法一 解决办法二 概念 一个模板的参数是模板类型. 举例 在c++11-17 模板核 ...

  5. Docker 与 K8S学习笔记(二)—— 容器核心知识梳理

    本篇主要对容器相关核心知识进行梳理,通过本篇的学习,我们可以对容器相关的概念有一个全面的了解,这样有利于后面的学习. 一.什么是容器? 容器是一种轻量级.可移植.自包含的软件打包技术,使应用程序可以在 ...

  6. Struts2入门1 Struts2基础知识

    Struts2入门1 Struts2基础知识 20131130 代码下载: 链接: http://pan.baidu.com/s/11mYG1 密码: aua5 前言: 之前学习了Spring和Hib ...

  7. 《Maven实战》关联实际工作的核心知识

    通读了<Maven实战>这本书,由于在实际的工作中,对其有一定的操作上的经验.因此,再回头去通读这本书,就能够更加精准的把握里面的核心知识了. 以下我主要从两点去介绍之—— 1> m ...

  8. Linux(1)- 服务器核心知识、Linux入门、VMware与centeos安装、远程连接linux、linux基本命令使用

    一.服务器核心知识 1.电脑和电脑的硬件组成 现在的人们几乎无时无刻不在使用着电脑!不管是桌上型电脑(桌机).笔记型电脑(笔电).平板电脑,还是智慧型手机等等,这些东西都算是电脑.虽然接触这么多,但是 ...

  9. HTML中DOM核心知识有哪些(带实例超详解)

    HTML中DOM核心知识有哪些(带实例超详解) 一.总结: 1.先取html元素,然后再对他进行操作,取的话可以getElementById等 2.操作的话,可以是innerHtml,value等等 ...

随机推荐

  1. 国内外最全的asp.net开源项目

    转自:http://www.cnblogs.com/fengtangquan/archive/2010/10/19/1855472.html 最近一些项目开始用到CMS系统,最开始是研究JAVA的,无 ...

  2. Legal or Not

    Legal or Not Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  3. python 默认的系统编码 sys.setdefaultencoding

    python2.x的编码问题有时让人很头疼,一会ascii,一会unicode. 在脚本里多见这样的操作: import sys reload(sys) sys.setdefaultencoding( ...

  4. HDOJ/HDU 1372 Knight Moves(经典BFS)

    Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...

  5. Datable 详解,及用法

    一.DataSet.DataTable.DataRow.DataColumn 1] 在DataSet中添加DataTable DataSet.Tables.Add(DataTable) 实例: Dat ...

  6. bzoj 3531 [Sdoi2014]旅行(树链剖分,线段树)

    3531: [Sdoi2014]旅行 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 876  Solved: 446[Submit][Status][ ...

  7. 2013 ACM区域赛长沙 C Collision HDU 4793

    题意:在平面上0,0点,有一个半径为R的圆形区域,并且在0,0点固定着一个半径为RM(<R)的圆形障碍物,现在圆形区域外x,y,有一个半径 为r的,并且速度为vx,vy的硬币,如果硬币碰到了障碍 ...

  8. SRM 446(1-250pt, 1-500pt)

    嗯....今天的500确实比较好 DIV1 250 模拟...略 // BEGIN CUT HERE /* * Author: plum rain * score : */ /* */ // END ...

  9. javascript获取选中的文本/html

    首先来谈一下Selection对象和Range对象. Selection是window.getSelection()方法返回的一个对象,用于表示用户选中的文本区域.Selection对象表现为一组Ra ...

  10. 探讨Web组件化的实现

    CMS组件化,简单架构示意图: Web组件使用WebPage+WebAPI的好处: Ø  组件复用(组件条件管理页面复用+获取组件数据API复用). Ø  组件是分布式的第三方应用,本身高内聚.组件之 ...