1.运行环境搭建
导入相应jar包
(1)在web工程的src下创建struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
(2)在web.xml中配置struts过滤器
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"
version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
<!-- <init-param>
<param-name>config</param-name>
<param-value>struts.xml</param-value>
</init-param> -->
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(3)创建测试类
public class HelloAction {
public String execute() {
System.out.println("hello");
return "hello";
}
public String addHello() {
System.out.println("add hello");
return "success";
}
}
(4)配置测试类
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="java1702" extends="struts-default">
<action name="hello" class="net.study.HelloAction">
<result name="hello">/hello.jsp</result>
</action>
<action name="addHello" class="net.study.HelloAction" method="addHello">
<result>/hello.jsp</result>
</action>
</package>
</struts>
(5)创建result返回的结果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>
hello world
</body>
</html>
2.参数传递
(1)传递基本数据类型
通过设置参数及其set,get方法获取和传递
从地址栏获取http://localhost:8080/strutsstudy/add?name=11
返回到jsp页面<body>hello world<br/>name=${name}</body>
@Actions({
@Action(value="/add",results={
@Result(name="success",location="/hello.jsp")
})
})
public String addHello() {
System.out.println("add hello:" + name);
return "success";
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
(2)传递对象(多个对象)
@ParentPackage("javastruts")
@Namespace("hello")
public class HelloAction extends ActionSupport{
//传递单个对象时可通过实现ModelDriven,重新getModel
//implements ModelDriven<Person>
private static final long serialVersionUID = 1L;
public String execute() {
System.out.println("hello");
return "hello";
}
@Actions({
@Action(value="/add",results={
@Result(name="success",location="/hello.jsp")
})
})
public String addHello() {
System.out.println("peoson:" + person.getAge() + person.getName() +
user.getAddress());
return "success";
}
/*@Override
public Person getModel() {
// TODO Auto-generated method stub
return person;
}*/
//参数传递 作用域为request
private Person person;
private User user;
public Person getPerson() {
return person;
}
public User getUser() {
return user;
}
public void setPerson(Person person) {
this.person = person;
}
public void setUser(User user) {
this.user = user;
}
}
注:struts继承struts-default
struts-default.xml中的参数拦截器
<interceptor name="params"
class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/>
获取到的参数会根据所需的数据类型进行转换
(3)实体类
public class Person implements Serializable{
private static final long serialVersionUID = 1501197896931045497L;
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;
}
}
3.作用域request,session,application
//获取 作用域对象 默认request
//第一种 推荐使用
//获取上下文
ActionContext context = ActionContext.getContext();
//获取application
Map<String, Object> appliaction = context.getApplication();
appliaction.put("appli", 100);
//获取session
Map<String, Object> session = context.getSession();
session.put("session", 10);
context.put("request", 1);
注:
//值栈,存放获取到的数据,根据相应的范围返回数据
ValueStack vStack = context.getValueStack();
//可以直接设置 范围是request
//vStack.setValue(arg0, arg1);修改
vStack.set("test", 2222);*/
//第二种 原生的servlet对象 (除非要用到原生对象,否则不建议使用)
ServletContext application = ServletActionContext.getServletContext();
application.setAttribute("appli", 100);
HttpSession session = ServletActionContext.getRequest().getSession();
session.setAttribute("session", 10);
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("request", 1);*/
//第三种 不建议使用
//通过实现ServletRequestAware接口,重写setServletRequest方法,
注入request对象
public class HelloAction extends ActionSupport implements
ServletRequestAware{...}
private HttpServletRequest request;
@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
后续会继续更新一些知识点