This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

How to use html:select tag in struts

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all!
i'm trying to fetch user ids in a <html:select> tag from database table. but i'm not able to do it. userId belongs to a bean named User. here is source code for it. plz tell me how to pull the record from a table to a html:select tag.

----------------------------------------------------------
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 <tml:select tag. How will I do that can somebody help me?

regards--
umar
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi umar,
u can use the options tag for this
<html:select property="userId">
<html ptions labelName="user" name="user" />
</html:select>
where "user" is session attribute name
Hope that solved your problem.
 
umar ali karimi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On doing so, the following error is generated

Cannot find bean under name org.apache.struts.taglib.html.BEAN
 
Thara Visu
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can i c whats in ur showusers.jsp?
 
umar ali karimi
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is showUsers.jsp code

<code>
<html:select property="userId">
<html ptionsCollection name="USERS_LIST" label="userId" value="userId"/>
</html:select>
</code>
 
Thara Visu
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umar
try this..

<html:select property="userId">
<html ptions name="USERS_LIST" labelName="USERS_LIST" />
</html:select>
i am assuming u have declared the html:select in a html:form with the correct formbean type.
 
My favorite is a chocolate cupcake with white frosting and tiny ad sprinkles.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic