知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
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>
<action name="hello" class="com.tutorialspoint.struts2.HelloWorldAction" method="execute"> <result name="success" type="redirect"> <param name="location"> /NewWorld.jsp </param > </result> </action>
<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&width=100&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>
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>
<result name="success" type="json"> <param name="enableGZIP">true</param> </result>
<result name="success" type="json"> <param name="excludeProperties">.*direction</param> </result>
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"); } }