1.method标签
格式:<s:submit name="method:方法名" value="按钮名">
其实很简单,举个例子说明:
(1)action类,用来处理用户请求。methodPrefixAction.java
1 package com.action;
2
3 public class MethodPrefixAction {
4 public String execute1(){
5 System.out.println("execute1().....");
6 return null;
7 }
8 public String execute2(){
9 System.out.println("execute2().....");
10 return null;
11 }
12 }
在一个类中有两个函数,分别针对用户请求调用。
(2)struts.xml配置
1 <?xml version="1.0" encoding="GB2312" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
5
6
7 <struts>
8 <package name="com.action" extends="struts-default">
9 <action name="methodAction" class="com.action.MethodPrefixAction">
10 </action>
11 </package>
12
13 </struts>
(3)jsp文件,即用户界面层。method.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
2 <%@ taglib prefix="s" uri="/struts-tags"%>
3
4 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
5 <html>
6 <head>
7 <title>method.jsp</title>
8
9 </head>
10
11 <body>
12 <s:form action="methodAction">
13 <s:submit name="method:execute1" value="提交"></s:submit>
14 <s:submit name="method:execute2" value="提交"></s:submit>
15 </s:form>
16 </body>
17 </html>
第13、14行是关键,根据method标签后面的方法名去调用相应的方法。
2.action标签
格式:<s:action name="actionname" executeResult="true">
(1)ActionPrefixedAction.java
1 package com.action;
2
3 import org.apache.struts2.ServletActionContext;
4
5 public class ActionPrefixAction {
6 private String msg;
7 public String execute()throws Exception {
8 ServletActionContext.getRequest().setAttribute("ActionString", "这是default默认返回值,假设是welcome");
9 return "success";
10 }
11 public String getMsg(){
12 return msg;
13 }
14 public void setMsg(String msg){
15 this.msg = msg;
16 }
17 }
(2)struts.xml
1 <?xml version="1.0" encoding="GB2312" ?>
2 <!DOCTYPE struts PUBLIC
3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
4 "http://struts.apache.org/dtds/struts-2.0.dtd">
7 <struts>
8 <package name="com.action" extends="struts-default">
9 <action name="actionAction1" class="com.action.ActionPrefixAction">
10 <result name="success">success.jsp</result>
11 </action>
12 <action name="actionAction2" class="com.action.ActionPrefixAction" method="default">
13 <result name="success">success.jsp</result>
14 </action>
15 </package>
16
17 </struts>
(3)success.jsp用来显示结果界面
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
2 <%@ taglib prefix="s" uri="/struts-tags" %>
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <head>
6 <title>action.jsp</title>
7 </head>
8
9 <body>
10 <h3>Action返回的效果-success.jsp页面</h3>
11 <s:property value="msg"/>
12 <s:property value="#attr.ActionString"/>
13 </body>
14 </html>
(4)action.jsp 用户交互界面,但是这里代码比较简单,action标签相当于提交了表单,所以无需用户输入
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html; charset=utf-8"%>
2 <%@ taglib prefix="s" uri="/struts-tags" %>
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <head>
6 <title>action.jsp</title>
7 </head>
8
9 <body>
10 <div>下面的action标签将会显示结果的返回界面:::</div>
11 <br/>
12 <s:action name="actionAction1" executeResult="true"></s:action>
13 <br/>
14 ===========================================================
15 <div>下面的action标签将会显示dodefault方法的结果界面:::</div>
16 <s:action name="actionAction2" executeResult="true" ignoreContextParams="true"/>
17 <br/>
18 ===========================================================
19 <div>使用Action标签,但是executeResult为false,则不会显示结果界面:::</div>
20 <s:action name="actionAction2" executeResult="false"></s:action>
21 </body>
22 </html>
最后,配置编译后,输入 http://localhost:8080/Struts2Proj/action.jsp?msg=helloworld 其中Struts2Proj 是工程名。
显示结果如下:
3.Redirect标签和Redirect-action标签
这两个标签分别用于将请求重定向到URL或Action.
格式:<s:submit name="redirect:url" value="buttonname"/>
格式:<s:submit name="redirect-action:action-name" value="buttonname">
重定向到URL,点击按钮后,页面会重定向到指定网页,但是action按钮则重定向到指定的action,不过Redirect-action标签并不提交表单域数据。