Struts2で独自のhttpヘッダーを返したい場合2
httpheaderを見てみるとこれの後にdispatcherが呼べたら幸せなのにと思った。
で、作ってみた。
httpheaderとtempl(TemplateResult)を併せたresult型
※templはstruts.xml にはOGNL式が書ける。 - Struts 2+Spring 2+Tiles+Acegi+iBatis+GWT+JUnit 4+JMockit でフルスタックJAVAフレームワークで作成した独自のresult型でdispatcherとほぼ同じでさらにlocationのOGNL式をテンプレート化するもの。
これで、Cookieの設定や、200以外でのステータスコード返却を行ってさらに普通にコンテンツを返却したりできる。
なかなか良い感じなので、templに換わってdefault result型とする。
jp.co.hershe.struts2.util.TemplateWithHeaderResult.java
package jp.co.hershe.struts2.util; import java.util.Map; import org.apache.struts2.dispatcher.HttpHeaderResult; import org.apache.struts2.dispatcher.ServletDispatcherResult; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.Result; /** * httpheaderとtemplを併せた感じのResut * 例) * <result-type name="templWithHeader" class="tutorial.TemplateWithHeaderResult" default="true"> * <param name="template">${ localed.resource( "{0}#file#" ) }</param> * <param name="key">#file#</param> * </result-type> * * @author mikuni * */ public class TemplateWithHeaderResult extends TemplateResult { private static final long serialVersionUID = 5572203140029287914L; // 通常の Result 処理 HttpHeaderResult httpheader; // コンストラクタ public TemplateWithHeaderResult() { super(); httpheader = new HttpHeaderResult(); } // コンストラクタ public TemplateWithHeaderResult(String location) { super(location); httpheader = new HttpHeaderResult(); } public void setParse( boolean parse ) { httpheader.setParse(parse); } public void setStatus(int status) { httpheader.setStatus(status); } public Map getHeaders() { return httpheader.getHeaders(); } public void execute(ActionInvocation invocation) throws Exception { httpheader.execute(invocation); super.execute(invocation); } }
<result-types> <result-type name="templ2" class="jp.co.hershe.struts2.util.TemplateWithHeaderResult" default="true"> <param name="template">${ localed.resource( "/jsp{0}#file#" ) }</param> <param name="key">#file#</param> </result-type> </result-types>