1、在Struts2中有三种方式可以创建action处理类
(1) 通过实现Action接口来创建action的处理类(好处:使得我们编写的代码更加规范)
①新建Struts2项目,手动导入jar包(放到WEB-INF/lib目录下),配置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="hello" class="cn.ht.action.HelloAction"> <result>/index.jsp</result> </action> </package> </struts>
②配置web.xml(IDEA自动生成)
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <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> </web-app>
③index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Action</title>
</head>
<body>
action execute
</body>
</html>
④ 在src下创建HelloAction.java
package cn.ht.action; import com.opensymphony.xwork2.Action; // 通过实现Action接口来创建处理类 // 好处: 实现Action接口的方式可以直接使用action提供的常量 // 必须重写默认处理方法 // 这种方法用的较少 public class HelloAction implements Action { @Override public String execute() throws Exception { System.out.println("action的处理类被执行"); return SUCCESS; } }
⑤访问
(2)通过继承ActionSupport类来创建Action的处理类,struts2推荐使用这种方式(好处:可以继承一些 ActionSuport 实现功能,如:验证;官方推荐使用)
Hello1Action.java
package cn.ht.action; import com.opensymphony.xwork2.ActionSupport; /** * ActionSupport实现了Action接口 * 并且ActionSupport类提供了很多其他struts2提供的功能 * 比如:数据校验、国际化 * 创建后就有默认实现 */ public class Hello1Action extends ActionSupport { }
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="hello" class="cn.ht.action.HelloAction"> <result>/index.jsp</result> </action> <action name="hello1" class="cn.ht.action.Hello1Action"> <result>/HelloAction.jsp</result> </action> </package> </struts>
HelloAction.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>HelloAction</title> </head> <body> <h1>HelloAction</h1> </body> </html>
访问
(3)无侵入性的实现 (好处:自定义一个普通的 java 类即可,不具有侵入型)
Hello2Action.java
package cn.ht.action; /** * 无侵入性的实现 */ public class Hello2Action { public String execute(){ System.out.println("无侵入性的实现"); return "success"; } }
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="hello" class="cn.ht.action.HelloAction"> <result>/index.jsp</result> </action> <action name="hello1" class="cn.ht.action.Hello1Action"> <result>/HelloAction.jsp</result> </action> <action name="hello2" class="cn.ht.action.Hello2Action"> <result>/Hello2Action.jsp</result> </action> </package> </struts>
Hello2Action.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019-8-13 Time: 16:14 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Hello2Action</title> </head> <body> <h1>无侵入性的实现</h1> </body> </html>
访问