Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

validation using Resource Bundle

 
Manish Shinde
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to validate username and password using resource bundles in struts 2.
Here, I have written two keys in Properties file & in validation file I have used key attribute of message tag.
But when I clicked on submit button of EmployeeLogin.jsp without putting user name and password, I got username and password as output instead of user name is required & password is required


Here is code
1) EmployeeLogin.jsp
<%@page contentType="text/html" session="false" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib uri="/struts-tags" prefix="s"%>


<html>
<head> <title>Employee Login Form</title>

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

2)Login.properites
userName=user name is required
password=password is required
error.login=invalid username and password

3)Login-vaidation.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="userName">
<field-validator type="required">
<message key="username"/>
</field-validator>
</field>
<field name="password">
<field-validator type="required">
<message key=”password”/>
</field-validator>
</field>
</validators>

4)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("manish") && this.password.equals("manish")) {
return "success";
} else {
addActionError(getText("error.login"));
return "error";
}
}
}


Regards,
Manish
 
manni prat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not aware of struts 2.
But in struts 1 , we need to specify name of validation XML (in your case Login-vaidation.xml ) inside "struts-config.xml".
This is done where ValidatorPlugin is declared inside struts-config fiile.

Please check that once.
 
Manish Shinde
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank your for your reply

But struts 2 doesn't use struts-config.xml.
if you have any another solution please let me know

Regards,
Manish
 
manni prat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh ok.
Is it something to do with <s:fieldError/> in JSP?
 
Manish Shinde
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I tried EmployeeLogin.jsp <s:fieldError/> ,not working
I searched on net everywhere they just make properties file of the same name as of the action class & put it in the same package where the action class is. Then they just add key in validation xml ,
nothing written on the EmployeeLogin.jsp

I did same but it is not working for me.
i think I am missing very small thing but can not able to find it

Regards,
Manish
 
manni prat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a guess.
in your xml file your are using message key as below:

<field name="userName">
<field-validator type="required">
<message key="username"/>
</field-validator>
</field>

In this case your key is "username".Everywhere else you have used "userName" i.e camel case notation.
Is it possible that it is not able to find the key and hence not able to locate the message?
 
Manish Shinde
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
as per your suggestion i corrected that too but still no working
 
manni prat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
As we have tried all the possible options (which are not working ) I created sample app and tried with it .
It seems to be working .Could you please check for the differences in code below and your code:

============================STRUTS.XML
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="default" extends="struts-default">
<action name="LoginAction" class="vaannila.Login">
<result name="input">/login.jsp</result>
<result name="success">/success.jsp</result>
</action>
</package>
</struts>


============================Login-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="userName">
<field-validator type="requiredstring">
<message>User Name is required.</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message key="password.required" />
</field-validator>
</field>
</validators>

===========================LOGIN.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Login Page</title>
<s:head />
</head>
<body>
<s:form action="LoginAction">
<s:textfield name="userName" label="User Name" />
<s:password name="password" label="Password" />
<s:submit value="Login" />
</s:form>
</body>
</html>

 
manni prat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is your validation problem solved?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic