aspose file tools*
The moose likes Struts and the fly likes Struts2 - server side validation - data lost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 - server side validation - data lost" Watch "Struts2 - server side validation - data lost" New topic
Author

Struts2 - server side validation - data lost

francis varkey
Ranch Hand

Joined: Sep 13, 2005
Posts: 169
Hi,
I am using struts2-server side validation. Also I am using model driven approach. When validation fails, page is redirected to same page , but the data getting lost. More I have drop down on this page, which contains the list attribute, when the validation fails the list attribute is null consequently page is not compiling. I have integrated my struts2 projects as liferay portlet. Please advice.


Caused by: javax.portlet.PortletException: org.apache.jasper.JasperException: tag 'select', field 'list', name 'empList': The requested list key 'empMap' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]



Thanks,
Francis
francis varkey
Ranch Hand

Joined: Sep 13, 2005
Posts: 169
Let me explain with examples.


Action class
-----------------------

public class EmpAction extends ActionSupport implements ModelDriven<Object>,Preparable,ServletRequestAware,ServletResponseAware,SessionAware{

private HttpServletRequest request;
private HttpServletResponse response;
private Map<String,String[]> parameters;
private Map<String, Object> session;
private EmpModel empModel;

public void setServletRequest(HttpServletRequest request){
this.request =request;
}

public void setServletResponse(HttpServletResponse response){
this.response =response;
}

public void setParameters(Map<String,String[]> parameters){
this.parameters=parameters;
}

public void setSession(Map<String, Object> map) {
this.session=map;
}

public Model getModel(){
return empModel;
}


public void prepare() throws Exception{
empModel =new EmpModel();

}

public String showEmpPage(){

EmpDAO dao =new EmpDAO();
Map<String,String> desgMap=new HashMap<String,String>();

desgMap=dao.getDesignations();


empModel.seDesgMap(desgMap);

return SUCCESS;

}


}


Model class
--------------------------

public class EmpModel {

private Map<String,String> empMap;

public Map<String, String> getEmpMap() {
return empMap;
}
public void setEmpMap(Map<String, String> empMap) {
this.empMap =empMap;
}


DAO class
----------------

public class EmpDAO{

public Map getDesignations(){
Map<String,String> desgMap=new HashMap<String,String>();
desgMap.put("Dev","Developer");
desgMap.put("Mgr","Manager");
desgMap.put("Tl","TechLead");

return desgMap;
}

}




jsp ( first.jsp)
------------------

<s:form action="showEmpPage" method="POST" theme="simple" validate="true">
</s:form>

jsp(emp.jsp)
----------------------

<s:textfield id="emailAddress" name="emailAddress"" />

<s:select list="empMap" id="empDeg" name="empDesg" />




struts.xml



---------------------

<action name="showEmpPage" class="com.test.action. EmpAction" method="showEmpPage">
<result name="input">/WEB-INF/view/firstpafe.jsp</result>
<result name="success">/WEB-INF/view/emp.jsp</result>

</action>

<action name="createEmp" class="com.test.action.SecondAction" method="createEmp">
<result name="success">/WEB-INF/view/success.jsp</result>

<result name="input">/WEB-INF/view/emp.jsp</result>
</action>


Second Action
------------------------
public class SecondAction extends ActionSupport implements ModelDriven<Object>,Preparable,ServletRequestAware,ServletResponseAware,SessionAware{

private HttpServletRequest request;
private HttpServletResponse response;
private Map<String,String[]> parameters;
private Map<String, Object> session;
private SecondModel secondModel;

public void setServletRequest(HttpServletRequest request){
this.request =request;
}

public void setServletResponse(HttpServletResponse response){
this.response =response;
}

public void setParameters(Map<String,String[]> parameters){
this.parameters=parameters;
}

public void setSession(Map<String, Object> map) {
this.session=map;
}

public Model getModel(){
return secondModel;
}


public void prepare() throws Exception{
secondModel =new SecondModel();

}

public String creatEmp(){


return SUCCESS;

}


}


validation file (SecondAction-validation.xml)
--------------------------------------------------

<validators>

<field name="emailAddress">
<field-validator type="email">
<message>Please enter a valid email</message>
</field-validator>
</field>
</validators>




When I am entering wrong email id , the validation in second jsp will failed , and I am getting error .


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Struts2 - server side validation - data lost