This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes problems in validation part of struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Reply locked New topic
Author

problems in validation part of struts 2

Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
I m facing some problems in validation part of the struts 2

I have created some login application where I want validate to user name and password.

Here is the code.

1)index.jsp
<ul>
<li>
<s:a href="EmployeeLogin.action">Employee Login Application</s:a>
</li>
</ul>

2) EmployeeLogin.jsp
</head>
<body>
<div id="global">
<h3 align="center">Employee Login</h3>
<s:form action="Login" validate="true">
<table align="center">
<tr> <td><s:textfield name="userName" label="User Name" </td> </tr>
<tr><td><s:password name="password" label="Password" /></td></tr>
<tr><td><s:submit/></td> </tr>
</table>
</s:form>
</div>
</body>
</html>


3)Action Class –Login.java
package employee;
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport{
private String userName;
private String password;

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
System.out.println(password);
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
System.out.println(userName);
}

public String doLogin() {
if (this.userName.equals("Tom") && this.password.equals("Tom")) {
return "success";
} else {
return "error";
}
}
}

4)struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<include file="employee.xml"/>
</struts>

5)employee.xml
<struts>
<package name="employee" namespace="/JSP" extends="struts-default" >
<action name="EmployeeLogin">
<result>/JSP/EmployeeLogin.jsp</result>
</action>

<action name="Login" class="employee.Login" method="doLogin">
<result name="success">
<param name="location">/JSP/LoggedIn.jsp</param>
</result>
<result name="error">/JSP/EmployeeLogin.jsp</result>
</action>

</package>

</struts>

6)login-validation.xml

<?xml version="1.0" encoding="UTF-8"?>


<!DOCTYPE validators PUBLIC 'PUBLIC:-//OpenSymphony Group//XWork Validator 1.0.2//EN'
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="userNAME">
<field-validator type="required">
<message>User Name is required </message>
</field-validator>
</field>
<field name="password">
<field-validator type="required">
<message>Password is required </message>
</field-validator>
</field>
</validators>

When I go on EmployeeLogin.jsp and without putting user name & password I click on submit it should show message User Name is required and password is required
But this not working.Also not showing any kind of error.

I have put login-validation.xml in the place where struts.xml and employee.xml are.
Also I tried another way by putting login-validation.xml in place where Login.java is.
Still it is not working.


Your help will be appreciated.

Regards,
Manish


Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8839
    
    7

Please do not post the same question more than once. It causes confusion and duplication of effort as the community tries to help everyone.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems in validation part of struts 2
 
Similar Threads
problems validation.xml in struts 2
validation in struts2
validation using Resource Bundle
Struts2: Cannot submit action after a second validation attempt.
Form Validation using xml in struts2