wood burning stoves 2.0*
The moose likes Struts and the fly likes Form values are not set  while getting the validation(fielderrors) errors in struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Form values are not set  while getting the validation(fielderrors) errors in struts 2" Watch "Form values are not set  while getting the validation(fielderrors) errors in struts 2" New topic
Author

Form values are not set while getting the validation(fielderrors) errors in struts 2

dhanaraj krishnasamy
Greenhorn

Joined: Jan 07, 2009
Posts: 4
Hai,

i am working with Struts 2

In that use the form with enctype="multipart/form-data" because i use fileupload. in this when get any one field error it wont display the other fields value so please help me any one...

The Following code i used in the JSP

<s:form name="signup" theme="ajax" action="signup.action" method="POST" enctype="multipart/form-data">

<div style="text-align:right; width:600px"> <strong class="strong"> * </strong> All the fields are Mandatory </div>
<dl>
<dt> <span> <img src="<s:url value='/images/jobseeker_signup.png'/>" alt="recruiter" title="recruiter" border="0" style="margin:5px 0 0 20px;;"/> </span></dt>
<dd>  </dd>
<dt><strong> Login Details </strong> </dt>
<dd>  <s:actionerror/> </dd>
<dt> <s:label key="getText('signup.username')" theme="simple"></s:label> <strong class="strong"> * </strong>:</dt>

<dd> <s:textfield id="username" name="username" onchange="CheckDuplicate();" theme="simple"/>
<s:fielderror>
<s:param>username</s:param>
</s:fielderror>
<span class="hint"> Choose a Username. The username should be a minimum of 6 characters and a maximum of 24 characters. Do not include spaces or special characters , other than _, . , @ For e.g.: John_kingston, brad.123.<span class="hint-pointer"> </span></span>
<div id="chkusername" align="center" style="visibility:hidden;color:#2A2B7E;font-weight: bold;"></div> </dd>
<dt> <s:label key="getText('signup.password')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:password name="password" theme="simple" />
<s:fielderror>
<s:param>password</s:param>
</s:fielderror>
<span class="hint"> provide the password which should have minimum 6 to maximum 10 characters.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.confirmpassword')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:password name="confirmpassword" theme="simple"/>
<s:fielderror>
<s:param>confirmpassword</s:param>
</s:fielderror>

<span class="hint"> reenter the password you already entered in the previous option for the verification purpose.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.email')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>

<dd> <s:textfield id="email" name="email" onchange="CheckEmail();" theme="simple"/>

<s:fielderror>
<s:param>email</s:param>
</s:fielderror>
<span class="hint"> enter your email which you have using currently.<span class="hint-pointer"> </span></span>
<div id="chkemail" align="center" style="visibility:hidden;color:#2A2B7E;font-weight: bold;"></div> </dd>
<dt> <s:label key="getText('signup.mobile')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:textfield name="mobileno" theme="simple"/>
<s:fielderror>
<s:param>mobileno</s:param>
</s:fielderror>
<span class="hint"> enter your mobile number.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.landline')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:textfield name="landlineno" theme="simple"/>
<s:fielderror>
<s:param>landlineno</s:param>
</s:fielderror>
<span class="hint"> provide your landline number with state code.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.industry')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:radio cssStyle="width:45px;vertical-align:center;" list="'textile'" name="industrytype" onclick="document.signup.industry.disabled=1" theme="simple"/>
<s:radio cssStyle="width:45px;" list="'others'" name="industrytype" onclick="document.signup.industry.disabled=0" theme="simple"/>

<s:fielderror>
<s:param>industrytype</s:param>
</s:fielderror>
</dd>
<dt> <s:label key="getText('signup.otherindustry')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:textfield name="industry" disabled="1" theme="simple"/>
<s:fielderror>
<s:param>industry</s:param>
</s:fielderror>
<span class="hint"> provide your current industry name if you chosen other industry.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.role')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:textfield name="role" theme="simple"/>
<s:fielderror>
<s:param>role</s:param>
</s:fielderror>
<span class="hint"> enter your role or designation of your current or previous company.<span class="hint-pointer"> </span></span> </dd>
<dt> <s:label key="getText('signup.experience')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:select name="years" headerValue="-Years-" headerKey="1" list="{'0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','32','33','34','35','36','37','38','39','40+'}" theme="simple"/>
<s:select name="months" headerValue="-Months-" headerKey="1" list="{'0','1','2','3','4','5','6','7','8','9','10','11'}" theme="simple"/>
<s:fielderror>
<s:param>years</s:param>
<s:param>months</s:param>
</s:fielderror>
</dd>
<dt> <s:label key="getText('signup.upload')" theme="simple"></s:label> <strong class="strong"> * </strong>: </dt>
<dd> <s:file name="resume" accept="text/plain" theme="simple"/>

</dd>
<dt>  </dt>
<dd> <input type="checkbox" style="border:none; width:50px;vertical-align:middle" />I Accept the Whereismyboss Terms of use </dd>
<dt>  </dt>
<dd>
<button type="submit">
<img src="<s:url value='/images/buttons/submit.png'/>" alt="submit" style="vertical-align:middle"/> </button>
<button type="submit">
<img src="<s:url value='/images/buttons/cancel.png'/>" alt="submit" style="vertical-align:middle"/> </button>
</dd>
</dl>
</s:form>


The Following code is used in Action


package login.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.*;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;



import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.pgcinfotech.conf.LoadDatabase;
import com.pgcinfotech.crypto.Encrypt;

import login.constants.WIMBConstants;


public class SignUpAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{


private String username=getUsername();
private String password=getPassword();
private String confirmpassword=getConfirmpassword();
private String firstname=getFirstname();
private String lastname=getLastname();
private String email=getEmail();
private String mobileno=getMobileno();
private String landlineno=getLandlineno();
private String industrytype=getIndustrytype();
private String industry=getIndustry();
private String role=getRole();
private String resumeContentType=getResumeContentType();
private String resumeFileName=getResumeFileName();
private File resume=getResume();
private int years=getYears();
private int months=getMonths();
private String parm;
private String j_captcha_response;
private HttpSession session;
//private CommonCaptchaImage CommonCaptcha = new CommonCaptchaImage();
private HttpServletRequest request;
private HttpServletResponse response;

private String recaptcha_challenge_field;
private String recaptcha_response_field;
private String remote_Address;
//private ReCaptchaResponse reCaptchaResponse;
//private ReCaptchaImpl reCaptchaImpl = new ReCaptchaImpl();
//private SQLExecuter sqlExe = null;
//Connection conn=null;
//CallableStatement ct =null;
//private ResultSet rs=null;

public String execute() throws Exception {
Connection conn = null; // Connection declaration
/** java.io.InputStream is = new java.io.FileInputStream(resume);
java.io.OutputStream os = new java.io.FileOutputStream("g:/resume/" + resumeFileName);
byte buffer[] = new byte[8192];
int count = 0;
while((count = is.read(buffer)) > 0)
os.write(buffer, 0, count);
os.close();
is.close();

if(industrytype.equals("others")){
industrytype=industry;
}**/

//request = (HttpServletRequest) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
//response = (HttpServletResponse) ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_RESPONSE);
/*parm = request.getParameter("j_captcha_response");
session = request.getSession();
j_captcha_response= (String)session.getAttribute(CommonCaptcha.CAPTCHA_KEY);
if(!parm.equals(j_captcha_response)){
addActionError("Invalid Code! Please try again!");
return ERROR;
}*/
/* remote_Address = request.getRemoteAddr();

recaptcha_challenge_field = request.getParameter("recaptcha_challenge_field");

recaptcha_response_field = request.getParameter("recaptcha_response_field");

reCaptchaImpl.setPrivateKey("6LeQfgQAAAAAABXtqyuq5zDSHbLj6iNgtkXe7uYw");
Checking the challenge field and response field are equal using checkAnswer()
reCaptchaResponse = reCaptchaImpl.checkAnswer(remote_Address, recaptcha_challenge_field, recaptcha_response_field);
if(!reCaptchaResponse.isValid()){
addActionError("Invalid Recaptcha code! Please try again");
return ERROR;

}*/
if(!resumeFileName.equals("")){


String folder = ServletActionContext.getServletContext().getRealPath("/resume");

File rootDir = new File(folder);

if(!rootDir.exists())
rootDir.mkdirs();

String fileEx = resumeFileName.substring(
resumeFileName.indexOf("."),
resumeFileName.length());

String fileRealName = resumeFileName.substring(0, resumeFileName.indexOf(".")) +fileEx;

String fileName = folder + "\\" + fileRealName;


System.out.println("documentFileFileName : "+resumeFileName);


//MimetypesFileTypeMap m = new MimetypesFileTypeMap();
//System.out.println("Type "+m.getContentType(documentFileContentType));
copy(resume,new File(fileName));
}

months=(years*12)+months;
int randVal = new java.util.Random().nextInt();
String key = Integer.toString(randVal);
Encrypt ecr = new Encrypt();
String encrPwd = ecr.encryptText(key + "", password);
try {

conn = LoadDatabase.getInstance(WIMBConstants.DATA_SOURCE_KEY).getConnection();
CallableStatement ct=conn.prepareCall("call sp_insertjssignup(?,?,?,?,?,?,?,?,?,?,?,?,?)");
conn.setAutoCommit(false);

ct.setString(1, username);
ct.setString(2, encrPwd);
ct.setString(3, firstname);
ct.setString(4, lastname);
ct.setString(5, email);
ct.setString(6, mobileno);
ct.setString(7, landlineno);
// ct.setString(8, industrytype);
ct.setString(8, role);
ct.setInt(9, months);
ct.setString(10, key);
ct.setString(11, "JS");
ct.setString(12, "WEB");
ct.setString(13, resumeFileName);

ct.execute();
ct.close();
conn.commit();

return SUCCESS;
}
catch(Exception e) {
java.io.StringWriter sw = new java.io.StringWriter();
java.io.PrintWriter pw = new java.io.PrintWriter(sw);
e.printStackTrace();
return ERROR;}
finally {
try {
if(conn!=null)
conn.close();
} catch (SQLException e) {

}}
//return ERROR;

}
public int getMonths() {
return months;
}
public void setMonths(int months) {
this.months = months;
}
public int getYears() {
return years;
}
public void setYears(int years) {
this.years = years;
}
public String getConfirmpassword() {
return confirmpassword;
}
public void setConfirmpassword(String confirmpassword) {
this.confirmpassword = confirmpassword;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getIndustrytype() {
return industrytype;
}
public void setIndustrytype(String industrytype) {
this.industrytype = industrytype;
}
public String getLandlineno() {
return landlineno;
}
public void setLandlineno(String landlineno) {
this.landlineno = landlineno;
}
public String getMobileno() {
return mobileno;
}
public void setMobileno(String mobileno) {
this.mobileno = mobileno;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public File getResume() {
return resume;
}
public void setResume(File resume) {
this.resume = resume;
}
public String getResumeContentType() {
return resumeContentType;
}
public void setResumeContentType(String resumeContentType) {
this.resumeContentType = resumeContentType;
}
public String getResumeFileName() {
return resumeFileName;
}
public void setResumeFileName(String resumeFileName) {
this.resumeFileName = resumeFileName;
}
public String getIndustry() {
return industry;
}
public void setIndustry(String industry) {
this.industry = industry;
}

public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}

public String getJ_captcha_response() {
return j_captcha_response;
}
public void setJ_captcha_response(String j_captcha_response) {
this.j_captcha_response = j_captcha_response;
}
public String getParm() {
return parm;
}
public void setParm(String parm) {
this.parm = parm;
}

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

public HttpServletRequest getServletRequest(){
return request;
}

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

public HttpServletResponse getServletResponse(){
return response;
}

private void copy(File src, File dst){
InputStream in = null;
OutputStream out = null;
try{
in = new BufferedInputStream( new FileInputStream(src));
out = new BufferedOutputStream( new FileOutputStream(dst));

byte [] buffer = new byte [1024];
while (in.read(buffer) > 0 )
out.write(buffer);
in.close();
out.close();
}catch (Exception e){
e.printStackTrace();
}

}

}

Struts.properties

struts.custom.i18n.resources=package
struts.ui.theme=simple

Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hi,

First understand when you validate your form and validation called then the flow of your flow not reached at your action calss.

So all the variable which is use in jsp initialize again when your validation fail.

so, when you validate every time your validate methid call.

so Override validate methd in your Action class and initialize your hidden varibale in validate methid. Like.

@Override
public void validate() {

initialize all variable and write code what you written at your Action class

}

all variable which you want to get back when validation failed you have to again initialize at validate method.



Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
 
 
subject: Form values are not set while getting the validation(fielderrors) errors in struts 2
 
Similar Threads
How to display error message
Showing Hiding <s:div> tag in Struts2
How to Validate duplicate entries in database using struts1
Doubt in Action Form
how to do validation in struts1