Struts actionsupport class: Business Controller
•
Java
import com. opensymphony. xwork2. ActionSupport; // Inherit actionsupport base class public class XXX extends actionsupport {/ / omit some attribute logic codes... / / action default execution method public string execute() throws exception {return success;}
This is the syntax structure of an action class that inherits actionsupport class.
Example
import com.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
public static final String MES = "Hello world!"; //默认的执行方法
public String execute()throws Exception{
setMes(MES);
return SUCCESS;
} //唯一属性,将被赋值和打印
private String mes;
public String getMes(){
return mes;
}
public void setMes(String mes){
this.mes = mes;
}
}
<body> <s:property value="mes"/> </body>
Typical application
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--Struts2过滤器-->
<filter>
<!--过滤器名称-->
<filter-name>struts2</filter-name>
<!--过滤器类-->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilt
er</filter-class>
</filter>
<!--Struts2过滤器映射-->
<filter-mapping>
<!--过滤器名称-->
<filter-name>struts2</filter-name>
<!--过滤器映射-->
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
package com.lyq.action;
import com.opensymphony.xwork2.ActionSupport;/**
*用户Action
*@author zx
*/
public class UserAction extends ActionSupport{
private static final long serialVersionUID = 1L; //提示信息
private String info; //添加用户信息
public String add()throws Exception{
info = "添加用户信息";
return"add";
} //更新用户信息
public String update()throws Exception{
info = "更新用户信息";
return"update";
}
public String getInfo(){
return info;
}
public void setInfo(String info){
this.info=info;
}
}
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!--声明包-->
<package name="myPackage"extends="struts-default">
<!--定义action-->
<action name="userAction"class="com.lyq.action.UserAction">
<!--添加成功的映射页面-->
<result name="add">user_add.jsp</result>
<!--更新成功的映射页面-->
<result name="update">user_update.jsp</result>
</action>
</package>
</struts>
<body>
<font color="red">
<s:property value="info"/>
</font>
</body>
<body>
<font color="red">
<s:property value="info"/>
</font>
</body>
<body> <a href="https://www.jb51.ccuserAction!add">添加用户</a> <br> <a href="https://www.jb51.ccuserAction!update">更新用户</a> </body>
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
