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

Struts2 Validation

ec hurley
Greenhorn

Joined: Mar 11, 2005
Posts: 20
I am using Stuts 2 w/ Tiles 2. The validation is not working for me. I will post the code if anyone can help.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It'd make helping a little easier.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Hi,

I can also help you if you put your code.


And for Basic you need to learn.


First of all for Struts 2 validation your xml name must specify by your Action-validation.xml.

Every field in Xml file must with same jsp field name.

When your validation done your Action method not called so if you get contains data from Action class then make data list to global. and define in validate() method.

@Override
public void validate() {
call your method which retrieve your data.
}


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
ec hurley
Greenhorn

Joined: Mar 11, 2005
Posts: 20
Thanks for responding.

loginAction:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package va.fsc;

import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.apache.struts2.util.ServletContextAware;


/**
*
* @author ehurley
*/
public class loginAction extends ActionSupport
implements SessionAware, ServletRequestAware, ServletResponseAware, ServletContextAware
{
protected Logger log = LogManager.getLogger(va.fsc.loginAction.class);

private ServletContext sc;
private HttpServletRequest req;
private HttpServletResponse res;
private Map sessionMap;

private String username;
private String password;
private String chgpswd;
boolean isInvalidLogin = true;
String encrypted_password;


@Override
public String execute() throws Exception {

LOG.info("Login: execute() method is called, username = " + username);
if (isInvalid(getUsername())) return INPUT;
if (isInvalid(getPassword())) return INPUT;
return SUCCESS;
}

private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}

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 getChgpswd() {
return chgpswd;
}

public void setChgpswd(String chgpswd) {
this.chgpswd = chgpswd;
}

public void setServletRequest(HttpServletRequest req){
this.req = req;
}

public void setServletResponse(HttpServletResponse res){
this.res = res;
}

public void setServletContext(ServletContext sc){
this.sc = sc;
}

public void setSession(Map map){
this.sessionMap = map;
}

}

login-Action-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-validator type="stringthlength">
<param name="minLength">5</param>
<message>User name must be greater than (5) characters</message>
</field-validator>
</field>
<field name="password">
<field-validator type="requiredstring">
<message>Password is Required</message>
</field-validator>
</field>
</validators>

Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<package name="va.fsc" namespace="/" extends="tiles-default">

<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="index">
<result type="tiles">login</result>
</action>
<action name="login" class="va.fsc.loginAction" method="execute">
<interceptor-ref name="validation"/>
<result name="input" type="tiles">login</result>
<result name="chgpswd" type="tiles">chgpswd</result>
<result name="success" type="tiles">welcome</result>
</action>
<action name="chgpswd" class="va.fsc.chgpswdAction" method="execute">
<result name="input" type="tiles">chgpswd</result>
<result name="success" type="tiles">welcome</result>
</action>
<action name="expired" class="va.fsc.chgpswdAction" method="execute">
<result name="input" type="tiles">expired</result>
<result name="success" type="tiles">welcome</result>
</action>
<action name="visnlink">
<result type="tiles">fiscalyr</result>
</action>
<action name="fiscalyr" class="va.fsc.fiscalyrAction" method="execute">
<result name="success" type="tiles">finished</result>
</action>
<action name="logoff">
<result type="tiles">login</result>
</action>
</package>


</struts>
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Hi,

No need to change more in your code.

first you have to change your validatin xml file.

validatin xml file must same name as your action class.

so, change and make it to loginAction-Validation-xml.


every thing is perfect still if you face any problem then post again your reply and question.



Thanks,
Nishan Patel.
Java Daveloper
ec hurley
Greenhorn

Joined: Mar 11, 2005
Posts: 20
I solved my own problem. Once I brought tiles into the mix, I had to use programmatic validation. Thanks for making me think harder.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

XML validation works with Tiles as well.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Struts2 Validation