aspose file tools*
The moose likes Struts and the fly likes problems validation.xml 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 "problems validation.xml in struts 2" Watch "problems validation.xml in struts 2" New topic
Author

problems validation.xml in 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


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Hey ho, hey ho, off to the Struts forum we go!

In other words, this thread has nothing to do with user interfaces and therefore does not belong here. I will move it to our Struts forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
Welcome to Ranch..

Please use code tags to post any kind of code

Now, about your question did you try to refer and use the error tags
<html:errors/>

in your case try to insert the tag <s:errors/> ---------- this tag is used to display all the error messages
if you need to display the error message for each and every property at different positions <s:errors property="propertyname"/>

Hope it helps
Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
Thank you very much for quick reply.

I tried <s:errors/> but it is showing following error
no tag "errors" defined in tag library imported with prefix "s"

Regards,
manish
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
Please edit your first post with code tags....

i feel the EmployeeLogin.jsp is not complete.....

where did you use the <s:errors/> tag { it needs to be used in EmployeeLogin .jsp}.......???
with an taglib statement in the begining as: <%@taglib uri="/WEB-INF/struts-html.tld" prefix="s" %>
Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
hi,
Here is my EmployeeLogin.jsp

<%@ taglib uri="/struts-tags" prefix="s"%>
<html> <head> <title>Employee Login Form</title>
</head>
<body>
<s:div id="global">
<s:errors/> //here it is giving error.
<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><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>
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41


On the top of your jsp page you can have some like this:
Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
hi,
i tried <%@ taglib uri="/struts-tags" prefix="s"%>

there is no such tag <s:forEachError> in the tld referenced by above URI.

Would you tell me in which tld <s:forEachError> tag is present?

regards,
Manish
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
check the tld file for each kind of the error tags as below............{please read the previous messages in details}


<s:errors/> tag { it needs to be used in EmployeeLogin .jsp}.......???
with an taglib statement in the begining as: <%@taglib uri="/WEB-INF/struts-html.tld" prefix="s" %>



On the top of your jsp page you can have some like this:
view plaincopy to clipboardprint?
<%@ taglib uri="taglib.tld" prefix="s" %>

<s:forEachError>
<br><%=errorProperty%> : <%=errorMessage%>
</s:forEachError>

Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
Hi,
I have tried above things but it is not working
If you could provide me another working example on the same ground that will be helpful to me
Thank you.
Regards,
Manish
anirudh jagithyala
Ranch Hand

Joined: Dec 07, 2010
Posts: 41
Hey Manish,

After observing your files closely i found a mistake.....
Mostly that might cause the error

First remove all the error tags { sorry for that}
then check yout EmployeeLogin.jsp withe the validation.xml file

EmployeeLogin.jsp





login-validation.xml









The name in the login.jsp and the validator.xl must be the same ,,, here you user "userName" in login.jsp and "userNAME" in validator.xml they are different.........



Hope this helps
Manish Shinde
Ranch Hand

Joined: Mar 30, 2011
Posts: 39
I found the mistake.
validation xml file name should be same as your action class.

In my case my action class is Login.java and validation xml file name is EmployeeLogin-Validation.xml. It should be Login-validation.xml

Regards,
Manish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problems validation.xml in struts 2