struts2是通过过滤器启动的,以下是web.xml
这是项目文件目录struts2 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter actionPackages * struts2 /* index.jsp
struts2的action
LoginAction.java
package user.action;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.Result;import user.entity.User;import com.opensymphony.xwork2.ActionSupport;@Namespace("/user")//命名空间public class LoginAction extends ActionSupport{ private static final long serialVersionUID = -6962103412798846062L; private User user1; private User user2; @Action(value="login-index",results={@Result(name=SUCCESS,location="login-index.jsp")}) public String loginIndex(){ System.out.println("login-index"); return SUCCESS; } @Action(value="login",results={@Result(name=SUCCESS,location="login-success.jsp")}) public String login(){ System.out.println("login"); return SUCCESS; } public User getUser1() { return user1; } public void setUser1(User user1) { this.user1 = user1; } public User getUser2() { return user2; } public void setUser2(User user2) { this.user2 = user2; } }User.java
package user.entity;public class User { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
login-index.jsp
<%@ page language="java" pageEncoding="utf-8"%>login
login-success.jsp
<%@ page language="java" pageEncoding="utf-8"%>success user1: ${user1.username } user2: ${user2.username }
使用tomcat6部署应用,并访问路径http://localhost/项目名称/user/login-index
服务端执行@Action(value="login-index"注解的方法loginIndex(),并返回login-index.jsp
输入表单数据,并提交
表单使用get方式提交,所以这时候浏览器地址栏的路径变为
http://localhost/项目名称/user/login?user1.username=13&user2.username=123
服务端执行@Action(value="login注解的方法login(),并返回login-success.jsp
就这样,一个简单的struts2流程就实现了