permaculture playing cards*
The moose likes Struts and the fly likes How to initialize checkbox with a value from the DB? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to initialize checkbox with a value from the DB?" Watch "How to initialize checkbox with a value from the DB?" New topic
Author

How to initialize checkbox with a value from the DB?

mafe masilang
Greenhorn

Joined: Nov 25, 2007
Posts: 1
Hi, I need to make a checkbox (<html:checkbox> that initialize values from the database. If the user unchecks the box, and save it, newly checked values will be save in the db. Actually when i run the jsp, the values from the db is displayed in my tomcat, but the checkbox in my jsp is not checked.. can you somebody help me.. tnx in advance.

heres my actionInit page:

package com.pse.action;

import java.util.List;

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

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
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.pse.service.GroupModuleManager;
import com.pse.vo.GroupModuleVO;
import com.pse.vo.GroupVO;
import com.pse.actionform.AssignRolesToGroupsForm;

public class AssignRolesToGroupsActionInit extends Action {

private Logger logger = LogManager.getLogger(AssignRolesToGroupsActionInit.class);
private GroupModuleManager groupModuleManager;

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
) throws Exception {

String pGroupId = request.getParameter("groupId");
AssignRolesToGroupsForm argf = (AssignRolesToGroupsForm) form;

try{
/**
* To display groupName in 2nd page:
*/
GroupVO groupVO = groupModuleManager.getGroupInfo(pGroupId);

argf.setGroupId(groupVO.getGroupId());
argf.setName(groupVO.getName());

/**
* To diplay the variable names in a dropdown:
*/

System.out.println("Getting Modules...");
String pColumnName = "MODULE_ID";
List refList = groupModuleManager.getRefByColumn(pColumnName);
request.setAttribute("refList", refList);

/**
* To display the checkboxes
*/
String[] listOfDataStatus = new String[6];
//String[] listOfDBAccess = new String[6];

pColumnName = "DATA_STATUS";
System.out.println("Getting Data Status ...");
listOfDataStatus[0]="ENCODE";
listOfDataStatus[1]="REVIEW";
listOfDataStatus[2]="APPROVE";
listOfDataStatus[3]="AUTOREVIEW";
listOfDataStatus[4]="AUTOAPPROVE";
listOfDataStatus[5]="DATAVIEW";

System.out.println("Setting Data Status ...");

request.setAttribute("listOfDataStatus", listOfDataStatus);
String moduleId = "";

if ((request.getParameter("act")!=null)&&(request.getParameter("act")).equals("getDBValue")){
moduleId = argf.getModuleId();
System.out.println("moduleId="+moduleId);
System.out.println("pGroupId="+pGroupId);

GroupModuleVO gmVO = new GroupModuleVO();

gmVO = groupModuleManager.GetGroupWorkflowByModule(moduleId,pGroupId);
if (gmVO==null){System.out.println("gmVO is NULL");}else{System.out.println("gmVO is NOT NULL");}

System.out.println("okie");

argf.setGroupModuleAccessId(gmVO.getGroupModuleAccessId());

System.out.println("Encode; "+gmVO.getEncode());
System.out.println("Review: "+gmVO.getReview());
System.out.println("Approve: "+gmVO.getApprove());
System.out.println("AutoReview: "+gmVO.getAutoReview());
System.out.println("AutoApprove: "+gmVO.getAutoApprove());
System.out.println("DataView: "+gmVO.getDataView());

if (gmVO.getEncode().equals("1")){listOfDataStatus[0]="ENCODE";}
if (gmVO.getReview().equals("1")){listOfDataStatus[1]="REVIEW";}
if (gmVO.getApprove().equals("1")){listOfDataStatus[2]="APPROVE";}
if (gmVO.getAutoReview().equals("1")){listOfDataStatus[3]="AUTOREVIEW";}
if (gmVO.getAutoApprove().equals("1")){listOfDataStatus[4]="AUTOAPPROVE";}
if (gmVO.getDataView().equals("1")){listOfDataStatus[5]="DATAVIEW";}

System.out.println("SETTING SELECTED VALUES");
request.setAttribute("selectedAccessValues",listOfDataStatus);
System.out.println("Test Loop");
for (int i=0;i<listOfDataStatus.length;i++){
System.out.println("i="+i);
System.out.println("argf.selected="+listOfDataStatus[i]);
}

}else{
//GET VALUES FROM THE FORM
System.out.println("Selected Values: "+argf.getSelectedAccessValues().length);
System.out.println("............");
System.out.println("GET FORM VALUES");

for (int i=0;i<argf.getSelectedAccessValues().length;i++){
System.out.println("VALUES FROM THE FORM: "+argf.getSelectedAccessValues()[i]);
}
}
argf.setModuleId(moduleId);
argf.setGroupId(pGroupId);

logger.debug("going to success");
return mapping.findForward("success");

}catch(Exception ex){
ex.printStackTrace();
logger.debug("going to error");
return mapping.findForward("error");
}
}

public void setGroupModuleManager(GroupModuleManager groupModuleManager) {
this.groupModuleManager = groupModuleManager;
}
}

here's my jsp page:

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-bean-el" prefix="bean-el" %>
<%@ taglib uri="http://struts.apache.org/tags-html-el" prefix="html-el" %>
<%@ taglib uri="http://struts.apache.org/tags-logic-el" prefix="logic-el" %>

<%@ page import="java.util.*"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Assign Roles To Groups</title>
<link REL=stylesheet type="text/css" HREF=../copybook/stylesheet.css>
<script type="text/javascript">
function submitDoc(){
var x = 'AssignRolesToGroupsInit.do?act=getDBValue';
document.forms[0].action = x;
document.forms[0].submit();
}
</script>
</head>
<body>
<html-el:form action="/group/AssignRolesToGroups" method="post" >
<bean efine id="argf" name="AssignRolesToGroupsForm" type="com.pse.actionform.AssignRolesToGroupsForm"/>
<table>
<tr><td class="header2">ASSIGN ROLES TO GROUPS</td><td> </td></tr>
<tr><td class="txt_regular"><html-el:errors/></td><td> </td></tr>
</table>
<table border='1'>
<tr>
<td class="txt_regular">Group Name</td>
<td class="txt_regular">
<bean:write name="argf" property="name"/></td>
<html-el:hidden property="groupId"/>
</tr>
<tr>
<td class="txt_regular">
<b>Module Name</b>
</td>
<td class="txt_regular">
<html-el:select property="moduleId" onchange="submitDoc();">
<logic-el:iterate id="item" collection="${refList}" indexId="index" type="com.pse.vo.RefVO">
<html-el ption value='${item.variableName}'><bean:write name="item" property="value"/></html-el ption>
</logic-el:iterate>
</html-el:select>
</td>
</tr>
<logic-el:iterate id="item" collection="${listOfDataStatus}" property="selectedAccessValues" indexId="index" type="String">
<tr>
<td colspan='2' class="txt_regular">
<html-el:checkbox name="item" property="selectedAccessValues" value="${item.selectedAccessValues}"><bean:write name="item" /></html-el:checkbox></td>
</tr>
</logic-el:iterate>
<tr align='center'>
<td colspan='2' class="txt_regular">
<input type="submit" name="submitButton" value="Submit">
</td>
</tr>
</table>

<html-el:hidden property="encode"/>
<html-el:hidden property="review"/>
<html-el:hidden property="approve"/>
<html-el:hidden property="autoReview"/>
<html-el:hidden property="autoApprove"/>
<html-el:hidden property="dataView"/>
</html-el:form>

</body>
</html>
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
furbie bernie,

Please check your private messages for an important message from me. You can do so by clicking My Private Messages in the upper right corner of the page.


Merrill
Consultant, Sima Solutions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to initialize checkbox with a value from the DB?