Configure web.xml
for Struts
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
Prevent from
resubmit form
public ActionForward
beforeLogin( .. ) throws Exception {
this.saveToken(request);//save token before
showing Login.jsp
return mapping.findForward(LOGINPAGE);
}
public ActionForward login(
.. ) throws Exception {
if(this.isTokenValid(request)){
resetToken(request);
}else if(!this.isTokenValid(request)){
ActionErrors actionErrors = new ActionErrors();
actionErrors.add("Resubmit", new ActionMessage("Resubmit.Resubmit"));
saveErrors(request, actionErrors);
}
return mapping.findForward(LOGINSUCCESS);
}
Include action
- In
struts-config.xml
<action
path="/myIncludeLink" type="org.apache.struts.actions.IncludeAction" parameter="/include.jsp" />
- In Jsp
<jsp:include page="/myIncludeLink.do"
/>
Forward action
- In
struts-config.xml
<action
path="/CalculationPage" type="org.apache.struts.actions.ForwardAction"
parameter="/pages/calculation.jsp"
/>
- In Jsp
<html:link action="/Welcome.do">Forward Action</html:link>
Dynaform
- In
Struts-config.xml
<form-bean name="calculationForm"
type="org.apache.struts.action.DynaActionForm">
<form-property name="number1"
type="java.lang.Integer" />
<form-property name="number2"
type="java.lang.Integer" />
<form-property name="total"
type="java.lang.Integer" />
</form-bean>
- In Action
class
DynaActionForm calForm = (DynaActionForm) form;
Integer number1 = Integer.parseInt(calForm.get("number1").toString());
Configuring action
class in struts-config.xml
<action path="/calculation" type="com.shambhu.kumar.actions.CalculationAction"
name="calculationForm" parameter="method">
<forward name="success"
path="/pages/calculation.jsp"/>
</action>
Configuring
Message resource propeties
<message-resources parameter="ApplicationResources"
/>
Configuring
validation Plug-in
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,
/WEB-INF/validator-user.xml" />
</plug-in>
Validation-user.xml
<form-validation>
<formset>
<form name="loginForm">
<field property="username"
depends="required,maxlength,minlength,mask">
<msg name="required"
key="err.user.username.required" />
<msg name="minlength"
key="err.user.username.length" />
<msg name="mask"
key="err.user.username.invalid" />
<arg name="minlength"
key="${var:minlength}" position="0"
resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>3</var-value>
</var>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z0-9-_]*$</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
Can pass method
name from button
<html:form action="calculation">
<table>
<tr>
<td align="center"
bgcolor="red" colspan="2"><html:errors
/></td>
</tr>
<tr>
<td align="right"
width="35%">Number 1 :</td>
<td align="left"><html:text property="number1" /></td>
</tr>
<tr>
<td align="right">Number 2 :</td>
<td align="left"><html:text property="number2" /></td>
</tr>
<tr>
<td align="right">Total :</td>
<td align="left"><html:text property="total" /></td>
</tr>
<tr>
<td align="right"
colspan="2">
<html:submit property="method"
value="add" />
<html:submit property="method"
value="substract" />
<html:submit property="method"
value="multiply" />
</td>
</tr>
</table>
</html:form>
0 comments: