在开始前需要确定一个概念,便于理解:
struts2相当于自定mvc中的action层。
1、环境搭建
①导jar包
②在web.xml中配置过滤器
注:filter-class 为StrutsPrepareAndExecuteFilter类的全类名。
③ struts.xml
struts.xml(核心配置文件)
下面是配置的代码
struts.xml
另外两个文件的代码:
struts-sy.xml(此文件相当于自定义mvc中的mvc.xml) struts-base.xml(下面为配置的一些常量)
2、Hello struts!
package com.struts.study.one.demo;
public class Demo1Action{
/**
*如果在配置文件中没有指定方法,struts会默认匹配执行execute方法
*/
public String execute() {
System.out.println("Hello struts");
return "success";
}
}
新建一个用来测试的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>
<a href="${pageContext.request.contextPath}/sy/demo1Action.action">click</a>
</body>
</html>
3、动态调用方法
在action标签的name后拼上 _* , 且为action标签加上method属性
这里没有它规定的execute方法,而是我们自己定义的方法
package com.zking.study.one.demo;
public class Demo1Action{
public String add() {
System.out.println("add");
return "success";
}
public String remove() {
System.out.println("remove");
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>
<a href="${pageContext.request.contextPath}/sy/demo1Action_add.action">click</a>
</body>
</html>
4、参数赋值
1. Action中定义属性,并提供get/set方法
package com.zking.study.one.demo;
public class Demo1Action{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String execute() {
System.out.println(name);
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>
<a href="${pageContext.request.contextPath}/sy/demo1Action.action?name=张三">click</a>
</body>
</html>
2. 实现ModelDriven接口 ,不需要提供get/set方法
package com.zking.study.one.demo;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ModelDriven;
import com.zking.study.one.entity.User;
public class Demo1Action implements ModelDriven<User>{
//定义的一个实体类,定义了一个属性uname
private User user=new User();
public String execute() {
System.out.println(user);
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>
<a href="${pageContext.request.contextPath}/sy/demo1Action.action?uname=张三">click</a>
</body>
</html>
5、与J2EE容器交互
可以参考这篇博客: