struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<package name="policy" extends="struts-default">
<global-results>
<result name="success">/view/commons/Success.jsp</result>
<result name="error">/view/commons/Error.jsp</result>
<result name="*"> /view/commons/InvalidResult.jsp</result>
</global-results>
<action name="*/*/*" class="com.policydesigner.controller.{1}.{2}CTRL" method="{3}" >
<result name="create">/view/{1}/{2}Create.jsp</result>
<result name="update">/view/{1}/{2}Update.jsp</result>
<result name="view">/view/{1}/{2}View.jsp</result>
<result name="list">/view/{1}/{2}Search.jsp</result>
</action>
</package>
</struts>
Action class
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import com.elitecore.nvsmx.policydesigner.controller.constants.Results;
import com.elitecore.nvsmx.policydesigner.model.policy.PolicyData;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
/**
* @author kirpalsinh.raj
*
*/
public class PolicyCTRL extends ActionSupport implements ServletRequestAware, SessionAware, ParameterAware, Preparable {
private static final long serialVersionUID = 1L;
private static final String MODULE = PolicyCTRL.class.getSimpleName();
private static final Logger LOG = LoggerFactory.getLogger(PolicyCTRL.class);
private HttpServletRequest request;
private Map<String, String[]> parameters;
private Map<String, Object> session;
private String message;
private PolicyData policy = new PolicyData();
/* This method will be used to set a success/failure message.
* Message will be displayed on the same respected page */
public void setMessage(String message){
this.message=message;
}
public String getMessage(){
return message;
}
/* This method gives All the request parameter in Map */
@Override
public void setParameters(Map<String, String[]> thatParameters) {
this.parameters = thatParameters;
}
/* This method gives map of all the attributes stored in current session */
@Override
public void setSession(Map<String, Object> thatSession) {
this.session = thatSession;
}
/* This method gives the current request object*/
@Override
public void setServletRequest(HttpServletRequest thatRequest) {
this.request = thatRequest;
}
/* In each call to any method, this method will be called first.*/
@Override
public void prepare() throws Exception {
}
/* This method will be used to create the policy.
* If the create page violets the validation rules then @InputConfig(resultName="create")
* will take the page back to create page by returning "create" string in result*/
@InputConfig(resultName="create")
public String create(){
LOG.info(MODULE, "Method called create()");
setMessage("Policy Created Successfully");
return SUCCESS;
}
/* This method will be used to update the policy.
* If the update page violets the validation rules then @InputConfig(resultName="update")
* will take the page back to update page by returning "update" string in result*/
@InputConfig(resultName="update")
public String update(){
setMessage("Policy Updated Successfully");
return SUCCESS;
}
public String view(){
setMessage("Viewing Policy");
return Results.VIEW;
}
public String delete(){
setMessage("Policy deleted Successfully");
return SUCCESS;
}
public String initCreate(){
setMessage("Create Policy");
System.out.println("inside initCreate()");
return Results.CREATE;
}
public String initUpdate(){
setMessage("Update Policy");
return Results.UPDATE;
}
public String list(){
return Results.LIST;
}
public PolicyData getPolicy() {
return policy;
}
public void setPolicy(PolicyData policy) {
this.policy = policy;
}
public void validate() {
System.out.println("Method called validate()");
}
}
VALIDATION FILE
PolicyCTRL-validation.xml
Note: this file must be place at the same location where PolicyCTRL.java file resides
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="policy.name">
<field-validator type="requiredstring">
<message key="error.policy.name.required"/>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">2</param>
<param name="maxLength">16</param>
<param name="trim">true</param>
<message key="error.length.field" />
</field-validator>
</field>
</validators>
JSP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page import="com.policydesigner.model.policy.PolicyData"%>
<html>
<head>
<title>Create</title>
<s:head/>
</head>
<body>
<center>
<div class="msg" >
<s:form action="policy/Policy/create" validate="true" id="createForm" name="createForm">
<table>
<tr><td><s:textfield name="policy.name" id="name" label="Name" /></td></tr>
<tr><td> <s:submit/> </td></tr>
</table>
</s:form>
</div>
</center>
</body>
</html>
Take Care
Note: see the action attribute of s:form tag
<s:form action="policy/Policy/create" validate="true" id="createForm" name="createForm">
in this tag action attribute contains the values policy/Policy/create
this will be map with the wild cards used in the struts.xml file.
and remember to not begin the action attribute value with slash "/"
not do this : action="/policy/Policy/create" because it will make a call to the validate method of the respected action class. means it will load the page.
and action ="policy/Policy/create" : this will not load the page and it will perform the client side validation withe the generated java script.
:) Keep Visiting :)
<struts>
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<package name="policy" extends="struts-default">
<global-results>
<result name="success">/view/commons/Success.jsp</result>
<result name="error">/view/commons/Error.jsp</result>
<result name="*"> /view/commons/InvalidResult.jsp</result>
</global-results>
<action name="*/*/*" class="com.policydesigner.controller.{1}.{2}CTRL" method="{3}" >
<result name="create">/view/{1}/{2}Create.jsp</result>
<result name="update">/view/{1}/{2}Update.jsp</result>
<result name="view">/view/{1}/{2}View.jsp</result>
<result name="list">/view/{1}/{2}Search.jsp</result>
</action>
</package>
</struts>
Action class
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ParameterAware;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.interceptor.validation.SkipValidation;
import com.elitecore.nvsmx.policydesigner.controller.constants.Results;
import com.elitecore.nvsmx.policydesigner.model.policy.PolicyData;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
/**
* @author kirpalsinh.raj
*
*/
public class PolicyCTRL extends ActionSupport implements ServletRequestAware, SessionAware, ParameterAware, Preparable {
private static final long serialVersionUID = 1L;
private static final String MODULE = PolicyCTRL.class.getSimpleName();
private static final Logger LOG = LoggerFactory.getLogger(PolicyCTRL.class);
private HttpServletRequest request;
private Map<String, String[]> parameters;
private Map<String, Object> session;
private String message;
private PolicyData policy = new PolicyData();
/* This method will be used to set a success/failure message.
* Message will be displayed on the same respected page */
public void setMessage(String message){
this.message=message;
}
public String getMessage(){
return message;
}
/* This method gives All the request parameter in Map */
@Override
public void setParameters(Map<String, String[]> thatParameters) {
this.parameters = thatParameters;
}
/* This method gives map of all the attributes stored in current session */
@Override
public void setSession(Map<String, Object> thatSession) {
this.session = thatSession;
}
/* This method gives the current request object*/
@Override
public void setServletRequest(HttpServletRequest thatRequest) {
this.request = thatRequest;
}
/* In each call to any method, this method will be called first.*/
@Override
public void prepare() throws Exception {
}
/* This method will be used to create the policy.
* If the create page violets the validation rules then @InputConfig(resultName="create")
* will take the page back to create page by returning "create" string in result*/
@InputConfig(resultName="create")
public String create(){
LOG.info(MODULE, "Method called create()");
setMessage("Policy Created Successfully");
return SUCCESS;
}
/* This method will be used to update the policy.
* If the update page violets the validation rules then @InputConfig(resultName="update")
* will take the page back to update page by returning "update" string in result*/
@InputConfig(resultName="update")
public String update(){
setMessage("Policy Updated Successfully");
return SUCCESS;
}
public String view(){
setMessage("Viewing Policy");
return Results.VIEW;
}
public String delete(){
setMessage("Policy deleted Successfully");
return SUCCESS;
}
public String initCreate(){
setMessage("Create Policy");
System.out.println("inside initCreate()");
return Results.CREATE;
}
public String initUpdate(){
setMessage("Update Policy");
return Results.UPDATE;
}
public String list(){
return Results.LIST;
}
public PolicyData getPolicy() {
return policy;
}
public void setPolicy(PolicyData policy) {
this.policy = policy;
}
public void validate() {
System.out.println("Method called validate()");
}
}
VALIDATION FILE
PolicyCTRL-validation.xml
Note: this file must be place at the same location where PolicyCTRL.java file resides
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.2//EN" "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
<field name="policy.name">
<field-validator type="requiredstring">
<message key="error.policy.name.required"/>
</field-validator>
<field-validator type="stringlength">
<param name="minLength">2</param>
<param name="maxLength">16</param>
<param name="trim">true</param>
<message key="error.length.field" />
</field-validator>
</field>
</validators>
JSP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page import="com.policydesigner.model.policy.PolicyData"%>
<html>
<head>
<title>Create</title>
<s:head/>
</head>
<body>
<center>
<div class="msg" >
<s:form action="policy/Policy/create" validate="true" id="createForm" name="createForm">
<table>
<tr><td><s:textfield name="policy.name" id="name" label="Name" /></td></tr>
<tr><td> <s:submit/> </td></tr>
</table>
</s:form>
</div>
</center>
</body>
</html>
Take Care
Note: see the action attribute of s:form tag
<s:form action="policy/Policy/create" validate="true" id="createForm" name="createForm">
in this tag action attribute contains the values policy/Policy/create
this will be map with the wild cards used in the struts.xml file.
and remember to not begin the action attribute value with slash "/"
not do this : action="/policy/Policy/create" because it will make a call to the validate method of the respected action class. means it will load the page.
and action ="policy/Policy/create" : this will not load the page and it will perform the client side validation withe the generated java script.
:) Keep Visiting :)
No comments:
Post a Comment