JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Struts HTML Tag

Overview
  • The HTML tag library is mainly used to render the fields on input forms
  • Copy the TLD file, struts-html.tld, to the WEB-INF directory of your web application.
  • If you to use the Struts HTML Tags you need to include the following taglib directive
    in the jsp page.

    .<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
  • To use the Struts HTML Tag Library you need to add the following <taglib> subelement
    to the web.xml file.

    <taglib>
    <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-html.tld</tagliblocation>
    </taglib>


Example:
  • The dropdown values are stored in two ArrayList namely countryList and stateList
  • . We use HTML optionsCollection tag to display the values present in the ArrayList
  • The property attribute of the HTML optionsCollection tag holds the ArrayList.
  • The property attribute of the HTML select tag hold the country value selected by the user

index.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<jsp:forward page="inputAction.do?method=populate"/>


struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation/
/DTD Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts
/dtds/struts-config_1_2.dtd"
>


<struts-config>
    <form-beans>
        <form-bean name="InputForm" type="mypack.InputForm"/>
    
    </form-beans>
    
    
    <action-mappings>
        <action input="/input.jsp"
         parameter="method"
         name="InputForm"
         path="/inputAction"
         scope="session"
         type="mypack.InputAction">
            <forward name="success" path="/input.jsp" />
        </action>
    </action-mappings>
    
    
  
</struts-config>

input.jsp

<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
    <h3>Struts &lt;html:select&gt; Example</h3>
    <body>
        <html:form action="/inputAction" >
            <table>
                <tr>
                    <td>
                        Select Country :
                    </td>
                    <td>
                        <html:select property="country" >
                            <html:option value="0">Select Country
</html:option>

                            <html:optionsCollection name="InputForm"
property="countryList" label="countryName" value="countryId" />
                        </html:select>
                    </td>
                </tr>
                <tr>
                    <td>
                        Select State :
                    </td>
                    <td>
                        <html:select property="state" >
                            <html:option value="0">Select State
</html:option>

                            <html:optionsCollection
name=
"InputForm"
 
property="stateList" label="label" value="value" />
                        </html:select>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
                        <html:submit property="method"
 
value="save" />
                    </td>
                </tr>
            </table>
        </html:form>
    </body>
</html>

CountryData.java
package mypack;

public class CountryData {

  private String countryId;

  private String countryName;

  CountryData(String countryId, String countryName) {
    this.countryId = countryId;
    this.countryName = countryName;
  }

  public String getCountryId() {
    return countryId;
  }

  public void setCountryId(String countryId) {
    this.countryId = countryId;
  }

  public String getCountryName() {
    return countryName;
  }

  public void setCountryName(String countryName) {
    this.countryName = countryName;
  }

}

InputForm.java

package mypack;

import java.util.ArrayList;

public class InputForm extends
 
org.apache.struts.action.ActionForm {

  private String country;

  private String state;

  private ArrayList countryList;

  private ArrayList stateList;

  public String getCountry() {
    return country;
  }

  public void setCountry(String country) {
    this.country = country;
  }

  public String getState() {
    return state;
  }

  public void setState(String state) {
    this.state = state;
  }

  public ArrayList getCountryList() {
    return countryList;
  }

  public void setCountryList(ArrayList countryList) {
    this.countryList = countryList;
  }

  public ArrayList getStateList() {
    return stateList;
  }

  public void setStateList(ArrayList stateList) {
    this.stateList = stateList;
  }

}

InputAction.java

package mypack;

import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.util.LabelValueBean;


public class InputAction extends DispatchAction {
    
    private final static String SUCCESS = "success";
    
    public ActionForward populate
(
ActionMapping mapping, ActionForm  form,
            HttpServletRequest request,
 HttpServletResponse response
)
            throws Exception {

        ArrayList countryList = new ArrayList();
        ArrayList stateList = new ArrayList();
        InputForm inputForm = (InputFormform;

        countryList.add(new CountryData("1""USA"));
        countryList.add(new CountryData("2""Canada"));
        countryList.add(new CountryData("3""Mexico"));

        stateList.add(new LabelValueBean("New York""1"));
        stateList.add(new LabelValueBean("California""2"));
        stateList.add(new LabelValueBean("Los Angeles""3"));

        inputForm.setCountryList(countryList);
        inputForm.setStateList(stateList);

        return mapping.findForward(SUCCESS);
    }

    public ActionForward save(ActionMapping mapping,
 ActionForm  form,

            HttpServletRequest request,
HttpServletResponse response
)
            throws Exception {
        
        return mapping.findForward(SUCCESS);
    }
}

Output:
  • On executing the example the following page will be dispalyed to the user.


  • The country and the state dropdowns are populate with the values present in the array list.

Download Eclipse Code without lib

Download code with lib(WAR)

Example:-
  • Once the feedback is submitted by the user, the user will be forwarded to
    the success page.
  • Here the feedback submitted by the user will be displayed.
  •  We use Struts Bean Tag do to this.
  •  We use Struts Logic Equal Tag to display Male if the user has selected M
    for sex and Female if the user has selected F for sex.

feedback.jsp
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html>
    <body>
       <h3>Struts html tags Example</h3>
        <hr>
        <html:form action="/feedbackAction">
            <table>
                <tr>
                    <td>
                        Name :
                    </td>
                    <td>
                        <html:text name="feedbackForm"
 
property="name" />
                    </td>
                </tr>
                <tr>
                    <td>
                        Sex :
                    </td>
                    <td>
                        <html:radio name="feedbackForm"
 
property="sex" value="M" >
                            M
                        </html:radio>
                        <html:radio name="feedbackForm"
 
property="sex" value="F" >
                            F
                        </html:radio>
                    </td>
                </tr>
                
                <tr>
                    <td>
                        <html:submit />
                    </td>
                    <td>
                        <html:reset />
                    </td>
                </tr>
            </table>
        </html:form>
    </body>
</html>

success.jsp
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html>
    <body>
<h3>Struts html tags Example</h3>
        You have submitted the following feedback.
        <table>
            <tr>
                <td>
                    Name :
                </td>
                <td>
                    <bean:write name="feedbackForm"
 
property="name" />
                </td>
            </tr>
            <tr>
                <td>
                    Sex :
                </td>
                <td>
                    <logic:equal name="feedbackForm"
 
property="sex" value="M">
                        Male
                    </logic:equal>
                    <logic:equal name="feedbackForm"
property="sex" value="F">
                        Female
                    </logic:equal>
                </td>
            </tr>
            
            
        </table>
    </body>
</html>

FeedbackForm.java
package mypack;

public class FeedbackForm extends
org.apache.struts.action.ActionForm {

  private String name;

  private String sex;

  private String comments;

  public String getName() {
    return name;
  }

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

  public String getSex() {
    return sex;
  }

  public void setSex(String sex) {
    this.sex = sex;
  }

  public String getComments() {
    return comments;
  }

  public void setComments(String comments) {
    this.comments = comments;
  }

}

FeedbackAction.java

package mypack;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
public class FeedbackAction extends
 
org.apache.struts.action.Action {
    
    private final static String SUCCESS = "success";
    
    public ActionForward execute(ActionMapping
 mapping, ActionForm form,

            HttpServletRequest request,
 HttpServletResponse response
)
            throws Exception {
        
        return mapping.findForward(SUCCESS);
    }
}

struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD
 Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts
/dtds/struts-config_1_2.dtd"
>


<struts-config>
    <form-beans>
        <form-bean name="feedbackForm" 
        type="mypack.FeedbackForm"/>
    
    </form-beans>


    <action-mappings>
        <action 
         
        name="feedbackForm"
         path="/feedbackAction" 
         scope="session" 
         type="mypack.FeedbackAction">
        
            <forward name="success" path="/success.jsp" />
        </action>
    </action-mappings>
    
     
</struts-config>

Output:-
  • On executing the example the following page will be dispalyed to the user.

  • On submitting the feedback the following page will be dispalyed to the
    user.

Download Eclipse Code without lib

Download code with lib(WAR)

Struts Logic Tag 
  • If you to use the Struts Logic Tags you need to include the following taglib directive
    in the jsp page.

    .<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

  • To use the Struts Logic Tag Library you need to add the following <taglib> subelement
    to the web.xml file.

    <taglib>
    <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
     <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
    </taglib>

Example:-
  • In this example we will create a user.jsp page wich displays the user details according to
    the conditions specified using the logic tags
index.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<jsp:forward page="userAction.do"/>

Userform.jsp
package myform;

import java.util.ArrayList;

import org.apache.struts.action.ActionForm;

public class UserForm extends ActionForm {

  private String name;
  private int age;
  private float height;
  private float weight;
  private String favouriteFood;
  private ArrayList hobbies;

  public String getName() {
    return name;
  }

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

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public float getHeight() {
    return height;
  }

  public void setHeight(float height) {
    this.height = height;
  }

  public float getWeight() {
    return weight;
  }

  public void setWeight(float weight) {
    this.weight = weight;
  }

  public String getFavouriteFood() {
    return favouriteFood;
  }

  public void setFavouriteFood(String favouriteFood) {
    this.favouriteFood = favouriteFood;
  }

  public ArrayList getHobbies() {
    return hobbies;
  }

  public void setHobbies(ArrayList hobbies) {
    this.hobbies = hobbies;
  }

}

UserAction.java
package myaction;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import myform.UserForm;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class UserAction extends Action {
  
  
  public ActionForward execute(ActionMapping mapping,
 ActionForm form,

      HttpServletRequest request,
 HttpServletResponse response
)
      throws Exception {
    
    ArrayList hobbiesList = new ArrayList();
     UserForm userForm = (UserForm)form;
     userForm.setName("Javed");
     userForm.setAge(25);
     userForm.setHeight(5.10f);
     userForm.setWeight(80f);
     userForm.setFavouriteFood("Mutton and Fish");
     hobbiesList.add("Dancing");
     hobbiesList.add("Cricket");
     hobbiesList.add("Music");
     userForm.setHobbies(hobbiesList);
     return mapping.findForward("success");
    
  }

}

struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD
Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts/d
tds/struts-config_1_2.dtd"
>
<struts-config>
    <form-beans>
        <form-bean name="UserForm" type="myform.UserForm"/>    
    </form-beans>
    
    <global-exceptions>
    
    </global-exceptions>

    <global-forwards>
        <forward name="userAction"  path="/userAction.do"/>
    </global-forwards>

    <action-mappings>
        <action name="UserForm" path="/userAction"
 
scope="session" type="myaction.UserAction">
            <forward name="success" path="/user.jsp" />
        </action>
    </action-mappings>      
</struts-config>

user.jsp
<%@taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
    <head>
        <meta http-equiv="Content-Type"
 
content="text/html; charset=UTF-8">
        <title>User Details</title>
    </head>
    <body>
        <table>
            <tr>
                <td>
                    <logic:present name="UserForm"
 
property="name">
                        The User Name is Javed.
                    </logic:present>
                    <logic:notPresent name="UserForm"
property="name">
                        The User is not Present.
                    </logic:notPresent>
                </td>
            </tr>
            <tr>
                <td>
                    <logic:equal name="UserForm"
property="age" value="18">
                        The age is equal to 18.
                    </logic:equal>
                    <logic:notEqual name="UserForm"
property="age" value="18">
                        The age is not equal to 18.
                    </logic:notEqual>
                </td>
            </tr>
            <tr>
                <td>
                    <logic:greaterEqual name="UserForm"
 
property="height" value="5.10">
                        The height is greater
than or equal to 
5.10
                    </logic:greaterEqual>
                    <logic:greaterThan name="UserForm"
 
property="height" value="5.10">
                        The height is greater than 5.10
                    </logic:greaterThan>
                </td>
            </tr>
            <tr>
                <td>
                    <logic:lessEqual name="UserForm"
 
property="weight" value="60">
                        The weight is less
than or equal to 
60.
                    </logic:lessEqual>
                    <logic:lessThan name="UserForm"
 
property="weight" value="60">
                        The weight is less than 60.
                    </logic:lessThan>
                </td>
            </tr>
            <tr>
                <td>
                    <logic:match name="UserForm"
property=
"favouriteFood" value="Chicken">
                        The user's favourite
food includes Mutton.

                    </logic:match>
                    <logic:notMatch name="UserForm"
property=
"favouriteFood" value="Chicken">
                        The user's favourite
food does not includes Mutton.

                    </logic:notMatch>
                </td>
            </tr>
            <tr>
                <td>
                    The hobbies are
                    <logic:iterate name="UserForm"
property="hobbies" id="data">
                        <bean:write name="data" />
                    </logic:iterate>
                </td>
            </tr>
        </table>
    </body>
</html>

Output:-
  • On running the index.jsp he below page will be displayed to the user.

Download Eclipse Code without lib

Download code with lib(WAR)

Example:-<logic:iterate>

index.jsp
<%@taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>

<html>
    <body>
        <h3>Struts &lt;logic:iterate&gt; example</h3>
        <html:link action="PrintMsg">Begin</html:link>
    </body>
</html>
    

PrintMsgAction.java
package myaction;
 
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import myform.User;
 
public class PrintMsgAction extends Action{
 
  public ActionForward execute(ActionMapping
mapping,ActionForm form,

    HttpServletRequest request,
HttpServletResponse response

        throws Exception {
 
    List<String> listMsg = new ArrayList<String>();
    
    listMsg.add("Message A");
    listMsg.add("Message B");
    listMsg.add("Message C");
    listMsg.add("Message D");
    
    request.setAttribute("listMsg", listMsg);
    
    
    List<User> listUsers = new ArrayList<User>();
    
    listUsers.add(new User("user1""http://www.user1.com"));
    listUsers.add(new User("user2""http://www.user2.com"));
    listUsers.add(new User("user3""http://www.user3.com"));
    listUsers.add(new User("user4""http://www.user4.com"));
    
    request.setAttribute("listUsers", listUsers);
    
    return mapping.findForward("success");
  }
 
}

User.java
package mypack;
 
public class User{
 
  String username;
  String url;
  
  public User(String username, String url) {
    this.username = username;
    this.url = url;
  }
  
  public String getUsername() {
    return username;
  }
  public void setUsername(String username) {
    this.username = username;
  }
  public String getUrl() {
    return url;
  }
  public void setUrl(String url) {
    this.url = url;
  }
 
  
}

struts-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD
 Struts Configuration 1.2//EN"

          "http://jakarta.apache.org/struts/
dtds/struts-config_1_2.dtd"
>

<struts-config>

  <action-mappings>
  
    <action 
    input="/index.jsp"
     path="/PrintMsg" 
     type="myaction.PrintMsgAction"
    scope="session">
    
    <forward name="success" path="/pages/printMsg.jsp" />
    
    </action>
    
  </action-mappings>
  
</struts-config>


Output:-




Download Eclipse Code without lib

Download code with lib(WAR)