Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Use of validation interceptor with alias interceptor in struts2

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have implemented alias interceptor. Now when i implement the validation interceptor the validation works fine but the error message is not shown. The reason I understand is due to the difference in field name in the action and jsp pages, Request your assistance to fix.

The code listing is as given below....



The index.jsp file is
--------------------------



<%@ 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>Struts 2</title>
<s:head/>
</head>
<body>
<s:form action="hello">
<s:textfield label="User Name" name="username"/> <br/>
<s:textfield label="User password" name="userpwd"/> <br/>
<s:textfield label="Field 1" name="scrField1"/> <br/>
<s:textfield label="Field 2" name="scrField2"/> <br/>
<s:submit value="Say Hello"/>
</s:form>
</body>
</html>


====================================

The HelloAction is as given below
===================================


package org.tp.actions;

import com.opensymphony.xwork2.ActionSupport;

public class HelloAction extends ActionSupport{

private String name;
private String password;
private String field1;
private String field2;

public String getName() {
return name;
}

public void setName(String nm) {
this.name = nm;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getField1() {
System.out.println("inside HelloAction.getField1 field1 =="+field1);
return field1;
}

public void setField1(String scrField1) {

this.field1 = scrField1;
System.out.println("inside HelloAction.setField1 field1=="+field1);
}

public String getField2() {
System.out.println("inside HelloAction.getField2 field2 =="+field2);
return field2;
}

public void setField2(String scrField2) {
this.field2 = scrField2;
}

public String execute() throws Exception{
return "success";
}

}
==============================================
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>
<constant name="struts.devMode" value="true" />
<package name="helloworld" extends="struts-default">
<action name="hello" class="org.tp.actions.HelloAction" method="execute">

<param name="aliases">#{ 'username' : 'name','userpwd' : 'password', 'scrField1' : 'field1', 'scrField2' : 'field2'}</param>

<interceptor-ref name="alias">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>

<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>

<result name="success">/MainPage.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
============================================
HelloAction-validation is as given below
==============================
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
"http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">;
<validators>
<field name="name" >
<field-validator type="requiredstring">
<message >User Name is Required.</message>
</field-validator>
</field>

</validators>
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic