aspose file tools*
The moose likes Struts and the fly likes validation using Resource Bundle Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "validation using Resource Bundle" Watch "validation using Resource Bundle" New topic
Author

validation using Resource Bundle

Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
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

Joined: Apr 07, 2011
Posts: 7
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

Joined: Mar 30, 2011
Posts: 39
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

Joined: Apr 07, 2011
Posts: 7
ohh ok.
Is it something to do with <s:fieldError/> in JSP?
Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
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

Joined: Apr 07, 2011
Posts: 7
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

Joined: Mar 30, 2011
Posts: 39
hi,
as per your suggestion i corrected that too but still no working
manni prat
Greenhorn

Joined: Apr 07, 2011
Posts: 7
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

Joined: Apr 07, 2011
Posts: 7
Is your validation problem solved?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validation using Resource Bundle