概述

Struts2是一个基于MVC设计模式web应用框架,在webwork框架技术基础上,Strurts2核心是拦截器,Struts2框架的核心功能都依靠拦截器实现的。Struts2框架对控制器进行了统一的和规范。

创建 一个Java web应用

增加 jetty plugin

  1. <groupId>cn.java.struts</groupId>
  2. <artifactId>BasicStruts</artifactId>
  3. <version>1.0-SNAPSHOT</version>
  4. <packaging>war</packaging>
  5. <properties>
  6. <struts2.version>2.5.14.1</struts2.version>
  7. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  8. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  9. <maven.compiler.source>1.8</maven.compiler.source>
  10. <maven.compiler.target>1.8</maven.compiler.target>
  11. </properties>
  12. ...
  13. <build>
  14. ...
  15. <finalName>basic-struts</finalName>
  16. <plugins>
  17. <plugin>
  18. <groupId>org.eclipse.jetty</groupId>
  19. <artifactId>jetty-maven-plugin</artifactId>
  20. <version>9.4.7.v20170914</version>
  21. <configuration>
  22. <webApp>
  23. <contextPath>/${build.finalName}</contextPath>
  24. </webApp>
  25. <stopKey>CTRL+C</stopKey>
  26. <stopPort>8999</stopPort>
  27. <scanIntervalSeconds>10</scanIntervalSeconds>
  28. <scanTargets>
  29. <scanTarget>src/main/webapp/WEB-INF/web.xml</scanTarget>
  30. </scanTargets>
  31. </configuration>
  32. </plugin>
  33. </plugins>
  34. </build>

在src/main/下创建webapp目录,在webapp下创建WEB-INF目录,在WEB-INF目录创建web.xml

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. </web-app>

在src/main/webapp新建index.jsp

  1. <!DOCTYPE html>
  2. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
  3. <html>
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Basic Struts 2 Application - Welcome</title>
  7. </head>
  8. <body>
  9. <h1>Welcome To Struts 2!</h1>
  10. </body>
  11. </html>

增加 Struts 2 jar包

  1. <dependency>
  2. <groupId>org.apache.struts</groupId>
  3. <artifactId>struts2-core</artifactId>
  4. <version>${struts2.version}</version>
  5. </dependency>

在web.xml创建 Servlet Filter

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app id="WebApp_ID" version="2.4"
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  6. <display-name>Basic Struts2</display-name>
  7. <welcome-file-list>
  8. <welcome-file>index</welcome-file>
  9. </welcome-file-list>
  10. <filter>
  11. <filter-name>struts2</filter-name>
  12. <!--如果Struts2 的2.3.24版本应该是这样的
  13. <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  14. -->
  15. <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  16. </filter>
  17. <filter-mapping>
  18. <filter-name>struts2</filter-name>
  19. <url-pattern>/*</url-pattern>
  20. </filter-mapping>
  21. </web-app>

创建HelloAction

  1. import com.opensymphony.xwork2.ActionSupport;
  2. public class HelloAction extends ActionSupport {
  3. @Override
  4. public String execute() throws Exception {
  5. System.out.println("执行action");
  6. return SUCCESS;
  7. }
  8. }

创建struts.xml
在src/main/resources文件夹下创建 struts.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  4. "http://struts.apache.org/dtds/struts-2.5.dtd">
  5. <struts>
  6. <package name="default" extends="struts-default">
  7. <action name="hello" class="cn.java.struts.HelloAction">
  8. <result name="success">/success.jsp</result>
  9. </action>
  10. </package>
  11. </struts>

mvn直接运行jetty:

  1. mvn jetty:run

打开浏览器输入http://localhost:8080/basic-struts/hello.action 注意是URL末尾是hello.action不是 hello.jsp
Spring——Struts2 - 图1

Struts动态方法调用

动态方法调用就是为了解决一个Anction对应多个请求的处理,以免Action太多。
1、xml中配置 method 属性,对应 指定方法

  1. public class HelloAction extends ActionSupport {
  2. public String add(){
  3. System.out.println("执行add");
  4. return SUCCESS;
  5. }
  6. public String update(){
  7. System.out.println("执行update");
  8. return SUCCESS;
  9. }
  10. @Override
  11. public String execute() throws Exception {
  12. System.out.println("执行action");
  13. return SUCCESS;
  14. }
  15. }

修改struts.xml

  1. <struts>
  2. <package name="default" extends="struts-default">
  3. <action name="hello" class="cn.java.struts.HelloAction">
  4. <result >/success.jsp</result>
  5. </action>
  6. <action name="add" method="add" class="cn.java.struts.HelloAction">
  7. <result >/add.jsp</result>
  8. </action>
  9. <action name="update" method="update" class="cn.java.struts.HelloAction">
  10. <result >/update.jsp</result>
  11. </action>
  12. </package>
  13. </struts>

2、感叹号方式

  1. public class HelloAction extends ActionSupport {
  2. public String add(){
  3. System.out.println("执行add");
  4. return "add";
  5. }
  6. public String update(){
  7. System.out.println("执行update");
  8. return "update";
  9. }
  10. @Override
  11. public String execute() throws Exception {
  12. System.out.println("执行action");
  13. return SUCCESS;
  14. }
  15. }
  1. <struts>
  2. <package name="default" extends="struts-default">
  3. <global-allowed-methods>regex:.*</global-allowed-methods>
  4. <action name="hello" class="cn.java.struts.HelloAction">
  5. <result >/success.jsp</result>
  6. <result name="add">/add.jsp</result>
  7. <result name ="update">/update.jsp</result>
  8. </action>
  9. </package>
  10. <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
  11. </struts>

在struts2.5 为了提升安全性,要在package标签后加上regex:.*
Spring——Struts2 - 图2

3、通配符方式

  1. <struts>
  2. <package name="default" extends="struts-default">
  3. <global-allowed-methods>regex:.*</global-allowed-methods>
  4. <action name="hello_*" method="{1}" class="cn.java.struts.HelloAction">
  5. <result >/success.jsp</result>
  6. <result name="add">/{1}.jsp</result>
  7. <result name ="update">/{1}.jsp</result>
  8. </action>
  9. </package>
  10. <constant name="struts.enable.DynamicMethodInvocation" value="false"></constant>
  11. </struts>

Spring——Struts2 - 图3

默认Action页面

  1. <default-action-ref name="error"></default-action-ref>
  2. <action name="error" method="error" class="cn.java.struts.HelloAction">
  3. <result name="error">/error.jsp</result>
  4. </action>
  1. public class HelloAction extends ActionSupport {
  2. public String error(){
  3. System.out.println("执行error");
  4. return ERROR;
  5. }
  6. }

Spring——Struts2 - 图4

Struts2接收参数

使用Action属性接收
  1. public class LoginAction extends ActionSupport {
  2. private String name;
  3. private String password;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public String getPassword() {
  11. return password;
  12. }
  13. public void setPassword(String password) {
  14. this.password = password;
  15. }
  16. public String login() throws Exception {
  17. if ("admin".equals(name)&&"123456".equals(password)){
  18. return SUCCESS;
  19. }
  20. return ERROR;
  21. }
  22. }

struts.xml增加login action

  1. <action name="login" method="login" class="cn.java.struts.LoginAction">
  2. <result name="success">/success.jsp</result>
  3. <result name="error">/error.jsp</result>
  4. </action>

Spring——Struts2 - 图5

通过model

创建login.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>User Login</title>
  5. </head>
  6. <body>
  7. <center>
  8. <h1>用户登录</h1>
  9. <form action="login.action" method="post">
  10. 用 户:<input type="text" name="name"><br>
  11. 密 码:<input type="password" name="password"><br>
  12. <input type="submit" value="commit">
  13. </form>
  14. </center>
  15. </body>
  16. </html>
  1. public class LoginAction extends ActionSupport {
  2. private User user;
  3. public User getUser() {
  4. return user;
  5. }
  6. public void setUser(User user) {
  7. this.user = user;
  8. }
  9. public String login() throws Exception {
  10. if ("admin".equals(user.getName())&&"123456".equals(user.getPassword())){
  11. return SUCCESS;
  12. }
  13. return ERROR;
  14. }
  15. }

User.java

  1. public class User {
  2. private String name;
  3. private String password;
  4. public String getName() {
  5. return name;
  6. }
  7. public void setName(String name) {
  8. this.name = name;
  9. }
  10. public String getPassword() {
  11. return password;
  12. }
  13. public void setPassword(String password) {
  14. this.password = password;
  15. }
  16. }

修改login.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>User Login</title>
  5. </head>
  6. <body>
  7. <center>
  8. <h1>用户登录</h1>
  9. <form action="login.action" method="post">
  10. 用 户:<input type="text" name="user.name"><br>
  11. 密 码:<input type="password" name="user.password"><br>
  12. <input type="submit" value="commit">
  13. </form>
  14. </center>
  15. </body>
  16. </html>

ModelDriven

  1. public class LoginAction extends ActionSupport implements ModelDriven<User> {
  2. private User user = new User(); //必须实例化
  3. public String login() throws Exception {
  4. System.out.println(user.getName());
  5. System.out.println(user.getPassword());
  6. if ("admin".equals(user.getName())&&"123456".equals(user.getPassword())){
  7. return SUCCESS;
  8. }
  9. return ERROR;
  10. }
  11. @Override
  12. public User getModel() {
  13. return user;
  14. }
  15. }

取得user

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>User Login</title>
  5. </head>
  6. <body>
  7. <center>
  8. <h1>用户登录</h1>
  9. <form action="login.action" method="post">
  10. 用 户:<input type="text" name="name"><br>
  11. 密 码:<input type="password" name="password"><br>
  12. <input type="submit" value="commit">
  13. </form>
  14. </center>
  15. </body>
  16. </html>

Action中五种内置属性

(1) SUCCESS Action正确的执行完成,返回相应的视图,success是name属性的默认值。
(2) NONE 表示Action正确的执行完成,但并不返回任何事视图。
(3) ERROR 表示Action执行失效,返回错误处理视图。
(4) LOGIN Action因为用户没有登录的原因没有正确执行,将返回该登录视图,要求用户进行登录验证
(5) INPUT Action的执行,需要从前端界面获取参数,INPUT就是代表这个参数输入界面,一般在应用中,会对这些 参数进行验证,如果验证没有通过,将自动返回该视图。

  1. public class LoginAction extends ActionSupport implements ModelDriven<User> {
  2. private User user = new User(); //必须实例化
  3. public String login() throws Exception {
  4. if(user.getName() ==null
  5. ||"".equals(user.getName().trim())){
  6. this.addFieldError("username","用户名为空");
  7. return INPUT;
  8. }
  9. if ("admin".equals(user.getName())&&"123456".equals(user.getPassword())){
  10. return SUCCESS;
  11. }
  12. return ERROR;
  13. }
  14. @Override
  15. public User getModel() {
  16. return user;
  17. }
  18. }

增加input结果处理

  1. <action name="login" method="login" class="cn.java.struts.LoginAction">
  2. <result name="success">/success.jsp</result>
  3. <result name="error">/error.jsp</result>
  4. <result name="input">/login.jsp</result>
  5. </action>