Home | 简体中文 | 繁体中文 | 杂文 | 打赏(Donations) | OSChina 博客 | Facebook | Linkedin | 知乎专栏 | Search | About

15.3. Action

struts.xml

		
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

	<constant name="struts.devMode" value="true" />

	<package name="basicstruts2" extends="struts-default">
		<action name="index">
			<result>/index.jsp</result>
		</action>

		<action name="hello"
			class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute">
			<result name="success">/HelloWorld.jsp</result>
		</action>

		<action name="register" class="org.apache.struts.register.action.Register"
			method="execute">
			<result name="success">/thankyou.jsp</result>
			<result name="input">/register.jsp</result>
		</action>

	</package>
</struts>
		
		

15.3.1. redirect

			
<action name="hello" 
   class="com.tutorialspoint.struts2.HelloWorldAction"
   method="execute">
   <result name="success" type="redirect">
       <param name="location">
         /NewWorld.jsp
      </param >
   </result>
</action>
			
			

15.3.2. redirectAction

			
<package name="public" extends="struts-default">
    <action name="login" class="...">
        <!-- Redirect to another namespace -->
        <result type="redirectAction">
            <param name="actionName">dashboard</param>
            <param name="namespace">/secure</param>
        </result>
    </action>
</package>
 
<package name="secure" extends="struts-default" namespace="/secure">
    <-- Redirect to an action in the same namespace -->
    <action name="dashboard" class="...">
        <result>dashboard.jsp</result>
        <result name="error" type="redirectAction">error</result>
    </action>
 
    <action name="error" class="...">
        <result>error.jsp</result>
    </action>
</package>
 
<package name="passingRequestParameters" extends="struts-default" namespace="/passingRequestParameters">
   <!-- Pass parameters (reportType, width and height) -->
   <!--
   The redirectAction url generated will be :
   /genReport/generateReport.action?reportType=pie&amp;width=100&amp;height=100#summary
   -->
   <action name="gatherReportInfo" class="...">
      <result name="showReportResult" type="redirectAction">
         <param name="actionName">generateReport</param>
         <param name="namespace">/genReport</param>
         <param name="reportType">pie</param>
         <param name="width">100</param>
         <param name="height">100</param>
         <param name="empty"></param>
         <param name="suppressEmptyParameters">true</param>
         <param name="anchor">summary</param>
      </result>
   </action>
   
   <action name="Auth" class="cn.netkiller.api.action.Auth">
		<result name="credit" type="redirectAction">
			<param name="actionName">history</param>
			<param name="namespace">/api/report</param>
			<param name="LoginName">${LoginName}</param>
			<param name="Direction">${Direction}</param>
			<param name="StartDate">${StartDate}</param>
			<param name="EndDate">${EndDate}</param>
		</result>
	</action>
   
</package>
			
			

15.3.3. JSON

JSON 拦截器

			
	<action name="withdraw" class="cn.netkiller.api.action.Report" method="getHistory">
		<interceptor-ref name="defaultStack" />
		<interceptor-ref name="json">
			<param name="enableSMD">true</param>
		</interceptor-ref>
		<result name="success" type="json">
			<param name="enableGZIP">true</param>
			<param name="excludeProperties">.*direction</param>
		</result>
	</action>
			
			

15.3.3.1. enableGZIP 压缩传输

				
		<result name="success" type="json">
			<param name="enableGZIP">true</param>
		</result>
				
				

15.3.3.2. excludeProperties 排除 Properties

				
		<result name="success" type="json">
			<param name="excludeProperties">.*direction</param>
		</result>
				
				

15.3.3.3. 

				
				
				

15.3.4. 传递 Timestamp 变量

Struts 从URL传递 Timestamp 类型的变量 StartDate

			
http://www.netkiller.cn/api/report/history.do?LoginName=888666&StartDate=2016-01-01&EndDate=2016-02-29
			
			

解决方法 参考 setStartDate / setEndDate 两个方法,以字符串方式传入,然后转换为Timestamp类型

			
package cn.netkiller.api.action;

import java.sql.Timestamp;
import java.util.Calendar;
import org.apache.struts2.json.annotations.JSON;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
			
public class Report extends ActionSupport {
	private static final long serialVersionUID = 6484202866632836225L;

	private String loginName;
	private Timestamp startDate = null;
	private Timestamp endDate = null;

	public Report() {

	}

	public String execute() {
		return Action.SUCCESS;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public Timestamp getStartDate() {
		return startDate;
	}

	public void setStartDate(String startDate) {
		this.startDate = Timestamp.valueOf(startDate + " 00:00:00");
	}

	public Timestamp getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = Timestamp.valueOf(endDate + " 00:00:00");
	}
	
}