<bean:message key="app.title"/>
デフォルトでは、Strutsのタグはxhmlに対応していない(閉じタグとか)。
<html:html xhtml="true">
とすることで、xhtml形式のタグを出力します。
たとえば何かを検索する画面(JSP)で、最初にその画面を開いたときは検索パラメータがのってませんよね。検索パラメータを画面に表示している場合、最初のアクセスだけエラーにならないようなロジックが必要になりそうですが、こんな回避法がありました。何のことはない、パラメタチェックを入れているだけですが、こんな感じでやるとキレイになりますね。
こちらは検索側の記述 <html:form action="/search"> <html:text property="input" /> <html:submit>送信</html:submit> </html:form>
こちらが表示側の記述 <logic:present name="input"> 入力値: <bean:write name="input" /> </logic:present>
ちなみに、下のinputはrequest.getParameter("input");ではなく、xxxx.getAttribute("input")*1;であることに注意。
<action path="/forward" parameter="/WEB-INF/jsp/index.jsp" type="org.apache.struts.actions.ForwardAction" />
FormBean?のvalidateメソッドを実装する。
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request){ ActionErrors errors = new ActionErrors(); //ユーザIDが空だったら if(userId==null || userId.length()==0){ ユーザIDのテキストボックスでエラーが発生した、エラー文言は「ユーザID」 errors.add("userId",new ActionError("errors.required","ユーザID")); } return errors; }
errors.add("userId",new ActionError?("errors.required","ユーザID"));の意味は
userIdが、<html:errors property="userId"/>に対応し、"errors.required"がプロパティファイルに対応し"ユーザID"が、{0}に対応する。
ApplicationResources?.propertiesに下記内容を追加(ファイルは/WEB-INF/classesにある)
errors.header=<hr><font color="red"><h3>ERROR</h3> errors.footer=<hr></font> errors.required={0} Required ← {0}にerrors.addでセットした文言が入る。
struts-config.xml のActionで
<action path="/user_regist" type="kino.action.UserRegistAction" name="userRegistForm" scope="request" attribute="userRegistForm" input="/regist.jsp">
のようにエラー遷移先(/regist.jsp)を指定する
まとめると、errors.add("userId",new ActionError?("errors.required","ユーザID")); で、errors.required={0} Required の{0}をユーザIDで置換して ユーザID Required となる
<% Map map=new HashMap(); map.put("data1","value1"); map.put("data2","value2"); map.put("data3","value3"); map.put("data4","value4"); request.setAttribute("map",map); %> <html:link page="/execute.do" name="map">aaaa</html:link>
みたく、mapを指定すると、パラメータとなる。(nameでBeanを指定、propertyでmapを指定してもよい。)
struts-config.xml
<form-beans> <form-bean name="testForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="name" type="java.lang.String" /> <form-property name="word" type="java.lang.String" /> </form-bean> </form-beans> <action-mappings> <action name="testForm" path="/test" scope="request" type="kino.action.TestAction"> <forward name="success" path="/WEB-INF/jsp/Test.jsp"> </forward> </action> </action-mappings>
につき
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html:html> <HEAD> <%@ page language="java" contentType="text/html; charset=SHIFT_JIS" pageEncoding="SHIFT_JIS" %> <META http-equiv="Content-Type" content="text/html; charset=SHIFT_JIS"> <META name="GENERATOR" content="IBM WebSphere Studio"> <TITLE>Test.jsp</TITLE> </HEAD> <BODY> <html:form action="/test"> <html:text property="name" /> <br /> <html:text property="word" /> <html:submit>送信</html:submit> </html:form> <logic:present name="name"> <bean:write name="name" /> </logic:present> <br /> <logic:present name="word"> <bean:write name="word" /> </logic:present> </BODY> </html:html>
struts-config.xml:
<action-mappings> <action path="/forward" type="org.apache.struts.actions.ForwardAction" parameter="/WEB-INF/jsp/Test.jsp"></action> </action-mappings>
で、forward.do でTest.jspに転送される。
現在のアクセス:75977