java - Populating checkboxes in JSP page with data from a JavaBean -
i have jsp page checkboxes within html form below
now while editing user skill want take comma separated values table , populate checkboxes in jsp. next code brings csv skills database table.
list<userdetails> skills = new arraylist<userdetails>(); pstmt = (preparedstatement) conn.preparestatement(strsql); rs = pstmt.executequery(); string strskills = rs.getstring("skills"); list<string> items = arrays.aslist(strskills.split("\\s*,\\s*")); objuserdetails.setskills(items.toarray(new string[0])); skills.add(objuserdetails); homecoming skills;
now need populate checkboxes in jsp respective skills checked. used request getattribute()
method , passing jsp below
protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dbutil objdbutil = new dbutil(); list<userdetails> skills = objdbutil.getskills(); request.setattribute("arrskills", skills); requestdispatcher rqst = request.getrequestdispatcher("skills.jsp"); rqst.forward(request, response); }
how utilize skills got in arrskills array , populate check boxes. tried using
<c:foreach var="account" items="${arruserslist}"> <input type="checkbox" name="chkskills" id="chkphp" value="php"/>php <input type="checkbox" name="chkskills" id="chkjava" value="java"/>java <input type="checkbox" name="chkskills" id="chkmysql" value="mysql"/>mysql <input type="checkbox" name="chkskills" id="chkjavascript" value="javascript"/>javascript <input type="checkbox" name="chkskills" id="chkjquery" value="jquery"/>jquery <input type="checkbox" name="chkskills" id="chknode" value="node"/>node js </c:foreach>
but sure not right way utilize it.
examining example, here notes.
1) related skills variable. in java local variables, instance variables, , class variables convention written in camelcase with lowercase first letter.
read wikipedia article java naming conventions. see chapter 9 naming conventions code conventions java™ programming language.
so better:
list<userdetails> skills = new arraylist<userdetails>();
2) 1 time again related skills variable. named skills, although code clear skills one of properties of userdetails object. making assumption, userdetails class user skills? if yes, improve somehow reflect in class name, userskills, example. otherwise, if skills one of user details, better:
list<userdetails> userdetailslist = new arraylist<userdetails>();
again, highly recommended utilize meaningful variable names. read naming conventions above.
3) don't need cast preparedstatement when calling connection.preparestatement() method because returns preparedstatement object. do:
pstmt = conn.preparestatement(strsql);
as answer question, yes, of course of study can utilize <c:forloop>
jstl, illustration to iterate thru list of users , output related details each user. mutual practice.
it not clear question, allow me guess. in example, have finite list of skills, mean php, java, mysql, javascript, jquery, node.js, , each user want related check boxes checked if user has appropriate skill.
if above assumption right here 1 of possible solutions.
set attribute containing array or list of skills need.
considering fact list limited predefined values, can store list in servletcontext
, available whole application. improve set such global objects in class implements servletcontextlistener.
example: appcontextlistener.java:
package com.example.listener; import javax.servlet.servletcontextevent; import javax.servlet.servletcontextlistener; import javax.servlet.annotation.weblistener; @weblistener public class appcontextlistener implements servletcontextlistener { @override public void contextinitialized(servletcontextevent event) { string[] skills = {"php", "java", "mysql", "javascript", "jquery", "node.js"}; event.getservletcontext().setattribute("skills", skills); } @override public void contextdestroyed(servletcontextevent event) { } }
nb! in order receive these notification events (contextinitialized, contextdestroyed), implementation class must either declared in deployment descriptor of web application, annotated weblistener, or registered via 1 of addlistener methods defined on servletcontext. here used @weblistener
annotation.
i don't have time deepen architectural details, illustration assume there class user, contains user related info. among others contains property skills implemented map<string, boolean>
. has getters , setters (like public map<string, boolean> getskills()
, public void setskills(map<string, boolean> skills)
).
example: user.java
package com.example.model; import java.util.date; import java.util.map; public class user { private string fisrtname; private string lastname; private date birthday; ... private map<sting, boolean> skills; // getters , setters here }
, somewhere, in 1 of servlets processes info submitted thru html form in dopost() method, populate user skills , other details. (simplified example):
user user = new user(); // set user related info first name or ... // list of available skills servletcontext string[] skills = (string[]) getservletcontext().getattribute("skills"); map<string, boolean> userskills = new hashmap<string, boolean>(); // set appropriate skills (string skill: skills) { if (request.getparameter(skill) != null) { userskills.put(skill, true); } else { userskills.put(skill, false); } } ... // set user skills user.setskills(userskills); ...
this way don't hard code skills' names, otherwise this:
... map<string, boolean> userskills = new hashmap<string, boolean>(); if (request.getparameter("php") != null) { userskills.put("php", true); } else { userskills.put("php", false); } // same way java, mysql , others ...
in servlet, users as, example, list<user> users
, set attribute in request scope, request.setattribute("users", users)
store users list , forwards view (jsp page) output info related users.
simple illustration of jsp page outputs user related data: users.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!doctype html> <html> <head> <title>test page</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <%-- iterate thru users --%> <c:foreach items="${users}" var="user"> <!-- outputting different user related info --> ... <!-- outputting user skills --> <h3>user skills</h3> <%-- in within loop, iterate thru available skills, stored in servletcontext --%> <c:foreach items="${skills}" var="skill"> <c:choose> <%-- if user has appropriate skill, related checkbox checked. otherwise unchecked. --%> <%-- note: using skill key in map of user skills --%> <c:when test="${user.skills[skill] == true}"> <input type="checkbox" name="chkskills" value="${skill}" checked="checked">${skill} </c:when> <c:otherwise> <input type="checkbox" name="chkskills" value="${skill}">${skill} </c:otherwise> </c:choose> </c:foreach> </c:foreach> </body> </html>
or more compact variant using <c:if>
tag: users.jsp
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!doctype html> <html> <head> <title>test page</title> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <c:foreach items="${users}" var="user"> ... <h3>user skills</h3> <c:foreach items="${skills}" var="skill"> <%-- note: here <c:if> tag used right within <input> tag --%> <input type="checkbox" name="chkskills" value="${skill}" <c:if test="${user.skills[skill]}">checked="checked"</c:if>>${skill} </c:foreach> </c:foreach> </body> </html>
note: comment jsp specific parts of code within jsp, improve utilize jsp comments in form of <%-- comment --%>
. unlike html comments (in form of <!-- comment -->
), jsp comments not visible in resulting page send client.
hope help , give useful ideas.
java jsp servlets jstl javabeans
No comments:
Post a Comment