statements in the<logic:equal> tags are not evaluated

Discussion in 'Java' started by mahaa_hth, Oct 22, 2008.

  1. mahaa_hth

    mahaa_hth

    Joined:
    Oct 22, 2008
    Messages:
    4
    Location:
    india
    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">
    <html:eek:ption value="a">0-19</html:eek:ption>
    <html:eek:ption value="b">20-40</html:eek:ption>
    <html:eek:ption value="c">41-60</html:eek:ption>
    </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>

    <logic:present 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"/>
    </logic:present>
    </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
     
    mahaa_hth, Oct 22, 2008
    #1
    1. Advertising

  2. mahaa_hth

    mahaa_hth

    Joined:
    Oct 22, 2008
    Messages:
    4
    Location:
    india
    Please do reply at the earlist
     
    mahaa_hth, Oct 22, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Emilio
    Replies:
    0
    Views:
    827
    Emilio
    Jul 12, 2004
  2. Jeroen

    Struts <logic:equal>

    Jeroen, Jul 17, 2004, in forum: Java
    Replies:
    0
    Views:
    1,053
    Jeroen
    Jul 17, 2004
  3. TheVooDooChild

    Using MOD function in <logic:equal> tag

    TheVooDooChild, Feb 24, 2005, in forum: Java
    Replies:
    2
    Views:
    2,937
    Oscar kind
    Feb 24, 2005
  4. nick
    Replies:
    3
    Views:
    11,556
    Wendy Smoak
    Jun 1, 2005
  5. William Payne
    Replies:
    4
    Views:
    475
    Patrick Kowalzick
    Sep 9, 2003
Loading...

Share This Page