Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Jayesh Patel
use struts validation with porltet.
April 18, 2013 11:51 PM
Answer

Jayesh Patel

Rank: New Member

Posts: 3

Join Date: March 18, 2013

Recent Posts

Hi to All :

I am new to liferay, i have developed a portlet with struts2 + spring 3 + hibernate 3 using maven. i need implement form validation in the site.
i have try to implement but it is not working. what to do .?? please help.

ACTION CLASS

public class DepartmentAction extends ActionSupport {

private static final long serialVersionUID = 6009588745294356983L;
private Department department;
private IDepartmentService departmentService;
private List<Department> departmentList;
private String result = "error";
private String resultSuccess = "success";
private String departmentNameExist = "";
private OrganizationGeneralInfo organizationGeneralInfo;
private Logger log = Logger.getLogger(DepartmentAction.class);
// Set organizationId references AdminMenu.jsp
private String organizationId;


// methods...

}

*** editDepartment.jsp ***
...
<s:div cssClass="controls">
<s:textfield id="departmentName" name="department.name" maxlength="45" required="true" />
</s:div>
...

*** struts.xml ***

<struts>
<constant name="struts.objectFactory" value="spring" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="AdminLabel"></constant>

<!-- ,json-default -->
<package name="default" namespace="/view" extends="struts-portlet-default">

<action name="saveDepartment" class="departmentAction" method="saveDepartment">
<result name="success">/pages/admin/department/departmentList.jsp</result>
<result name="error">/pages/admin/department/editDepartment.jsp</result>
<result name="input">/pages/admin/department/departmentList.jsp</result>
</action>
.....
.....

</package>
</struts>


**** DepartmentAction-saveDepartment-validation.xml ****

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="department.name">
<field-validator type="requiredstring">
<message>You must enter a name</message>
</field-validator>
</field>
</validators>

i have read that struts.xml packagemustextends "struts-default" i have extends "struts-portlet-default" , if i replce by "struts-default" i found a exception. " not allow in porlet "

this is my bytes of code. if any one can guide me, or help me....
Jack Bakker
RE: use struts validation with porltet.
April 19, 2013 3:18 PM
Answer

Jack Bakker

Rank: Liferay Master

Posts: 559

Join Date: January 3, 2010

Recent Posts

I have Struts2 going on for a Liferay v5.2.3 install ; but for v6 and better have gone with MVCPortlet for public/private and Vaadin for private-desktop-like

I really like Struts2 and sorry not to answer your question directly in context of Liferay

which version of Struts and Liferay are you trying to coordinate ?
Jayesh Patel
RE: use struts validation with porltet.
May 2, 2013 7:24 AM
Answer

Jayesh Patel

Rank: New Member

Posts: 3

Join Date: March 18, 2013

Recent Posts

sorry for late reply

Thanks dear jack for your reply.

i have resolve the issue.

****** struts.xml ******

<struts>
<package name="default" namespace="/view" extends="struts-portlet-default">
<action name="index" class="JobAction" method="saveJob">
<result name="success">/pages/login/view.jsp</result>
<result name="input">/pages/login/view.jsp</result>
</action>
</package>
</struts>


******** Action Class *********

public class JobAction extends ActionSupport {

public String saveJob() {
// method content
return result;
}
}

validate method to validate before saveJob() is called

public void validateSaveJob(){
clearErrors();
log.info("validate method job title : " + job.getTitle());
if (!validationUtil.isNonEmptyStringOnly(job.getTitle())) { /* here validationUtil is custom class with have method to validate the field is empty or not */
addFieldError("job.title", "Invalid Job Title"); /* this is add error method */
}
}


******** view.jsp file **********

<s:fielderror/> /* add this at the place where you want to show error message */
<s:form id="saveJobTitleForm" method="post" >

</s:form>