struts2中Action到底是什么,怎么理解
问题描述
struts2中Action到底是什么,怎么理解
1.配置完web.xml
2.创建视图页面login.jsp
3.创建业务控制器LoginAction类
(解释说:创建业务控制器LoginAction类,该类为程序的Action类)
4.配置LoginAction类
(解释说:当Action处理完客户端请求后返回一个字符串,没个字符串对应一个视图)
那么这个Action到底是什么,是一个类吗?怎么理解它,它和用于标签的有什么区别。
解决方案
首先,你了解Servlet么?Servlet是运行在java服务器端的java代码,这个Action本质就是Servlet啊。它作为java web应用的控制层,它完成浏览器的请求后返回一个页面给客户端浏览器。
其次,action的处理流程是由strut2的中央控制器完成的,Action处理完客户端请求后返回一个字符串,每个字符串对应一个视图,也就是说它处理浏览器的请求的过程,返回的视图本质就是jsp页面。
结论就是:action的本质就是Servlet。
Struts2如何在Action与JSP之间传值
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion> <packaging>war</packaging>
<name>ActionValueOf</name> <groupId>com</groupId> <artifactId>ActionValueOf</artifactId> <version>1.0-SNAPSHOT</version>
<dependencies> <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.16.3</version> </dependency> </dependencies>
</project>
|
User 用户实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package 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; } }
|
InfoAction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package action;
import com.opensymphony.xwork2.ActionSupport; import entity.User;
public class InfoAction extends ActionSupport { private User user = new User();
public User getUser() { return user; }
public void setUser(User user) { this.user = user; } }
|
index.jsp
index.jsp
只用于打开页面时先进入 action
1 2 3 4 5 6 7 8 9 10
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <%--meta标签在 index.jsp 打开后跳转到指定的 action--%> <meta http-equiv="refresh" content="0; url=info.action"/> </body> </html>
|
info.jsp
info.jsp
用一个页面来实现与 action
的相互传值,其中用 OGNL
来提取 action
实体类的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Title</title> <script type="text/javascript">
</script> </head> <body> <form method="post" action="info.action"> 用户名 : <input type="text" name="user.username" value="<s:property value="user.username"/>"/> 密 码 : <input type="password" name="user.password" value="<s:property value="user.password"/>"/> <input type="submit" value="提交"/> </form> <a href="info.action?user.username=Simon&user.password=password">利用script get方式进入action</a> <h1>用户名 : <s:property value="user.username"/></h1> <h1>密 码 : <s:property value="user.password"/></h1> </body> </html>
|
struts.xml 配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| <?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="info" class="action.InfoAction" method="execute"> <result>info.jsp</result> </action> </package> </struts>
|
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>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
|