MobileNo validation Error and datetime validation technique - java

I am working on Struts 2 application where am trying to provide validation for mobile number of 10 digit number .
I am allowing only 10 digit number but even if i am entering 10 digit number it still showing validation error message AND also want to know is there any way buy which we can perform validation for date time picker DOJO tag.
Empaction-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 ="firstname">
<field-validator type="requiredstring">
<message key="requiredstring"/>
</field-validator>
</field>
<field name ="email">
<field-validator type = "email">
<message>provide valid Email</message>
</field-validator>
</field>
<field name ="mobileno">
<field-validator type="long">
<param name="min">10</param>
<param name="max">10</param>
<message key="stringlength" />
</field-validator>
</field>
</validators>
registation form is
<%# page language ="java" contentType ="text/html; charset=ISO-8859-1" pageEncoding ="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<%# taglib uri="/struts-dojo-tags" prefix="sx" %>
<html>
<head>
<sx:head/>
<script type="text/javascript" src ="script.js"></script>
</head>
<body>
<div align="center"> <h1 style="color: red"> ENPLOYEE REGISTRATION FORM</h1>
<s:form action="emplogin" method="post" >
<s:textfield name="firstname" label="Employee Firstname"/>
<s:textfield name ="lastname" label ="Last name"/>
<s:textfield name ="id" label="Id"/>
<s:radio name ="gender" list="{'male', 'female'}" label = "Gender"/>
<sx:datetimepicker name="dob" displayFormat="dd-MMM-yyyy" label="DOB"></sx:datetimepicker>
<s:radio name ="maritalstatus" list="{'singale','married'}" label="Marital Status" />
<s:textfield name ="email" label ="Email"/>
<sx:datetimepicker name ="joiningdate" displayFormat="dd-MMM-yyyy" label="Joining Date"></sx:datetimepicker>
<s:textfield name= "designation" label = "Designation"/>
<s:textarea name ="address" label ="Address" />
<s:textfield name = "country" label ="Country" />
<s:textfield name ="state" label = "State" />
<s:textfield name ="city" label ="City"/>
<s:textfield name ="pincode" label ="Pincode"/>
<s:textfield name ="mobileno" label="Mobile No"/>
<s:select name ="groups" list="{'group 1', 'group 2', 'group 3'}" label ="Group" cssStyle="{width:184px"/>
<tr><td> </td></tr>
<tr>
<td> </td>
<s:submit align="center"></s:submit>
</s:form>
</div>
</body>
</html>
in my bean class it is of long type
package model;
public class Empmodel {
private String firstname;
private String lastname;
private String id;
private String gender;
private String dob;
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
private String maritalstatus;
private String email;
private String joiningdate;
private String designation;
private String address;
private String country;
private String state;
private String city;
private int pincode;
private long mobileno;
private String groups;
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 getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getMaritalstatus() {
return maritalstatus;
}
public void setMaritalstatus(String maritalstatus) {
this.maritalstatus = maritalstatus;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getJoiningdate() {
return joiningdate;
}
public void setJoiningdate(String joiningdate) {
this.joiningdate = joiningdate;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public long getMobileno() {
return mobileno;
}
public void setMobileno(long mobileno) {
this.mobileno = mobileno;
}
public String getGroups() {
return groups;
}
public void setGroups(String groups) {
this.groups = groups;
}
public int getPincode() {
return pincode;
}
public void setPincode(int pincode) {
this.pincode = pincode;
}
}

That using the stringlenth validator you can validate the length of the string. Check the stringlength validator.
And using the date validator you can validate the date format. Check the date validator.
If you want to validate datetime picker using validate() you should use the logic that is to parse the value returned by the picker using SimpleDateFormat if you are not using other date time libraries like JODA and don't care about timezone then you should create ThreadLocal parser for datetimes.
EDIT:
validators.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"http://www.opensymphony.com/xwork/xwork-validator-config-1.0.dtd">
<validators>
<validator name="longlength" class="jspbean.struts.vaidators.LongLengthFieldValidator"/>
</validators>
LongLengthFieldValidator.java
public class LongLengthFieldValidator extends FieldValidatorSupport {
private boolean doTrim = true;
private int maxLength = -1;
private int minLength = -1;
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public int getMaxLength() {
return maxLength;
}
public void setMinLength(int minLength) {
this.minLength = minLength;
}
public int getMinLength() {
return minLength;
}
public void setTrim(boolean trim) {
doTrim = trim;
}
public boolean getTrim() {
return doTrim;
}
public void validate(Object object) throws ValidationException {
String fieldName = getFieldName();
String val = getFieldValue(fieldName, object).toString();
if (val == null || val.length() <= 0) {
// use a required validator for these
return;
}
if (doTrim) {
val = val.trim();
if (val.length() <= 0) {
// use a required validator
return;
}
}
if ((minLength > -1) && (val.length() < minLength)) {
addFieldError(fieldName, object);
} else if ((maxLength > -1) && (val.length() > maxLength)) {
addFieldError(fieldName, object);
}
}
}
XXX-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<validator type="longlength">
<param name="fieldName">mobileno</param>
<param name="minLength">10</param>
<param name="maxLength">10</param>
<param name="trim">true</param>
<message>Your mobileno number needs to be 10 characters long</message>
</validator>
</validators>

Related

Why do I often get exceptions when uploading files using "multipart/form-data"? [duplicate]

UploadvideoAction.java
private File id;
private String title;
private String url;
private String name="";
private String message="";
private String idContentType;
private String idFileName;
public String getIdContentType() {
return idContentType;
}
public void setIdContentType(String idContentType) {
this.idContentType = idContentType;
}
public String getIdFileName() {
return idFileName;
}
public void setIdFileName(String idFileName) {
this.idFileName = idFileName;
}
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}
public String getMessage() {
return message;
}
public void setMessage(String message1) {
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File getId() {
return id;
}
public void setId(File id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
struts.xml:
<action name="uploadvideo" class="com.myapp.ysrcptv.UploadvideoAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">video/mp4,video/ogg,video/webm</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>${url}</result>
<result name="login">adminlogin.jsp</result>
<result name="input">${url}</result>
</action>
uploadvideos.jsp:
<s:form cssClass="form" action="uploadvideo" method="post" validate="false" enctype="multipart/form-data">
<s:file cssClass="input" name="id" value="" placeholder="Video"></s:file>
<s:textfield cssClass="input" name="title" value="" placeholder="Video Title"></s:textfield>
<input type="hidden" name="name" value="gellery pic"/>
<input type="hidden" name="url" value="uploadvideos.jsp"/>
<s:submit cssClass="btn" value="Upload"></s:submit>
<div class="formdiv"><s:property value="message"/></div>
</s:form>
UploadvideoAction-validation.xml:
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="id">
<field-validator type="requiredstring">
<message>File is required.</message>
</field-validator>
</field>
Problem: Only server side file id validation is not working. Even if I selects a file its also showing validation message File is required. Remaining validations are working perfectly. Here I'm placing some stuff. Before its worked. After restarting my server this validation not working.
You have used wrong validator. "requiredstring" validator is used to validate textfields. You can use "required" validator that validates the field to be not null.
<field name="id">
<field-validator type="required">
<message>File is required.</message>
</field-validator>
</field>

How to validate file upload in Struts 2

UploadvideoAction.java
private File id;
private String title;
private String url;
private String name="";
private String message="";
private String idContentType;
private String idFileName;
public String getIdContentType() {
return idContentType;
}
public void setIdContentType(String idContentType) {
this.idContentType = idContentType;
}
public String getIdFileName() {
return idFileName;
}
public void setIdFileName(String idFileName) {
this.idFileName = idFileName;
}
public void setServletRequest(HttpServletRequest servletRequest) {
this.servletRequest = servletRequest;
}
public String getMessage() {
return message;
}
public void setMessage(String message1) {
this.message = message;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public File getId() {
return id;
}
public void setId(File id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
struts.xml:
<action name="uploadvideo" class="com.myapp.ysrcptv.UploadvideoAction">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">video/mp4,video/ogg,video/webm</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result>${url}</result>
<result name="login">adminlogin.jsp</result>
<result name="input">${url}</result>
</action>
uploadvideos.jsp:
<s:form cssClass="form" action="uploadvideo" method="post" validate="false" enctype="multipart/form-data">
<s:file cssClass="input" name="id" value="" placeholder="Video"></s:file>
<s:textfield cssClass="input" name="title" value="" placeholder="Video Title"></s:textfield>
<input type="hidden" name="name" value="gellery pic"/>
<input type="hidden" name="url" value="uploadvideos.jsp"/>
<s:submit cssClass="btn" value="Upload"></s:submit>
<div class="formdiv"><s:property value="message"/></div>
</s:form>
UploadvideoAction-validation.xml:
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
<field name="id">
<field-validator type="requiredstring">
<message>File is required.</message>
</field-validator>
</field>
Problem: Only server side file id validation is not working. Even if I selects a file its also showing validation message File is required. Remaining validations are working perfectly. Here I'm placing some stuff. Before its worked. After restarting my server this validation not working.
You have used wrong validator. "requiredstring" validator is used to validate textfields. You can use "required" validator that validates the field to be not null.
<field name="id">
<field-validator type="required">
<message>File is required.</message>
</field-validator>
</field>

struts2 application defaulting to error.jsp

I am creating a struts2 application. I want users to login to their
account. If the record is found, it should go to the success.jsp. If
the account is not found, it should default to to error.jsp. They can
then register. The problem I am having is that whether the records are
in the database or not, I get redirected to my error.jsp.
I would appreciate some help here with some like explanation to go with it
Here are my files:
StudentInfo
package org.comp.dto;
//import
public class StudentInfo implements Serializable{
private int studentId;
private String userName;
private String password;
private String password1;
private String firstName;
private String lastName;
private int age;
private String dateofBirth;
private Calendar dateCreated;
private GregorianCalendar lastLogin;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDateofBirth() {
return dateofBirth;
}
public void setDateofBirth(String dateofBirth) {
this.dateofBirth = dateofBirth;
}
public GregorianCalendar getLastLogin() {
return lastLogin;
}
public void setLastLogin(GregorianCalendar lastLogin) {
this.lastLogin = lastLogin;
}
public Calendar getDateCreated() {
return dateCreated;
}
public void setDateCreated(Calendar dateCreated) {
this.dateCreated = dateCreated;
}
}
StudentInfo.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class mutable="true" name="org.comp.dto.StudentInfo" table="studentinfo">
<id name="studentId" type="int">
<column name="studentId"/>
<generator class="native"/>
</id>
<property column="userName" name="userName" type="string" not-null="true"/>
<property column="password" name="password" type="string" not-null="true"/>
<property column="firstName" name="firstName" type="string" not-null="true"/>
<property column="lastName" name="lastName" type="string" not-null="true"/>
<property column="age" name="age" type="int" not-null="true"/>
<property column="dateofBirth" name="dateofBirth" type="string" not-null="true"/>
<property column="dateCreated" name="dateCreated" type="calendar" not-null="true"/>
<property column="lastLogin" name="lastLogin" type="timestamp" not-null="true"/>
</class>
</hibernate-mapping>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="loginpackage" namespace="/" extends="struts-default">
<action name="login" class="org.action.LoginAction" method="login">
<result name="success">success.jsp</result>
<result name="input">login.jsp</result>
<result name="error">error.jsp</result>
</action>
<action name="accountSetUpAction" class="org.action.LoginAction" method="accountSetUp">
<result name="success">success.jsp</result>
</action>
</package>
</struts>
success.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Success page</title>
</head>
<body>
Login successfully! <s:property value="firstName"/>
</body>
</html>
LoginAction class
package org.action;
//import
public class LoginAction extends ActionSupport implements ModelDriven{
private int studentId;
private String userName;
private String password;
private String firstName;
private StudentInfo studentUser = new StudentInfo();
private Session session;
private String message;
PreparedStatement sQry;
ResultSet rs;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public StudentInfo getStudentUser() {
return studentUser;
}
public void setStudentUser(StudentInfo studentUser) {
this.studentUser = studentUser;
}
public LoginAction() {
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
public void validate() {
if (StringUtils.isEmpty(studentUser.getUserName())){
addFieldError("userName", "Username cannot be blank");
}
if (StringUtils.isEmpty(studentUser.getPassword())){
addFieldError("password", "Password cannot be blank");
}
}
#Override
public String execute() throws Exception {
return SUCCESS;
}
public String login(){
String ret = ERROR;
Connection conn = null;
try {
SessionImplementor impl = (SessionImplementor)session;
conn = impl.getJdbcConnectionAccess().obtainConnection();
org.hibernate.Transaction tx = session.beginTransaction();
String sQry = "SELECT firstName FROM StudentInfo WHERE studentinfo.studentId=? AND studentinfo.userName=? AND studentinfo.password=?";
PreparedStatement q = conn.prepareStatement(sQry);
q.setInt(1, studentId);
q.setString(2, userName);
q.setString(3, password);
rs = q.executeQuery();
while (rs.next()){
rs.getString(2);
ret = SUCCESS;
}
}
catch(Exception ex){
ret = ERROR;
}
finally
{
if (conn !=null){
try {
session.getTransaction().rollback();
conn.close();
}
catch (Exception ex){
}
}
}
return ret;
}
public String accountSetUp() throws Exception{
Transaction tx = null;
try {
tx = session.beginTransaction();
Calendar dateCreated = new GregorianCalendar();
studentUser.setDateCreated(dateCreated);
session.save(studentUser);
session.getTransaction().commit();
return SUCCESS;
}
catch (HibernateException ex){
if(tx != null) tx.rollback();
ex.printStackTrace();
return null;
}
finally
{
session.close();
}
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
#Override
public Object getModel() {
return studentUser;
}
}
The login.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Page</title>
</head>
<body>
<s:form action="login" method="get">
<s:textfield label="Please StudentID:" key="studentId"/>
<s:textfield label="Please Enter User Name:" key="userName"/>
<s:password label="Please Enter Password:" key="password"/>
<s:submit/>
</s:form>
</body>
</html>
In your LoginAction class:
String sQry = "SELECT firstName FROM StudentInfo WHERE studentinfo.userName=? AND studentinfo.password=?";
PreparedStatement q = conn.prepareStatement(sQry);
q.setInt(1, studentId);
q.setString(2, userName);
q.setString(3, password);
In the query you have two parameters (userName, password). But you are trying to set three after (studentId, userName, password).
To see the error in console, write ex.printStackTrace(); in your catch blocks (ex is your Exception name).

Why the errors.hasErrors() always return false in Spring 4 annotated form validation?

I tried to implement the annotated form input validation in Spring 4.
The following tutorials just don't work.
http://www.javacodegeeks.com/2013/04/spring-mvc-form-validation-with-annotations-2.html
http://codetutr.com/2013/05/28/spring-mvc-form-validation/
The symptoms are the same: the errors.hasErrors() always return false.
And some reader of the above tutorial 2 reported the identical issue, too.
This is the model object Spitter :
public class Spitter {
private Long id;
#NotNull
#Size(min = 5, max = 16)
private String username="default name";
#NotNull
#Size(min = 5, max = 25)
private String password;
#NotNull
#Size(min = 2, max = 30)
private String firstName;
#NotNull
#Size(min = 2, max = 30)
private String lastName;
#NotNull
#Email
private String email;
public Spitter() {
}
public Spitter(String username, String password, String firstName,
String lastName, String email) {
this(null, username, password, firstName, lastName, email);
}
public Spitter(Long id, String username, String password, String firstName,
String lastName, String email) {
this.id = id;
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getUsername() {
return username;
}
public void setUsername(#Size(min = 5, max = 16) String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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 getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
#Override
public boolean equals(Object that) {
return EqualsBuilder.reflectionEquals(this, that, "firstName",
"lastName", "username", "password", "email");
}
#Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this, "firstName",
"lastName", "username", "password", "email");
}
}
This the form JSP:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%# taglib uri="http://www.springframework.org/tags/form" prefix="f" %>
<%# page session="false" %>
<html>
<head>
<title>Spitter</title>
<link rel="stylesheet" type="text/css"
href="<c:url value="/resources/style.css" />" >
</head>
<body>
<h1>Register</h1>
<f:form method="POST" commandName="spitter" modelattribute="spitter">
f-First Name: <f:input path="firstName" /><br/>
f-Last Name: <f:input path="lastName" /><br/>
f-Email: <f:input path="email" /><br/>
f-User Name: <f:input path="username" /><br/>
f-Password: <f:input path="password" /><br/>
<input type="submit" value="Register" />
</f:form>
</body>
</html>
I am using:
validation-api-1.1.0.Final.jar
hibernate-validator-5.0.1.Final.jar
This question is solved at here!
How to turn on annotation driven validation in Spring 4?
You also need to tell Spring to enable JSR-303 validation. If you are using an xml way, try registering
<mvc:annotation-driven/> in your context xml
or if you are using java style:
#EnableWebMvc atop your context loading class
Hope this solves

In Struts2 I am not getting form value on success.jsp page

In my application I am trying to register a user by adding his details to the database and after success fully insert these values. I need to display form input page but on result page I am not getting any output.
My ACTION CLASS IS
package action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import dao.Empdao;
import model.Empmodel;
public class Empaction extends ActionSupport implements ModelDriven<Object>{
private static final long serialVersionUID = 1L;
Empmodel modelobj;
public Empmodel getModelobj() {
return modelobj;
}
public void setModelobj(Empmodel modelobj) {
this.modelobj = modelobj;
}
public String execute() throws Exception{
Empdao empdao = new Empdao();
int queryResult = empdao.registration(modelobj);
if (queryResult==0)
{
addActionError("it is a dublicate entry please enter anothe id");
return ERROR;
}
else{
return SUCCESS;
}
}
#Override
public Object getModel() {
modelobj = new Empmodel();
return null;
}
}
Success.jsp is
<body>
first name : <s:property value="modelobj.firstname"/> <br>
last name :<s:property value="modelobj.lastname" />
<s:property value="modelobj.id" />
<s:property value="modelobj.gender" />
<s:property value="modelobj.dob" />
<s:property value="modelobj.maritalstatus" />
<s:property value="modelobj.email" />
<s:property value="modelobj.joiningdate" />
<s:property value="modelobj.designation" />
<s:property value="modelobj.country" />
<s:property value="modelobj.state" />
<s:property value="modelobj.city" />
<s:property value="modelobj.pincode" />
<s:property value="modelobj.mobileno" />
<s:property value="modelobj.groups" />
</body>
</html>
Empmodel CLASS IS
package model;
public class Empmodel {
private String firstname;
private String lastname;
private String id;
private String gender;
private String dob;
private String maritalstatus;
private String email;
private String joiningdate;
private String designation;
private String address;
private String country;
private String state;
private String city;
private int pincode;
private long mobileno;
private String groups;
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 getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getMaritalstatus() {
return maritalstatus;
}
public void setMaritalstatus(String maritalstatus) {
this.maritalstatus = maritalstatus;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getJoiningdate() {
return joiningdate;
}
public void setJoiningdate(String joiningdate) {
this.joiningdate = joiningdate;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public long getMobileno() {
return mobileno;
}
public void setMobileno(long mobileno) {
this.mobileno = mobileno;
}
public String getGroups() {
return groups;
}
public void setGroups(String groups) {
this.groups = groups;
}
public int getPincode() {
return pincode;
}
public void setPincode(int pincode) {
this.pincode = pincode;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"/>
<constant name="struts.devmode" value="true"/>
<package name="loginmodel" extends ="struts-default">
<action name ="emplogin" class ="action.Empaction">
<result name= "wait">/Registration/wait.jsp</result>
<result name = "input">/Registration/empregistration.jsp</result>
<result name ="success" type="redirect" >/Registration/success.jsp</result>
<result name="error">/Registration/empregistration.jsp</result>
</action>
</package>
empregistration.jsp
<%# page language ="java" contentType ="text/html; charset=ISO-8859-1" pageEncoding ="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<%# taglib uri="/struts-dojo-tags" prefix="sx" %>
<html>
<head>
<sx:head/>
<script type="text/javascript" src ="script.js"></script>
</head>
<body>
<div align="center"> <h1 style="color: red"> ENPLOYEE REGISTRATION FORM</h1>
<s:form action="emplogin" method="post">
<s:textfield name="modelobj.firstname" label="Employee Firstname"/>
<s:textfield name ="modelobj.lastname" label ="Last name"/>
<s:textfield name ="modelobj.id" label="Id"/>
<s:radio name ="modelobj.gender" list="{'male', 'female'}" label = "Gender"/>
<sx:datetimepicker name="dob" displayFormat="dd-MMM-yyyy" label="DOB"></sx:datetimepicker>
<s:radio name ="modelobj.maritalstatus" list="{'singale','married'}" label="Marital Status" />
<s:textfield name ="modelobj.email" label ="Email"/>
<sx:datetimepicker name ="modelobj.joiningdate" displayFormat="dd-MMM-yyyy" label="Joining Date"></sx:datetimepicker>
<s:textfield name= "modelobj.designation" label = "Designation"/>
<s:textarea name ="modelobj.address" label ="Address" />
<s:textfield name = "modelobj.country" label ="Country" />
<s:textfield name ="modelobj.state" label = "State" />
<s:textfield name ="modelobj.city" label ="City"/>
<s:textfield name ="modelobj.pincode" label ="Pincode"/>
<s:textfield name ="modelobj.mobileno" label="Mobile No"/>
<s:select name ="modelobj.groups" list="{'group 1', 'group 2', 'group 3'}" label ="Group" cssStyle="{width:184px"/>
<tr><td> </td></tr>
<tr>
<td> </td>
<s:submit align="center"></s:submit>
</s:form>
</div>
</body>
</html>
You are redirecting the page on success that is wrong because you loose values needed to display results. Use
<result name ="success">/Registration/success.jsp</result>
or simply
<result>/Registration/success.jsp</result>
BTW, you only have getters & setters for firstname and lastname, so the only these properties will display. If you want to display other properties then you should add correcponding methods to fields.
You may read the servlet API javadoc to make clue what may cause result type="redirect".
For further compare the difference between forward and redirect results consider this thread.
If type=redirectAction then you have to mention the Action name without any extensions
if its only redirect then you have to mention including the action name and extension, For your Question it was because of redirect the problem happened.
Even after redirecting to some page and when you come back to the result page , and if you want to retain data then its better you better put in session .
Do not redirect you page remove it and use
<result name ="success">/Registration/success.jsp</result> in you struts.xml file

Categories