需要准备的基础设置:
jar 包的导入:
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.4.jar
commons-io-2.2.jar
commons-lang3-3.2.jar
freemarker-2.3.28.jar
javassist-3.11.0GA.jar
ognl-3.0.21.jar
strtus2-core-2.3.37.jar
xwork-core-2.3.37.jar
web.xml 需要添加的过滤器
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
工程根目录 src 下需要添加的配置文件:
struts.xml
1. 实现Action 接口(可以使用结果集常量字符串)
MyAction.java
/**
*
*/
package com.igeek;
import com.opensymphony.xwork2.Action;
/**
* @author Administrator
*
*/
public class MyAction implements Action {
/***
* Action 接口中有5个常量和一个execute 方法:
* SUCCESS = "success"; 运行成功是返回的逻辑视图
* NONE = "none"; 不反悔任何结果
* ERROR = "error" 运行失败时返回的逻辑试图
* INPUT = "input"; 校验失败时返回的逻辑试图
* LOGIN = "login"; 登录时返回的逻辑试图
*/
@Override
public String execute() throws Exception {
//
System.out.println("Action 编写方式一!!!");
return SUCCESS;
}
}
struts.xml 文件中配置
<package name="default" extends="struts-default" namespace="/">
<action name="my" class="com.igeek.MyAction">
<result name="success">/jsp/result.jsp</result>
</action>
</package>
访问路径:
http://localhost:8080/工程名/my.action
2.继承ActionSupport类(项目中常用的方式,重点掌握)
MyAction2.java
/**
*
*/
package com.igeek;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class MyAction2 extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
/***
* 继承ActionSupport类后,具有Action接口功能,还有验证功能
* 重点推荐方式
*/
@Override
public String execute() throws Exception {
//
System.out.println("Action类编写方式二!!!");
return SUCCESS;
}
}
struts.xml 文件中配置
<package name="default" extends="struts-default" namespace="/">
<!-- Action 的第二种编写方式 -->
<action name="my2" class="com.igeek.MyAction2">
<result name="success">/jsp/result.jsp</result>
</action>
</package>
访问路径:
http://localhost:8080/工程名/my2.action
3. pojo类(非侵入型,简单干净) 没有继承extends 父类,也没有实现implements 接口
MyAction3.java
/**
*
*/
package com.igeek;
/**
* @author Administrator
*
*/
public class MyAction3 {
public String execute() throws Exception{
System.out.println("Action的编写方式三!!!!");
return "success";
}
}
struts.xml 中配置
<package name="default" extends="struts-default" namespace="/">
<!-- Action 的第二种编写方式 -->
<action name="my2" class="com.igeek.MyAction2">
<result name="success">/jsp/result.jsp</result>
</action>
</package>
访问路径:
http://localhost:8080/工程名/my3.action
Action 接收请求参数的三种方式
1.独立使用Action 作为model 通过成员变量属性setter 方法接收参数
login.jsp 中的配置:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login in</title>
</head>
<body>
<h3>方式一: Action 作为表单模型,提供属性和属性对应的getter方法和setter方法</h3>
<form action="${pageContext.request.contentLength }/login.action"
method="post">
username:<input type="text" name="username"><br />
password:<input type="password" name="pwd"><br /> <input
type="submit" value="登录">
</form>
</body>
</html>
LoginAction.java
/**
*
*/
package com.igeek;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class LoginAction extends ActionSupport{
/***
*strtus2 框架自动接收参数
*/
private String username;
private String pwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "LoginAction [username=" + username + ", pwd=" + pwd + "]";
}
@Override
public String execute() throws Exception {
//
System.out.println(toString());
return NONE;
}
}
struts.xml 配置
<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.igeek.LoginAction">
</action>
</package>
访问路径:
http://localhost:8080/工程名/login.jsp
2.独立使用Actin 中创建独立的model 对象和setter ,getter 方法赋值,页面通过ognl 表达式接收参数
login.jsp 中代码实现
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login in</title>
</head>
<body>
<h3>方式二: Action 中创建独立的model 对象和setter ,getter 方法赋值,页面通过ognl 表达式接收参数</h3>
<form action="${pageContext.request.contentLength }/login2.action" method="post">
username:<input type="text" name="userInfo.username"><br/>
password:<input type="password" name="userInfo.pwd" ><br/>
<input type="submit" value="登录">
</body>
</html>
UserInfo.java
/**
*
*/
package com.igeek.model;
/**
* @author Administrator
*
*/
public class UserInfo {
private String username;
private String pwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public UserInfo() {
super();
// TODO Auto-generated constructor stub
System.out.println("UserInfo 的构造方法");
}
}
LoginActoin2.java
/**
*
*/
package com.igeek;
import com.igeek.model.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class LoginAction2 extends ActionSupport {
private UserInfo userInfo;
public UserInfo getUserInfo() {
return userInfo;
}
public void setUserInfo(UserInfo userInfo) {
this.userInfo = userInfo;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println(userInfo.getUsername() + "\t" + userInfo.getPwd());
return NONE;
}
}
struts.xml 配置
<package name="default" extends="struts-default" namespace="/">
<action name="login2" class="com.igeek.LoginAction2">
</action>
</package>
访问路径:
http://localhost:8080/工程名/login.jsp
注意:当前ognl 表达式中的变量要与Action 子类中的成员属性变量保持一致
3.独立使用ModelDriver 接口封装请求的数据
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login in</title>
</head>
<body>
<h3>方式三: Action使用ModelDriver 接口封装请求的数据</h3>
<form action="${pageContext.request.contentLength }/login3.action"
method="post">
username:<input type="text" name="username"><br />
password:<input type="password" name="pwd"><br /> <input
type="submit" value="登录">
</form>
</body>
</html>
UserInfo.java
/**
*
*/
package com.igeek.model;
/**
* @author Administrator
*
*/
public class UserInfo {
private String username;
private String pwd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public UserInfo() {
super();
// TODO Auto-generated constructor stub
System.out.println("UserInfo 的构造方法");
}
}
LoginAction3.java
/**
*
*/
package com.igeek;
import com.igeek.model.UserInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* @author Administrator
*
*/
public class LoginAction3 extends ActionSupport implements ModelDriven<UserInfo>{
//必须初始化javabean 对象
private UserInfo user = new UserInfo();
public UserInfo getUser() {
return user;
}
/***
* getModel 将页面中的数据接收到javabean
*/
@Override
public UserInfo getModel() {
System.out.println(user);
return user;
}
@Override
public String execute() throws Exception {
System.out.println(user.getUsername() + "\t" + user.getPwd());
return NONE;
}
}
struts.xml
<package name="default" extends="struts-default" namespace="/">
<action name="login3" class="com.igeek.LoginAction3">
</action>
</package>
访问路径:
http://localhost:8080/工程名/login.jsp
Action 中通配符的使用
UserAction.java
```java
/**
*
*/
package com.igeek;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class UserAction extends ActionSupport {
/**
*
*
*/
private static final long serialVersionUID = 1L;
public String login() {
System.out.println("执行了Login方法!!!");
return SUCCESS;
}
public String register()
{
System.out.println("执行了Register方法!!!");
return SUCCESS;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}
}
struts.xml
<package name="default" extends="struts-default" namespace="/">
<!-- 使用通配符方式配置执行的方法 !!!!重点掌握 -->
<action name="user_*" class="com.igeek.UserAction"
method="{1}">
<result>/jsp/{1}Result.jsp</result>
</action>
</package>
loginResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
login success
</body>
</html>
registerResult.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
register success
</body>
</html>
访问路径:
http://localhost:8080/工程名/user_login.action 访问login 方法,如果调用register方法,那么就是 http://localhost:8080/工程名/user_register.action
调用动态方法的
UserAction.java
/**
*
*/
package com.igeek;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class UserAction extends ActionSupport {
/**
*
*
*/
private static final long serialVersionUID = 1L;
public String login() {
System.out.println("执行了Login方法!!!");
return SUCCESS;
}
public String register()
{
System.out.println("执行了Register方法!!!");
return SUCCESS;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
return super.execute();
}
}
struts.xml
<!-- 调用动态方法,开启动态方法调用常量 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="default" extends="struts-default" namespace="/">
<!-- 动态方法调用
需要开启常量值 struts.enable.DynamicMethodInvocation
例如 访问路径 http://project/userDynamic!register.action
-->
<action name="useDynamic" class="com.igeek.UserAction">
<result name="success"> /jsp/loginResult.jsp</result>
</action>
</package>
使用servlet 相关的api
IndirectServletAction.java
/**
*
*/
package com.igeek;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class IndirectServletActin extends ActionSupport {
@Override
public String execute() throws Exception {
//接收参数
ActionContext context = ActionContext.getContext();
Map<String,Object> params= context.getParameters();
//接收的值放在数组中
String[] names = (String[]) params.get("name");
System.out.println(names[0]);
return NONE;
}
}
struts.xml
<package name="default" extends="struts-default" namespace="/">
<action name="indirect" class="com.igeek.IndirectServletActin">
</action>
</package>
访问路径
http://localhost:8080/工程名/indirect.action?name=admin
解耦方式调用ActionContext
InjectServletAction.java
/**
*
*/
package com.igeek;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*解耦合方式调用servlet
*/
public class InjectServletAction extends ActionSupport implements ServletRequestAware{
/**
*
*/
private static final long serialVersionUID = 1L;
protected HttpServletRequest request;
/***
* 自动获取request 对象
*/
@Override
public void setServletRequest(HttpServletRequest request) {
//
this.request = request;
}
/***
* 访问连接 http://localhost:8080/InjectServletAction/inject?name=admin
*/
@Override
public String execute() throws Exception {
//获取参数
String name= request.getParameter("name");
System.out.println(name);
request.setAttribute("msg", name);
return SUCCESS;
}
}
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>result</h1>
<br/>
${msg }
</body>
</html>
struts.xml
<package name="default" extends="struts-default" namespace="/">
<action name="inject" class="com.igeek.InjectServletAction">
<result name="success">/jsp/result.jsp</result>
</action>
</package>
访问路径
http://localhost:8080/工程名/inject.action?name=admin
耦合方式调用servlet
DirectServletAction.java
/**
*
*/
package com.igeek;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*耦合方式调用servlet
*/
public class DirectServletAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
//
HttpServletRequest request = ServletActionContext.getRequest();
String name= request.getParameter("name");
System.out.println(name);
return NONE;
}
/***
*
*/
// @Override
// public void setServletRequest(HttpServletRequest arg0) {
// // TODO Auto-generated method stub
//
// }
}
struts.xml
<package name="default" extends="struts-default" namespace="/">
<action name="direct" class="com.igeek.DirectServletAction">
</action>
</package>
访问路径
http://localhost:8080/工程名/direct.action?name=admin
复杂参数封装
product.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/productAction.action" method="post">
商品名称:<input type="text" name="list[0].name"/><br/>
商品价格:<input type="text" name="list[0].price"/><br/>
商品名称:<input type="text" name="list[1].name"/><br/>
商品价格:<input type="text" name="list[1].price"/><br/>
商品名称:<input type="text" name="list[2].name"/><br/>
商品价格:<input type="text" name="list[2].price"/><br/>
<input type="submit" value="批量导入">
</form>
</body>
</html>
ProductAction.java
/**
*
*/
package com.igeek;
import java.util.List;
import com.igeek.model.Product;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*
*/
public class ProductAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Product> list;
public List<Product> getList() {
return list;
}
public void setList(List<Product> list) {
this.list = list;
}
@Override
public String execute() throws Exception {
//
for(Product product : list)
{
System.out.println(product);
}
return NONE;
}
}
Product.java
/**
*
*/
package com.igeek.model;
/**
* @author Administrator
*
*/
public class Product {
private String name;
private double price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Product [name=" + name + ", price=" + price + "]";
}
}
struts.xml
<package name="default" extends="struts-default" namespace="/">
<action name="productAction" class="com.igeek.ProductAction">
</action>
</package>
访问路径
http://localhost:8080/工程名/product.jsp
Map 集合使用
produt.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/productAction2.action" method="post">
商品名称:<input type="text" name="map['one'].name"/><br/>
商品价格:<input type="text" name="map['one'].price"/><br/>
商品名称:<input type="text" name="map['two'].name"/><br/>
商品价格:<input type="text" name="map['two'].price"/><br/>
商品名称:<input type="text" name="map['three'].name"/><br/>
商品价格:<input type="text" name="map['three'].price"/><br/>
<input type="submit" value="批量导入">
</form>
</body>
</html>
ProductActoin2.java
/**
*
*/
package com.igeek;
import java.util.Map;
import com.igeek.model.Product;
import com.opensymphony.xwork2.ActionSupport;
/**
* @author Administrator
*集合 复杂数据的封装
*/
public class ProductAction2 extends ActionSupport {
private Map<String,Product> map;
public Map<String, Product> getMap() {
return map;
}
public void setMap(Map<String, Product> map) {
this.map = map;
}
@Override
public String execute() throws Exception {
// Iterator iterator = map.keySet();
for(String key : map.keySet())
{
Product product = map.get(key);
System.out.println(product);
}
return NONE;
}
}
struts.xml
<package name="default" extends="struts-default" namespace="/">
<action name="productAction" class="com.igeek.ProductAction2">
</action>
</package>
访问路径
http://localhost:8080/工程名/product.jsp
如果是多个人协同开发,可以引入分离文件
如在struts.xml 添加struts-part1.xml 的配置文件
<include file="struts-part1.xml"/>