Java学习路线-62:Struts2

举报
彭世瑜 发表于 2021/08/13 23:22:15 2021/08/13
【摘要】 一、Struts2 概述 Struts 翻译:支柱 Struts 是流行和成熟的基于 MVC 设计模式的 Web 应用程序框架 POJO(Plain Ordinary Java Object)简单的 Java 对象 MVC 模式 模型视图控制器 Model View Controller 工作原理图 环境 二、Struts2 项目 项目结构 ....

一、Struts2 概述

Struts 翻译:支柱

Struts 是流行和成熟的基于 MVC 设计模式的 Web 应用程序框架

POJO(Plain Ordinary Java Object)简单的 Java 对象

MVC 模式

模型视图控制器

Model View Controller

工作原理图

在这里插入图片描述

环境

二、Struts2 项目

项目结构

.
├── pom.xml
└── src ├── main │   ├── java │   │   └── com │   │ └── mouday │   │ └── HelloWorldAction.java │   ├── resources │   │   └── struts.xml │   └── webapp │ ├── WEB-INF │ │   └── web.xml │ ├── hello.jsp │ └── index.html └── test └── java


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

1、配置文件

(1)pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>java-demo-struts</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <!-- tomcat7插件 maven 命令 tomcat7:run 启动项目--> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.2</version> <configuration> <port>8080</port> <path>/</path> <uriEncoding>UTF-8</uriEncoding> <!--添加忽略war包检查标签,则可以让tomcat7:run指令正常启动tomcat--> <ignorePackaging>true</ignorePackaging> <contextFile>src/main/webapp/WEB-INF/web.xml</contextFile> <contextReloadable>true</contextReloadable> </configuration> </plugin> </plugins> </build> <!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core --> <dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.22</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> </dependencies>

</project>

  
 
  • 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

(2)src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="utf-8" ?>

<web-app> <display-name>Hello</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
</web-app>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

(3)src/main/resources/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">
<struts> <package name="default" namespace="/" extends="struts-default"> <action name="HelloWorldAction" class="com.mouday.HelloWorldAction"> <result name="">/hello.jsp</result> </action> </package>
</struts>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2、Action

src/main/java/com/mouday/HelloWorldAction.java

package com.mouday;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println("hello action"); return SUCCESS; }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

3、视图文件

src/main/webapp/index.html

<!DOCTYPE html>
<html lang="en">
  <body> hi
  </body>
</html>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

src/main/webapp/hello.jsp

<!DOCTYPE html>
<html lang="en">

<body>
hello action jsp
</body>
</html>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

4、访问测试

GET http://localhost:8080/

hi


GET http://localhost:8080/HelloWorldAction

hello action jsp

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

三、核心文件

web.xml
struts.xml
struts.properties 全局属性文件,自动加载

  
 
  • 1
  • 2
  • 3

struts.properties 常用配置

# struts.xml改动后重新加载重载 默认 false
struts.configuration.xml.reload=true

# 打开开发模式 默认 false
struts.devMode=true

# 设置浏览器缓存静态页面 默认true
struts.serve.static.browserCache=false

# 被struts处理的请求后缀
struts.action.extension=action,do

# 服务器运行时端口号
struts.url.http.port=8080

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

四、深入 Struts2

1、访问 Servlet API 方式

  1. ActionContext
  2. 实现 Aware 接口
  3. ServletActionContext

2、Action 搜索顺序
逐级向上查找

3、动态方法调用
(1)指定 method 属性

package com.mouday;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport { @Override public String execute() throws Exception { System.out.println("hello action"); return SUCCESS; } public String add(){ System.out.println("add"); return SUCCESS; }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

struts.xml(简化)

<struts> <package name="default" namespace="/" extends="struts-default"> <action name="add" class="com.mouday.HelloWorldAction" method="add"> <result name="">/add.jsp</result> </action> </package>
</struts>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

访问结果

GET http://localhost:8080/add.action

add result

  
 
  • 1
  • 2
  • 3

(2)感叹号方式(不推荐)

参考
struts2.5 使用感叹号和通配符实现动态方法调用无效的问题及解决!

配置文件

<?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="default" namespace="/" extends="struts-default"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="HelloWorldAction" class="com.mouday.HelloWorldAction"> <result>/hello.jsp</result> <result name="add">/add.jsp</result> </action> </package> <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
</struts>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
GET http://localhost:8080/HelloWorldAction!add.action

add result

  
 
  • 1
  • 2
  • 3

(3) 通配符方式

<?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="default" namespace="/" extends="struts-default"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="*_*" method="{2}" class="com.mouday.{1}Action"> <result name="{2}">/{2}.jsp</result> </action> </package> <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
</struts>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
GET http://localhost:8080/HelloWorldAction_add.action

add result

  
 
  • 1
  • 2
  • 3

4、指定多个配置文件

<include file="helloworld.xml" />

  
 
  • 1

配置示例

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">

<struts> <include file="hello.xml"/> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <!-- 以防乱码--> <constant name="struts.i18n.encoding" value="UTF-8"/>
</struts>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

hello.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">

<struts> <package name="default" namespace="/" extends="struts-default"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="*_*" method="{2}" class="com.mouday.{1}Action"> <result name="{2}">/{2}.jsp</result> </action> </package>

</struts>


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

访问

GET http://localhost:8080/HelloWorld_add.action

add result

  
 
  • 1
  • 2
  • 3

5、默认 Action

<?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="default" namespace="/" extends="struts-default"> <default-action-ref name="index" /> <action name="index"> <result>/error.jsp</result> </action> </package>
</struts>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

访问

GET http://localhost:8080/xxxx.action

error jsp

  
 
  • 1
  • 2
  • 3

6、Struts2 后缀
(1)struts.xml

<constant name="struts.action.extension" value="html" />

  
 
  • 1

(2)struts.properties

struts.action.extension=action,do

  
 
  • 1

(3)web.xml

 <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>struts.action.extension</param-name> <param-value>do</param-value> </init-param>
</filter>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

7、接收参数

(1)Action 属性

Action 类

package com.mouday;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Override public String execute() throws Exception { System.out.println(username); return SUCCESS; }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

路由配置

<action name="login" class="com.mouday.LoginAction"> <result>/login.html</result>
</action>

  
 
  • 1
  • 2
  • 3

表单页面

<form action="login.action" method="post">
  <input type="text" name="username" />
  <input type="submit" />
</form>

  
 
  • 1
  • 2
  • 3
  • 4

(2)Domain Model

新建 User 类

package com.mouday.bean;

public class User { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Action

package com.mouday.action;

import com.mouday.bean.User;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport { private User user; public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public String execute() throws Exception { System.out.println(user.getUsername()); return SUCCESS; }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

登录表单

<form action="login.action" method="post">
  <input type="text" name="user.username" />
  <input type="submit" />
</form>

  
 
  • 1
  • 2
  • 3
  • 4

(3)ModelDriven

Action

package com.mouday.action;

import com.mouday.bean.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); @Override public String execute() throws Exception { System.out.println(user.getUsername()); return SUCCESS; } public User getModel() { return user; }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

表单

<form action="login.action" method="post">
  <input type="text" name="username" />
  <input type="submit" />
</form>

  
 
  • 1
  • 2
  • 3
  • 4

(4)接收列表类型

User 类

package com.mouday.bean;

import java.util.List;

public class User { private String username; private List<String> list; public List<String> getList() { return list; } public void setList(List<String> list) { this.list = list; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; }
}


  
 
  • 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

Action

package com.mouday.action;

import com.mouday.bean.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); @Override public String execute() throws Exception { System.out.println(user.getUsername()); System.out.println(user.getList()); return SUCCESS; } public User getModel() { return user; }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

表单

<form action="login.action" method="post">
  <input type="text" name="username" />
  <input type="text" name="list[0]" />
  <input type="text" name="list[1]" />
  <input type="submit" />
</form>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

8、处理结果类型
Struts2 处理流程

用户请求
-> Stuts框架
-> 控制器Action
-> Stuts框架
-> 视图资源

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

result 如果省略 name,则默认是 success

<result name="success">/success.jsp</result>

<!-- 等价于 -->
<result>/success.jsp</result>

  
 
  • 1
  • 2
  • 3
  • 4

处理结果类型:

  1. SUCCESS 正确执行,默认视图
  2. NONE 正确执行,不返回视图
  3. ERROR 执行失败
  4. LOGIN 登录视图
  5. INPUT 参数输入界面

Action

package com.mouday.action;

import com.mouday.bean.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class LoginAction extends ActionSupport implements ModelDriven<User> { private User user = new User(); @Override public String execute() throws Exception { System.out.println(user.getUsername()); System.out.println(user.getList()); return SUCCESS; } @Override public void validate() { if (user.getUsername() == null || "".equals(user.getUsername()) ) { addFieldError("username", "用户名不能为空"); } } public User getModel() { return user; }
}


  
 
  • 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

配置

<?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="default" namespace="/" extends="struts-default"> <action name="login" class="com.mouday.action.LoginAction"> <result>/login.jsp</result> <result name="input">/login.jsp</result> </action> </package>

</struts>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

登录框

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@  taglib  prefix="s"  uri="/struts-tags"  %>

<form action="login.action" method="post"> <input type="text" name="username"/> <!-- 显示错误提示 --> <s:fielderror name="username"></s:fielderror> <input type="text" name="list[0]"/> <input type="text" name="list[1]"/> <input type="submit" />
</form>


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

9、局部结果和全局结果
OGNL

<result name=""> <param name="location">resource</param>
</result>

  
 
  • 1
  • 2
  • 3

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/106909752

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。