在jsp页面中直接调用Action
①先编写action类:ActionTagAction.java
package action;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class ActionTagAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String doDefault() throws Exception {
// TODO Auto-generated method stub
ServletActionContext.getRequest().setAttribute("stringByAction", "这是执行actiondefault方法时的输出");
return "done";
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
ServletActionContext.getRequest().setAttribute("stringByAction", "t方法时的输出");
return "done";
}
}
<action name="actionTagAction1" class="action.ActionTagAction">
<result name="done">/success.jsp</result>
</action>
③编写success.jsp页面
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
执行成功的结果页面内容
</body>
</html>
④编写调用页面DATAtage.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>数据标签</title>
</head>
<body>
<s:action name="actionTagAction1" executeResult="true"/>
<br>
<s:action name="actionTagAction1!default" executeResult="false">
<s:property value="#attr.stringByAction"/>
</s:action>
</body>
</html>
运行如图: