什么是Struts2
Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数据交互。Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。
环境
idea
tomcat
jdk
maven
步骤
pom.xml 配置 Struts2
点击前往获取自己想要的版本对应的XML
1 2 3 4 5 6 7 8 9
| <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16.3</version> </dependency>
</dependencies>
|
创建 User 类 用于展现提取对象的数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package com.entity;
public class User { private String username; private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; } }
|
创建 LoginAction 类 代替以往的 servlet 层
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| package com.action;
import com.entity.User; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
public class LoginAction extends ActionSupport implements Action { private String username; private String password;
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getPassword() { return password; }
public void setPassword(String password) { this.password = password; }
public String execute() throws Exception { return null; }
@Override public void validate() { if(username.length()==0 || password.length()==0){ addFieldError("username","用户名不能为空"); addFieldError("password","密码不能为空"); } }
public String login() { User user = new User(); user.setUsername("simon"); user.setPassword("simon");
ActionContext context = ActionContext.getContext();
Map<String, Object> request = (Map<String, Object>) context.get("request");
Map<String, Object> session = context.getSession();
Map<String, Object> application = context.getApplication();
if (user.getUsername().equals(getUsername()) && user.getPassword().equals(getPassword())) {
session.put("user", user);
return "success"; }
request.put("errorinfo", "登录失败"); return ERROR; } }
|
创建页面 index.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Title</title> </head> <body> <%--从request里面找errorinfo的数据--%> <h1>${requestScope.errorinfo}</h1> <%--显示全部 fielderror 错误信息--%> <h1><s:fielderror/></h1> <%--显示某个 fielderror 错误信息--%> <h1><s:fielderror fieldName="password"/></h1> <hr/> <%--Login 为 struts.xml 定义 action name 的名称 .action 为 Struts 标识--%> <form action="Login.action" method="post"> <%--name属性要和LoginAction对象的属性一致--%> 用户名: <input type="text" name="username"/><br/> 密 码: <input type="password" name="password"/><br/> <input type="submit" value="登录"/> </form> </body> </html>
|
创建页面 success.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Title</title> </head> <body> <h1>登录成功</h1> <h2>欢迎您,<s:property value="username"/></h2> <h1>session对象的User对象数据</h1> <h2>用户名: ${sessionScope.user.username}</h2> <h2>密 码: ${sessionScope.user.password}</h2> </body> </html>
|
配置 web.xml 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
|
配置 struts.xml 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="default" extends="struts-default"> <action name="Login" class="com.action.LoginAction" method="login"> <result name="success">success.jsp</result> <result name="error">index.jsp</result> <result name="input">index.jsp</result> </action> </package> </struts>
|
参考网址