1

Hi I m very new to struts2. from some books and websites I got this example. In action class it use validate method to check logic. The method is called bcoz from the print statements. But the error is not shown near to the fields. Jst help me

Index.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>Home</title>
</head>
<body>
<s:form action="LoginAction ">
<s:textfield name="username" label="Username" />
<s:textfield name="password" label="Password" />
<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>

Struts.xml

<package name="default" extends="struts-default">
        <action name="LoginAction" class="org.shammu.struts2.actions.LoginAction">
                <result name="success">/welcome.jsp</result>
                <result name="input">/index.jsp</result>
        </action>
        <action name="emptypassthrough">
                <result>index.jsp</result>
        </action>


</package>

LoginAction.java

 package org.shammu.struts2.actions;

 import org.shammu.struts2.beans.LoginBean;

 import com.opensymphony.xwork2.ActionSupport;

 @SuppressWarnings("serial")
 public class LoginAction extends ActionSupport{

private String username;
private String password;
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 execute(){
    LoginBean login = new LoginBean();
    login.setPassword(password);
    login.setUsername(username);
    if (getUsername().equals("abcd")) {
        return "success";
    } else {
        return "input";
    }

}

@Override
public void validate() {
    if (username.length() < 3) {
        System.out.print("user err ok");
        this.addFieldError(username, "Username Empty Pls provide");

    }
    if (password.length() < 3) {
        System.out.print("Pass err ok");
        this.addFieldError(password, "Password Empoty provride one pkls");
    }

}
 }

I m not considering execute method. I want jst the error msg printed near to fields... Help

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
Shameer
  • 351
  • 2
  • 5
  • 11
  • 1
    You are not correctly use `addFieldError` method. The first parameter is a field name. In your case it should be `addFieldError("username", "Username Empty Pls provide");`. – Aleksandr M Aug 28 '13 at 09:32
  • Tnx a lot Aleksandr.. In addFieldError, whenever I type username, eclips dropdown the username which is actually a field declared in the class. It got misundrstnad... Tnx agan. I dont knw how to vote, If I know, ll give u a 1000 votes, bcz u help to reduce mo a lot of tm..\ – Shameer Aug 28 '13 at 11:06
  • @AleksandrM how to make ur comment as answer..? – Shameer Aug 28 '13 at 11:16
  • You can accept/upvote my answer now. – Aleksandr M Aug 28 '13 at 18:30

2 Answers2

4

To display fielderror you need to add <s:fielderror /> in your jsp.

It Render field errors if they exists. Specific layout depends on the particular theme. The field error strings will be html escaped by default.

<%@ 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>Home</title>
</head>
<body>
<s:form action="LoginAction ">

<s:textfield name="username" label="Username" /><s:fielderror fieldName="username"/>
<s:textfield name="password" label="Password" /><s:fielderror fieldName="password"/>

<s:submit value="Login"></s:submit>
</s:form>
</body>
</html>
Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
  • tnx ankur it worked. Bt a problm. The field error dispalying all errors where it declared. I need separate error reports under their corresponding fields. Username error is under that txt field and password error is under its textfiled... – Shameer Aug 28 '13 at 08:02
  • @Shameer: Modified the answer as per your requirement. – Ankur Lathi Aug 28 '13 at 08:36
  • 3
    There is no need to use `` tags for default error reporting in `xhtml` theme. – Aleksandr M Aug 28 '13 at 09:39
  • @AnkurLathi The modified answer not working. Whenever I put the **fieldName** attribute, it displays nothing... – Shameer Aug 28 '13 at 10:59
  • You are not correctly use addFieldError method. The first parameter is a field name. In your case it should be addFieldError("username", "Username Empty Pls provide");. – Aleksandr M..... This works fine and its the right answer.. – Shameer Aug 28 '13 at 11:06
  • @Shameer: It is not working because you are not using addFieldError() correctly. Write "username" in first parameter instead of username. – Ankur Lathi Aug 28 '13 at 11:46
4

You are not correctly using addFieldError method. The first parameter in that method is a name of the field. In your case it should be "username".

addFieldError("username", "Username Empty Pls provide");
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143