Discussions

Web tier: servlets, JSP, Web frameworks: How to use <html:select> tag in struts

  1. How to use <html:select> tag in struts (3 messages)

    welcome.jsp
    ----------------------------------------------------------

    String userId = (String)session.getAttribute("user");
    Welcome <%=userId%>
    <br/>
    a href="/usersListAction.do" class="menu"> users list
    -->
    -----------------------------------------------------------
    This file forwards the request to /userListAction which takes a parameter as 'usersList'.
    -----------------------------------------------------------
    struts-config.xml file
    -----------------------------------------------------------
    ?xml version="1.0"
    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
      form-beans
        form-bean name="userBean" type="com.als.skilleval.web.form.UserForm"
      /form-beans
      global-exceptions
      /global-exceptions
      global-forwards
        forward name="welcome" path="/pages/Welcome.jsp" redirect="true"
      /global-forwards>
      action-mappings
        action path="/logonAction" name="userBean" type="com.als.skilleval.web.action.UserAction" validate="false" parameter="logon"
          forward name="success" redirect="true" path="/pages/jsp/welcome.jsp"/
          forward name="failure" redirect="true" path="/index.jsp"/
        /action
        action path="/usersListAction" name="userBean" type="com.als.skilleval.web.action.UserAction" validate="false" parameter="usersList"
          forward name="success" redirect="true" path="/pages/jsp/showUsers.jsp"/
        /action
      /action-mappings
      controller/
    /struts-config
    -->
    -----------------------------------------------------------
    the user beans in package com.als.skilleval.business.user
    -----------------------------------------------------------

    File: User.java
    -----------------------------------------------------------
    package com.als.skilleval.business.user;

    import java.io.Serializable;
    import java.util.ArrayList;

    /**
     * @author Als
     *
     */
    public class User extends Person implements Serializable {

    private String userId;
    private String password;
    private String highestQualification;
    private String skill;

    /**
    * @return Returns the highestQualification.
    */
    public String getHighestQualification() {
    return highestQualification;
    }

    /**
    * @param highestQualification The highestQualification to set.
    */
    public void setHighestQualification(String highestQualification) {
    this.highestQualification = highestQualification;
    }

    /**
    * @return Returns the password.
    */
    public String getPassword() {
    return password;
    }

    /**
    * @param password The password to set.
    */
    public void setPassword(String password) {
    this.password = password;
    }

    /**
    * @return Returns the skill.
    */
    public String getSkill() {
    return skill;
    }

    /**
    * @param skill The skill to set.
    */
    public void setSkill(String skill) {
    this.skill = skill;
    }

    /**
    * @return Returns the userId.
    */
    public String getUserId() {
    return userId;
    }

    /**
    * @param userId The userId to set.
    */
    public void setUserId(String userId) {
    this.userId = userId;
    }
    }
    -----------------------------------------------------------
    UserHelper.java
    -----------------------------------------------------------
    package com.als.skilleval.business.user;

    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.ArrayList;

    import com.als.skilleval.connectivity.DBConnectionManager;

    /**
     * @author Als
     *
     */
    public class UserHelper {

    private UserHelper(){

    }

    private static UserHelper c_Instance;

    static{
    c_Instance = new UserHelper();
    }

    public static UserHelper getInstance(){
    return c_Instance;
    }

    public ArrayList usersLogonList(){

    ArrayList usersList = new ArrayList();
    try{
    PreparedStatement pstmt =
    DBConnectionManager.getInstance()
    .getConnection()
    .prepareStatement("SELECT USERID, PASSWORD FROM user_info");
    ResultSet rs = pstmt.executeQuery();

    while(rs.next()){
    User user = new User();
    user.setUserId(rs.getString(1));
    user.setPassword(rs.getString(2));
    usersList.add(user);
    }

    } catch(SQLException ex){
    ex.printStackTrace();
    }

    return usersList;
    }
    }
    -----------------------------------------------------------
    File: UserClient.java
    -----------------------------------------------------------
    package com.als.skilleval.business.user;

    import java.util.ArrayList;

    /**
     * @author Als
     *
     */
    public class UserClient {

    private UserClient(){

    }

    private static UserClient c_Instance;

    static{
    c_Instance = new UserClient();
    }

    public static UserClient getInstance(){
    return c_Instance;
    }

    public ArrayList getUsersLogonList(){
    return UserHelper.getInstance().usersLogonList();
    }

    public User getUser(String aUserId, String aPassword){
    return UserHelper.getInstance().getUser(aUserId,aPassword);
    }
    }
    -----------------------------------------------------------
    files in package com.als.skilleval.web are:
    -----------------------------------------------------------
    File: UserAction.java
    -----------------------------------------------------------
    package com.als.skilleval.web.action;

    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Iterator;

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

    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 com.als.skilleval.business.user.User;
    import com.als.skilleval.business.user.UserClient;
    import com.als.skilleval.web.form.UserForm;

    /**
     * @author Als
     *
     */
    public class UserAction extends Action {

    /*
    * (non-Javadoc)
    *
    * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
    * org.apache.struts.action.ActionForm,
    * javax.servlet.http.HttpServletRequest,
    * javax.servlet.http.HttpServletResponse)
    */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {
    // TODO Auto-generated method stub
    ActionForward forward = null;

    String param = mapping.getParameter();
    if (param.intern() == "usersList") {
    forward = executeUsersList(mapping, form, request, response);
    } else if (param.intern() == "logon") {
    UserForm userform = (UserForm) form;
    String userId = userform.getUserId();
    String password = userform.getPassword();
    if(userId != null && password != null){
    forward = executeLogon(mapping,form,request,response,userId,password);
    }
    }
    return forward;
    }

    private ActionForward executeUsersList(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws IOException, ServletException {
    Iterator userIter = UserClient.getInstance().getUsersLogonList().iterator();
    int size = UserClient.getInstance().getUsersLogonList().size();
    User [] arrUser = new User[size];
    String [] usersId = new String[size];
    Integer userSz = new Integer(size);
    int count = 0;
    while(userIter.hasNext()){
    User user = (User)userIter.next();
    usersId[count] = (user.getUserId());
    count++;
    }
    HttpSession session = request.getSession();
    session.setAttribute("USERS_LIST", usersId);
    session.setAttribute("sz", userSz);
    return mapping.findForward("success");
    }

    private ActionForward executeLogon(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response,
    String aUserId, String aPassword) throws IOException,
    ServletException {
    HttpSession session = request.getSession();

    Iterator iter = UserClient.getInstance().getUsersLogonList().iterator();
    boolean flag = false;
    while(iter.hasNext()){
    User user = (User)iter.next();
    if(user.getUserId().equals(aUserId) && user.getPassword().equals(aPassword)){
    flag = true;
    break;
    }
    }

    if(flag == true){
    session.setAttribute("user", aUserId);
    return mapping.findForward("success");
    }
    else{
    return mapping.findForward("failure");
    }
    }
    }
    -----------------------------------------------------------
    File : UserForm.java
    package com.als.skilleval.web.form;

    import java.util.ArrayList;

    /**
     * @author Als
     *
     */
    public class UserForm extends ActionForm {
    private String userId;
    private String password;
    private String highestQualification;
    private String skill;
    /**
    * @return Returns the highestQualification.
    */
    public String getHighestQualification() {
    return highestQualification;
    }

    /**
    * @param highestQualification The highestQualification to set.
    */
    public void setHighestQualification(String highestQualification) {
    this.highestQualification = highestQualification;
    }

    /**
    * @return Returns the password.
    */
    public String getPassword() {
    return password;
    }

    /**
    * @param password The password to set.
    */
    public void setPassword(String password) {
    this.password = password;
    }

    /**
    * @return Returns the skill.
    */
    public String getSkill() {
    return skill;
    }

    /**
    * @param skill The skill to set.
    */
    public void setSkill(String skill) {
    this.skill = skill;
    }

    /**
    * @return Returns the userId.
    */
    public String getUserId() {
    return userId;
    }

    /**
    * @param userId The userId to set.
    */
    public void setUserId(String userId) {
    this.userId = userId;
    }

    }

    -----------------------------------------------------------

    In my UserAction.java file, I've a function called 'executeUsersList()' which is responsible for fetching userIds from database and displaying them on a page named showUsers.jsp. I want to display them in a <html:select> tag. How will I do that can somebody help me?

    regards--
    uamr
  2. first, add the struts html ctl to your jsp using the taglib entry at the begining of the file. Then, where you want to insert the select box add
    <code>
    <html:select property="userId" >
      <html:optionsCollection name="USERS_LIST" label="userId" value="userId"/>
    </html:select>
    </code>
  3. On doing so, the following exception occurs

    Cannot find bean under name org.apache.struts.taglib.html.BEAN
  4. Did you add the struts-html.tld file to your WEB-INF directory? Did you add an entry for it in your web.xml file?