1.将struts2过滤器添加到web.xml中
struts2
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
struts2
/*
3创建action(action就是一个POJO类)
3.1、为action编写execute方法
package com.yan.struts.action;
public class HelloAction {
public String execute(){
System.out.println("hello struts");
return "success";
}
}
3.2、在struts.xml文件中配置action和返回结果集
/hello.jsp
默认执行execute方法,但有多个操作显然不够,所以可以进行指定执行哪个方法
/WEB-INF/user/addInput.jsp
/WEB-INF/user/add.jsp
/WEB-INF/user/list.jsp
package com.yan.struts.action;
public class UserAction {
public String addInput(){
System.out.println("addInput");
return "success";
}
public String add(){
System.out.println("add");
return "success";
}
public String list(){
System.out.println("list");
return "success";
}
}
package com.yan.struts.action;
public class UserAction {
public String addInput(){
System.out.println("addInput");
return "success";
}
public String add(){
System.out.println("add");
return "success";
}
public String list(){
System.out.println("list");
return "success";
}
}
/WEB-INF/User/add.jsp
/WEB-INF/User/addInput.jsp
/WEB-INF/User/list.jsp
package com.yan.struts.action;
public class UserAction {
public String addInput(){
System.out.println("addInput");
return "success";
}
public String add(){
System.out.println("add");
return "success";
}
public String list(){
System.out.println("list");
return "success";
}
}
/WEB-INF/{1}/{2}.jsp
package com.yan.struts.action;
public class UserAction {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String addInput(){
System.out.println("addInput");
return "success";
}
public String add(){
System.out.println("add");
return "success";
}
public String list(){
this.setName("张三");
this.setAge(20);
System.out.println("list");
return "success";
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
User list
${name}----${age }
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
Insert title here
User list
----
public String list(){
ActionContext.getContext().put("aaa","张三");
ActionContext.getContext().put("bbb", "123");
System.out.println("list");
return "success";
}
----
public String list(){
ServletActionContext.getRequest().setAttribute("hello", "world");
System.out.println("list");
return "success";
}
package com.yan.struts.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;
import com.yan.struts.model.Department;
import com.yan.struts.model.Role;
import com.yan.struts.model.User;
import ognl.Ognl;
import ognl.OgnlException;
public class TestOgnl {
@Test
public void test01() {
try {
User u=new User(1,"ts","唐三");
//第二个参数为root,意思是输出u的id值
System.out.println(Ognl.getValue("id", u));
Department de=new Department("财务部");
u.setDe(de);
System.out.println(Ognl.getValue("de.name", u));
} catch (OgnlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void test02() {
try {
Map ctx=new HashMap();
User u=new User(1,"ts","唐三");
Department de=new Department("财务部");
Role r=new Role(1,"管理员");
ctx.put("user", u);
ctx.put("role", r);
u.setDe(de);
//在根root即u中找username
System.out.println(Ognl.getValue("username",ctx, u));
//加#号在map即ctx中找所存的user的username
System.out.println(Ognl.getValue("#user.username",ctx, u));
//目前root是ctx所以也可以取到
System.out.println(Ognl.getValue("role.name", ctx,ctx));
//Ognl其实就是一个大的Context(Map),根的key就是root,值为u
System.out.println(Ognl.getValue("#root.username",ctx,u));
} catch (OgnlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void test03() {
try {
Listusers=new ArrayList();
users.add(new User(1,"张三","zs"));
users.add(new User(2,"王五","ww"));
users.add(new User(3,"赵六","zl"));
//root为list要用#root[index]方式访问
System.out.println(Ognl.getValue("#root[0].username",users));
} catch (OgnlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void test04() {
try {
User u=new User();
//可以调用方法
System.out.println(Ognl.getValue("add(1,2)", u));
System.out.println(Ognl.getValue("hello('world')", u));
//在test03还可以
System.out.println(Ognl.getValue("get(1).username",users));
} catch (OgnlException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.yan.struts.model;
public class User {
private int id;
public Department de;
private String username;
private String nickname;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Department getDe() {
return de;
}
public void setDe(Department de) {
this.de = de;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public User(int id, String username, String nickname) {
super();
this.id = id;
this.username = username;
this.nickname = nickname;
}
public User() {
super();
}
public int add(int a,int b){
return a+b;
}
public String hello(String para){
return "hello"+para;
}
}
package com.yan.struts.model;
public class Department {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Department(String name) {
super();
this.name = name;
}
}
package com.yan.struts.model;
public class Role {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Role(int id, String name) {
super();
this.id = id;
this.name = name;
}
}
this.setName("八戒");
this.setAge(123);
User u=new User(10,"孙悟空","swn");
ActionContext.getContext().getValueStack().push(u);
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
Insert title here
User list
成年人
未成年
-------
public String list(){
Listusers=new ArrayList();
users.add(new User(1,"张三","zs"));
users.add(new User(2,"王五","ww"));
users.add(new User(3,"赵六","zl"));
ActionContext.getContext().put("users", users);
return "success";
}
package com.yan.struts.action;
import com.yan.struts.model.Message;
public class MessageAction {
private Message msg;
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
public String addInput(){
return "success";
}
public String add(){
return "success";
}
}
package com.yan.struts.model;
public class Message {
private int id;
private String title;
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
Message addInput
package com.yan.struts.action;
import com.opensymphony.xwork2.ModelDriven;
import com.yan.struts.model.Message;
public class MessageAction implements ModelDriven{
private Message msg;
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
public String addInput(){
return "success";
}
public String add(){
return "success";
}
@Override
public Message getModel() {
if(msg==null)msg=new Message();
return msg;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
Message addInput
package com.yan.struts.action;
import java.util.Date;
import com.opensymphony.xwork2.ModelDriven;
import com.yan.struts.model.Message;
import com.yan.struts.model.Point;
public class MessageAction implements ModelDriven{
private Message msg;
private Point point;
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
public String addInput(){
return "success";
}
public String add(){
return "success";
}
@Override
public Message getModel() {
if(msg==null)msg=new Message();
return msg;
}
}
package com.yan.struts.model;
public class Point {
private String x;
private String y;
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
}
package com.yan.struts.converter;
import java.util.Map;
import org.apache.struts2.util.StrutsTypeConverter;
import com.yan.struts.model.Point;
public class PointConverter extends StrutsTypeConverter {
@Override
public Object convertFromString(Map context, String[] values, Class toClass) {
Point p=null;
if(values.length<=1){
String str=values[0];
String []codes=str.split(",");
p=new Point();
p.setX(codes[0]);
p.setY(codes[1]);
}
return p;
}
@Override
public String convertToString(Map context, Object o) {
Point p=(Point)o;
return p.getX()+","+p.getY();
}
}
com.yan.struts.model.Point=com.yan.struts.converter.PointConverter
point=com.yan.struts.converter.PointConverter
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
文件上传
package com.yan.struts.action;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.yan.struts.model.Message;
import com.yan.struts.model.Point;
public class MessageAction implements ModelDriven{
private Message msg;
private Point point;
private Date endTime;
private File photo;
private String photoFileName;
private String photoContentType;
public String getPhotoFileName() {
return photoFileName;
}
public void setPhotoFileName(String photoFileName) {
this.photoFileName = photoFileName;
}
public String getPhotoContentType() {
return photoContentType;
}
public void setPhotoContentType(String photoContentType) {
this.photoContentType = photoContentType;
}
public File getPhoto() {
return photo;
}
public void setPhoto(File photo) {
this.photo = photo;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public Point getPoint() {
return point;
}
public void setPoint(Point point) {
this.point = point;
}
public Message getMsg() {
return msg;
}
public void setMsg(Message msg) {
this.msg = msg;
}
public String addInput(){
return "success";
}
public String add(){
return "success";
}
@Override
public Message getModel() {
if(msg==null)msg=new Message();
return msg;
}
public String fileInput(){
return "success";
}
public String file(){
try {
System.out.println(photoFileName+"..."+photoContentType);
FileUtils.copyFile(photo, new File("d:/"+photoFileName));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
}
package com.yan.struts.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class HelloInterceptor extends AbstractInterceptor {
@Override
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("hello interception");
return invocation.invoke();
}
}
/WEB-INF/{1}/{2}.jsp
/WEB-INF/{1}/{2}.jsp
/WEB-INF/{1}/{2}.jsp