struts2的hello world
1、导包(自行导包)
2、配置web.xml文件
struts2基于拦截器,配置的第一步是在WEB-INF/web.xml中配置struts2拦截器。
复制代码 struts2 index.html index.htm index.jsp default.html default.htm default.jsp struts2 org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter struts2 /*
3、配置struts.xml文件
复制代码 /hello.jsp /index.jsp
4、写一个HelloAction类
public class HelloAction extends ActionSupport{ public String execute() { System.out.println("hello world"); return "success"; }}复制代码
5、编写一个hello.jsp页面和index.jsp页面
hello.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>struts2 hello world 复制代码
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>struts2 hello struts 复制代码
6、测试
配置好后,就可以通过浏览器访问(根路径取决于项目配置) http://127.0.0.1/strut2/hello.action 如果访问成功,jsp页面也会显示hello struts http://127.0.0.1/strut2/hello.action 如果访问成功,控制台后打印 hello world,jsp页面也会显示hello world
7、补充(注解配置如下)
// 使用注解前必须导入 struts2-convention-plugin 包@ParentPackage("struts-default")public class HelloAction extends ActionSupport{ @Action(value="hello",results= { @Result(name="success",location="/hello.jsp") }) public String execute() { System.out.println("hello world"); return "success"; } }复制代码
struts2常量配置
复制代码
动态方法调用
HelloAction
public class HelloAction extends ActionSupport{ public String execute() { System.out.println("hello world"); return "success"; } public String execute1() { System.out.println("hello world1"); return "success"; } public String execute2() { System.out.println("hello world2"); return "success"; }}复制代码
execute1,execute2,execute3 /hello.jsp 复制代码 /hello.jsp
Action的创建方式
//第一种方式public class HelloAction{}//第二种方式public class Test implements Action{ @Override public String execute() throws Exception { // TODO Auto-generated method stub return null; }}//第三种方式public class HelloAction extends ActionSupport{}复制代码
Action中result处理方式
复制代码 /hello.jsp /hello.jsp hello1 / hello1 /
Action获取servlet api
//获得原生的servlet作用域对象 方式一public class Action1 extends ActionSupport{ public String hello1() { HttpServletRequest request=ServletActionContext.getRequest(); request.setAttribute("key", "value"); HttpSession session=request.getSession(); HttpServletResponse response=ServletActionContext.getResponse(); ServletContext servletContext=ServletActionContext.getServletContext(); return "success1"; }}//获得原生的servlet作用域对象 方式二public class Action1 extends ActionSupport implements ServletRequestAware,ServletResponseAware,ServletContextAware{ private HttpServletRequest request; private HttpServletResponse response; private ServletContext servletContext; @Override public void setServletRequest(HttpServletRequest request) { this.request=request; HttpSession session=request.getSession(); } @Override public void setServletResponse(HttpServletResponse response) { this.response=response; } @Override public void setServletContext(ServletContext servletContext) { this.servletContext=servletContext; }}//获得原生的ActionContext作用域对象(常用) 方式一public class Action1 extends ActionSupport{ public String hello2() { Maprequest=(Map )ActionContext.getContext().get("request"); ActionContext context=ActionContext.getContext(); context.put("key", "value"); Map session=ActionContext.getContext().getSession(); session.put("key", "value"); Map application=ActionContext.getContext().getApplication(); return "success1"; }}//获得原生的ActionContext作用域对象(常用) 方式二public class Action1 extends ActionSupport implements RequestAware, SessionAware, ApplicationAware { private Map request; private Map session; private Map application; @Override public void setRequest(Map request) { this.request = request; } @Override public void setSession(Map session) { this.session = session; } @Override public void setApplication(Map application) { this.application = application; }}复制代码
struts2接收前台传过来的参数的方式
第一种方式:属性加载驱动
index.jsp
复制代码
public class Action1{ private String name; private Integer age; private Date date; //...省略getter/setter方法 public String hello1() { System.out.println(name+"-"+age+"-"+date); return "success1"; } }复制代码
第二种方式:对象加载驱动
index.jsp
复制代码
Student.java
public class Student { private Integer id; private String name; private Integer age; private Date date; //...省略getter/setter方法}复制代码
public class Action1{ private Student student; //...省略getter/setter方法 public String hello1() { System.out.println(student); return "success1"; } }复制代码
第三种方式:ModelDriven模型驱动
index.jsp
复制代码
public class Student { private Integer id; private String name; private Integer age; private Date date; //...省略getter/setter方法}复制代码
public class Action1 implements ModelDriven{ private Student student=new Student(); public String hello1() { System.out.println(student); return "success1"; } @Override public Student getModel() { return student; } }复制代码
接收list集合
复制代码
public class Action1{ private Listlist; //...省略getter/setter方法 public String hello1() { System.out.println(list); return "success1"; }}复制代码
接收map集合
复制代码
public class Action1{ private Mapmap; //...省略getter/setter方法 public String hello1() { System.out.println(map); return "success1"; }}复制代码
值栈(ValueStack)
- 栈:先进后出
- 队列:先进先出
struts拦截器
StudentAction.java
public class StudentAction { public String getAdd() { System.out.println("add"); return "success"; } public String getUpdate() { System.out.println("update"); return "success"; } public String getDel() { System.out.println("del"); return "success"; } public String getSelect() { System.out.println("select"); return "success"; }}复制代码
MyInterceptor3.java
//自定义拦截器public class MyInterceptor3 extends MethodFilterInterceptor{ private static final long serialVersionUID = 1L; @Override protected String doIntercept(ActionInvocation invocation) throws Exception { //前处理 System.out.println("before"); invocation.invoke(); //后处理 System.out.println("after"); //返回结果集,不执行后面的拦截器,直接给result处理结果集,跳转页面 return null; }}复制代码
复制代码 getDel,getSelect getAdd,getUpdate,getDel,getSelect
struts2的执行流程
- 用户请求前端控制器(StrutspreapredAndExecuteFilter),它是一个 Filter,拦截用户的请求,根据用户请求的url调用Action
- 前端控制器(StrutsPrepareAndExecuteFilter)判定该请求是否是一个Struts2 请求(ActionMapper)
- 若该请求是一个Struts2请求,则前端控制器StrutsPrepareAndExecuteFilter 把请求的处理交给ActionProxy
- ActionProxy创建一个ActionInvocation的实例,并进行初始化
- ActionInvocation实例在调用 Action 的过程前后,涉及到相关拦截 器(Intercepter)的调用。
- Action 执行完毕,ActionInvocation 负责根据struts.xml中的配置 找到对应的result,渲染结果。
- 执行各个拦截器 invocation.invoke() 之后的代码
- 把结果发送到客户端