Hi all,
I am quite new to struts 1.1 and prtesently developing small struts application , which has three jsp pages and 1 actionform and 1 action class.
1) First see my intput jsp for getting input from users
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE></TITLE>
</HEAD>
<BODY>
<P>This is Input JSP</P>
<html:errors />
<html:form action="/input.do">
<bean:message key="mahesh.name" />
<html:text property="name"></html:text>
<BR>
<bean:message key="mahesh.address" />
<html:textarea property="address"></html:textarea>
<BR>
<bean:message key="mahesh.sex" />
<html:radio property="sex" value="M">Male</html:radio>
<html:radio property="sex" value="F">Female</html:radio>
<BR>
<bean:message key="mahesh.married" />
<html:checkbox property="marriage"></html:checkbox>
<br>
<bean:message key="mahesh.age" />
<html:select property="age">
<htmlption value="a">0-19</htmlption>
<htmlption value="b">20-40</htmlption>
<htmlption value="c">41-60</htmlption>
</html:select>
<html:submit>Enter</html:submit>
</html:form>
</BODY>
</html:html>
2) see my actionform named "InputForm.java"
package com.tbs.strutsweb.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* Form bean for a Struts application.
* Users may access 5 fields on this form:
* <ul>
* <li>address - [your comment here]
* <li>age - [your comment here]
* <li>name - [your comment here]
* <li>sex - [your comment here]
* <li>marriage - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class InputForm extends ActionForm {
private String address = null;
private String age = null;
private String name = null;
private String sex = null;
private String marriage = null;
/**
* Get address
* @return String
*/
public String getAddress() {
return address;
}
/**
* Set address
* @param <code>String</code>
*/
public void setAddress(String a) {
this.address = a;
}
/**
* Get name
* @return String
*/
public String getName() {
return name;
}
/**
* Set name
* @param <code>String</code>
*/
public void setName(String n) {
this.name = n;
}
/**
* Get sex
* @return String
*/
public String getSex() {
return sex;
}
/**
* Set sex
* @param <code>String</code>
*/
public void setSex(String s) {
this.sex = s;
}
/**
* Get marriage
* @return String
*/
public String getMarriage() {
return marriage;
}
/**
* Set marriage
* @param <code>String</code>
*/
public void setMarriage(String m) {
this.marriage = m;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// Reset values are provided as samples only. Change as appropriate.
address = null;
age = null;
name = null;
sex = null;
marriage = null;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.
if ((name == null) || (name.length() == 0))
{
errors.add("name", new org.apache.struts.action.ActionError("error.name.required"));
}
if ((address == null) || (address.length() == 0))
{
errors.add("address", new org.apache.struts.action.ActionError("error.address.required"));
}
if ((sex == null) || (sex.length() == 0))
{
errors.add("sex", new org.apache.struts.action.ActionError("error.sex.required"));
}
if ((marriage == null) || (marriage.length() == 0))
{
errors.add("marriage", new org.apache.struts.action.ActionError("error.marriage.required"));
}
return errors;
}
/**
* @return
*/
public String getAge() {
return age;
}
/**
* @param string
*/
public void setAge(String string) {
age = string;
}
}
3) see my actioinclass named " InputAction.java"
package com.tbs.strutsweb.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tbs.strutsweb.forms.InputForm;
/**
* @version 1.0
* @author
*/
public class InputAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
InputForm inputForm = (InputForm) form;
String name=inputForm.getName();
request.setAttribute("name",name.toUpperCase());
try {
if(inputForm.getAddress().length()<5)
{
errors.add("address", new ActionError("error.address.required"));
}
} catch (Exception e) {
// Report the error using the appropriate name and ID.
// errors.add("age", new ActionError("Invalid Age"));
}
// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.
if (!errors.isEmpty()) {
saveErrors(request, errors);
// Forward control to the appropriate 'failure' URI (change name as desired)
forward = mapping.findForward("failure");
} else {
// Forward control to the appropriate 'success' URI (change name as desired)
forward = mapping.findForward("success");
}
// Finish with
return (forward);
}
}
4) see my struts-cnfig.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- Data Sources -->
<data-sources>
</data-sources>
<!-- Form Beans -->
<form-beans>
<form-bean name="inputForm" type="com.tbs.strutsweb.forms.InputForm">
</form-bean>
</form-beans>
<!-- Global Exceptions -->
<global-exceptions>
</global-exceptions>
<!-- Global Forwards -->
<global-forwards>
</global-forwards>
<!-- Action Mappings -->
<action-mappings>
<action
name="inputForm"
path="/input"
scope="request"
type="com.tbs.strutsweb.actions.InputAction"
input="/input.jsp"
validate="true">
<forward name="success" path="/output.jsp">
</forward>
<forward name="failure" path="/actionerror.jsp">
</forward>
</action>
</action-mappings>
<!-- Message Resources -->
<message-resources parameter="com.tbs.strutsweb.resources.ApplicationResources"/>
</struts-config>
5) see my output.jsp .which shows the output to the users.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<HEAD>
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE>This is Output JSP</TITLE>
</HEAD>
<BODY>
<P>This is Ouput JSP</P>
<logicresent name="inputForm" property="age">Hello
<logic:equal name="inputForm" property="name" value="a">Young</logic:equal>
<logic:equal name="inputForm" property="name" value="b"> Adult </logic:equal>
<logic:equal name="inputForm" property="name" value="c">Old</logic:equal>
<bean:write name="name"/>
</logicresent>
</BODY>
</html:html>
*)
whenever ouput.jsp is called ,
statements in the
<logic:equal name="inputForm" property="name" value="a">Young</logic:equal>
<logic:equal name="inputForm" property="name" value="b"> Adult </logic:equal>
<logic:equal name="inputForm" property="name" value="c">Old</logic:equal> is not evaluated.
* suppose if i give name=sachin and select age=20-40 from input.jsp
the output should be "Hello Adult SACHIN"
but in my case it was "Hello SACHI" that is "Adult " is missing
*) <logic:equal > tags are not evaluated
please help me
i have been struggling this for past one week
Thanks people in advance
I am quite new to struts 1.1 and prtesently developing small struts application , which has three jsp pages and 1 actionform and 1 action class.
1) First see my intput jsp for getting input from users
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<HEAD>
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE></TITLE>
</HEAD>
<BODY>
<P>This is Input JSP</P>
<html:errors />
<html:form action="/input.do">
<bean:message key="mahesh.name" />
<html:text property="name"></html:text>
<BR>
<bean:message key="mahesh.address" />
<html:textarea property="address"></html:textarea>
<BR>
<bean:message key="mahesh.sex" />
<html:radio property="sex" value="M">Male</html:radio>
<html:radio property="sex" value="F">Female</html:radio>
<BR>
<bean:message key="mahesh.married" />
<html:checkbox property="marriage"></html:checkbox>
<br>
<bean:message key="mahesh.age" />
<html:select property="age">
<htmlption value="a">0-19</htmlption>
<htmlption value="b">20-40</htmlption>
<htmlption value="c">41-60</htmlption>
</html:select>
<html:submit>Enter</html:submit>
</html:form>
</BODY>
</html:html>
2) see my actionform named "InputForm.java"
package com.tbs.strutsweb.forms;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
/**
* Form bean for a Struts application.
* Users may access 5 fields on this form:
* <ul>
* <li>address - [your comment here]
* <li>age - [your comment here]
* <li>name - [your comment here]
* <li>sex - [your comment here]
* <li>marriage - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class InputForm extends ActionForm {
private String address = null;
private String age = null;
private String name = null;
private String sex = null;
private String marriage = null;
/**
* Get address
* @return String
*/
public String getAddress() {
return address;
}
/**
* Set address
* @param <code>String</code>
*/
public void setAddress(String a) {
this.address = a;
}
/**
* Get name
* @return String
*/
public String getName() {
return name;
}
/**
* Set name
* @param <code>String</code>
*/
public void setName(String n) {
this.name = n;
}
/**
* Get sex
* @return String
*/
public String getSex() {
return sex;
}
/**
* Set sex
* @param <code>String</code>
*/
public void setSex(String s) {
this.sex = s;
}
/**
* Get marriage
* @return String
*/
public String getMarriage() {
return marriage;
}
/**
* Set marriage
* @param <code>String</code>
*/
public void setMarriage(String m) {
this.marriage = m;
}
public void reset(ActionMapping mapping, HttpServletRequest request) {
// Reset values are provided as samples only. Change as appropriate.
address = null;
age = null;
name = null;
sex = null;
marriage = null;
}
public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
// Validate the fields in your form, adding
// adding each error to this.errors as found, e.g.
if ((name == null) || (name.length() == 0))
{
errors.add("name", new org.apache.struts.action.ActionError("error.name.required"));
}
if ((address == null) || (address.length() == 0))
{
errors.add("address", new org.apache.struts.action.ActionError("error.address.required"));
}
if ((sex == null) || (sex.length() == 0))
{
errors.add("sex", new org.apache.struts.action.ActionError("error.sex.required"));
}
if ((marriage == null) || (marriage.length() == 0))
{
errors.add("marriage", new org.apache.struts.action.ActionError("error.marriage.required"));
}
return errors;
}
/**
* @return
*/
public String getAge() {
return age;
}
/**
* @param string
*/
public void setAge(String string) {
age = string;
}
}
3) see my actioinclass named " InputAction.java"
package com.tbs.strutsweb.actions;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tbs.strutsweb.forms.InputForm;
/**
* @version 1.0
* @author
*/
public class InputAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
ActionErrors errors = new ActionErrors();
ActionForward forward = new ActionForward(); // return value
InputForm inputForm = (InputForm) form;
String name=inputForm.getName();
request.setAttribute("name",name.toUpperCase());
try {
if(inputForm.getAddress().length()<5)
{
errors.add("address", new ActionError("error.address.required"));
}
} catch (Exception e) {
// Report the error using the appropriate name and ID.
// errors.add("age", new ActionError("Invalid Age"));
}
// If a message is required, save the specified key(s)
// into the request for use by the <struts:errors> tag.
if (!errors.isEmpty()) {
saveErrors(request, errors);
// Forward control to the appropriate 'failure' URI (change name as desired)
forward = mapping.findForward("failure");
} else {
// Forward control to the appropriate 'success' URI (change name as desired)
forward = mapping.findForward("success");
}
// Finish with
return (forward);
}
}
4) see my struts-cnfig.xml file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<!-- Data Sources -->
<data-sources>
</data-sources>
<!-- Form Beans -->
<form-beans>
<form-bean name="inputForm" type="com.tbs.strutsweb.forms.InputForm">
</form-bean>
</form-beans>
<!-- Global Exceptions -->
<global-exceptions>
</global-exceptions>
<!-- Global Forwards -->
<global-forwards>
</global-forwards>
<!-- Action Mappings -->
<action-mappings>
<action
name="inputForm"
path="/input"
scope="request"
type="com.tbs.strutsweb.actions.InputAction"
input="/input.jsp"
validate="true">
<forward name="success" path="/output.jsp">
</forward>
<forward name="failure" path="/actionerror.jsp">
</forward>
</action>
</action-mappings>
<!-- Message Resources -->
<message-resources parameter="com.tbs.strutsweb.resources.ApplicationResources"/>
</struts-config>
5) see my output.jsp .which shows the output to the users.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<HEAD>
<%@ page language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
%>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="theme/Master.css" rel="stylesheet" type="text/css">
<TITLE>This is Output JSP</TITLE>
</HEAD>
<BODY>
<P>This is Ouput JSP</P>
<logicresent name="inputForm" property="age">Hello
<logic:equal name="inputForm" property="name" value="a">Young</logic:equal>
<logic:equal name="inputForm" property="name" value="b"> Adult </logic:equal>
<logic:equal name="inputForm" property="name" value="c">Old</logic:equal>
<bean:write name="name"/>
</logicresent>
</BODY>
</html:html>
*)
whenever ouput.jsp is called ,
statements in the
<logic:equal name="inputForm" property="name" value="a">Young</logic:equal>
<logic:equal name="inputForm" property="name" value="b"> Adult </logic:equal>
<logic:equal name="inputForm" property="name" value="c">Old</logic:equal> is not evaluated.
* suppose if i give name=sachin and select age=20-40 from input.jsp
the output should be "Hello Adult SACHIN"
but in my case it was "Hello SACHI" that is "Adult " is missing
*) <logic:equal > tags are not evaluated
please help me
i have been struggling this for past one week
Thanks people in advance