JSP info page debug helper - java

Does anyone has uses jsp debug helper page that can be dropped into any webapp or included from within another jsp page to view header, req and session attributes?
can you please share if possible ? It will be a great help for many

Drop pageDebugger.jsp in you webapp
Access the page directly or include from another jsp like, layout.jsp to show this details on every page
This page Lists following in a table
Request Parameters
Page scoped attributes and values
Request scoped attributes and values
Session scoped attributes and values
Application scoped attributes and values
Request headers
All Spring beans defined in the scope
Option to search for a resource on the classpath
Dependency on spring framework is optional remove it if you dont use
pageDebugger.jsp
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<style type="text/css">
td {
word-wrap: break-word;
}
</style>
</head>
<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0"
style="table-layout: fixed;">
<colgroup>
<col width="500">
</colgroup>
<tr>
<th colspan="2">
<h3>attributes in $paramValues</h3>
</th>
</tr>
<c:forEach var="entry" items="${paramValues}">
<tr>
<td><c:out value="${entry.key}" /></td>
<td><c:forEach var="item" items="${entry.value}"
varStatus="status">
<pre><c:out value="${item}" /></pre>
<c:if test="${not status.last}">
<br />
</c:if>
</c:forEach></td>
</tr>
</c:forEach>
<tr>
<th colspan="2">
<h3>attributes in $requestScope</h3>
</th>
</tr>
<c:forEach var="entry" items="${requestScope}">
<tr>
<td><c:out value="${entry.key}" /></td>
<td><pre><c:out value="${entry.value}" /></pre></td>
</tr>
</c:forEach>
<tr>
<th colspan="2">
<h3>attributes in $sessionScope</h3>
</th>
</tr>
<c:forEach var="entry" items="${sessionScope}">
<tr>
<td><c:out value="${entry.key}" /></td>
<td><pre><c:out value="${entry.value}" /></pre></td>
</tr>
</c:forEach>
<tr>
<th colspan="2">
<h3>attributes in $pageScope</h3>
</th>
</tr>
<c:forEach var="entry" items="${pageScope}">
<tr>
<td><c:out value="${entry.key}" /></td>
<td><pre><c:out value="${entry.value}" /></pre></td>
</tr>
</c:forEach>
<tr>
<th colspan="2">
<h3>attributes in $headerValues</h3>
</th>
</tr>
<c:forEach var="entry" items="${headerValues}">
<tr>
<td><c:out value="${entry.key}" /></td>
<td><c:forEach var="item" items="${entry.value}"
varStatus="status">
<pre><c:out value="${item}" /></pre>
<c:if test="${not status.last}">
<br />
</c:if>
</c:forEach></td>
</tr>
</c:forEach>
<tr>
<th colspan="2">
<h3>attributes in $applicationScope</h3>
</th>
</tr>
<c:forEach var="entry" items="${applicationScope}">
<tr>
<td><c:out value="${entry.key}" /></td>
<td><pre><c:out value="${entry.value}" /></pre></td>
</tr>
</c:forEach>
<tr>
<th colspan="2">
<h3>System Properties</h3>
</th>
</tr>
<tr>
<th>Key</th>
<th>Value</th>
</tr>
<%#page import="java.util.Map"%>
<%#page import="java.util.Set"%>
<%#page import="java.util.Properties"%>
<%#page import="java.util.Arrays"%>
<%
Properties p = System.getProperties();
Set<Map.Entry<Object, Object>> set = p.entrySet();
for (Map.Entry<Object, Object> e : set) {
%>
<tr>
<td><%=e.getKey()%></td>
<td><%="".equals(e.getValue()) ? " " : e.getValue()%></td>
<%
}
%>
</tr>
<tr>
<th colspan="2">
<h3>Spring Beans</h3>
</th>
</tr>
<%#page import="org.springframework.web.context.WebApplicationContext"%>
<%#page
import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%#page import="org.springframework.core.io.Resource"%>
<%
try {
WebApplicationContext springContext = WebApplicationContextUtils
.getWebApplicationContext(config.getServletContext());
if (springContext != null) {
String[] beanNames = springContext.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
String className = springContext.getType(beanName)
.getName();
%>
<tr>
<td><%=beanName%></td>
<td><%=className%></td>
</tr>
<%
}
%>
<tr>
<th colspan="2">
<h3>Spring Resources</h3>
</th>
</tr>
<tr>
<th colspan="2">
<form><input name="resources" size="50"/></form>
</th>
</tr>
<%
String resourceNames = request.getParameter("resources");
if (resourceNames != null) {
Resource[] resources = springContext
.getResources(resourceNames);
for (Resource r : resources) {
%>
<tr>
<td><%=r.getFilename()%></td>
<td><%=r.getURI()%></td>
</tr>
<%
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>
Additionally if you have option to modify web.xml, have a look at these that can also be used to monitor / profile active http session attributes
http://code.google.com/p/sessionmon/
http://messadmin.sourceforge.net/

At work I have used JDeveloper to run an application in debug mode from the IDE. You can insert breakpoints into your .jsp and view all the debugging info. Although, I am not a big fan of jdeveloper, it is a nice feature of the IDE.

Related

input map value into another map name in jsp page

I want to generate tables in jsp page using data I got from Map in java. I use which use map value for define 'item'. But, The name of map value that I want to put on 'item' needs value from another map.
To be clear, here my jsp code;
<c:forEach var="wilayahProvinsi" items="${model.kodeKabupatenList}">
<div data-role="page" id="kode${wilayahProvinsi.ID2012}">
<div data-role="header" data-position="inline" data-fullscreen="true">
Back
<h1>${wilayahProvinsi.NAMA}</h1>
</div>
<div class="container">
<table id="example1" class="display nowrap" >
<thead>
<tr>
<th>Kode Komoditas</th>
<th>Jenis Barang</th>
<th>Nilai Konsumsi</th>
<th>Nilai Imputasi</th>
<th>Nilai Konsumsi Akhir</th>
<th>Persentase trhdp Total</th>
<th>Persentase trhdp Kelompok</th>
<th>Persentase trhdp Subkelompok</th>
<th>Edit</th>
<th>Delete</th>
</tr>
<tbody>
<!-- this is the problem -->
<c:forEach var="pekerjaanku" items="${model.pekerjaankuList$wilayahProvinsi.ID2012}">
<tr>
<td>${pekerjaanku.kode_komoditas}</td>
<td id="jenis_barang">${pekerjaanku.jenis_barang}</td>
<td style="text-align: right">${pekerjaanku.nilai_konsumsi}</td>
<td style="text-align: right">${pekerjaanku.nilai_imputasi}</td>
<td style="text-align: right">${pekerjaanku.nilai_konsumsi_akhir}</td>
<td style="text-align: right">${pekerjaanku.persentase_total}</td>
<td style="text-align: right">${pekerjaanku.persentase_kelompok}</td>
<td style="text-align: right">${pekerjaanku.persentase_subkelompok}</td>
<td><a href="kotaEdit?id=${pekerjaanku.kode_komoditas}" class="ui-btn ui-mini ui-corner-all ui-icon-edit
ui-btn-icon-left">Edit</a></td>
<td><a href="kotaDelete?id=${pekerjaanku.kode_komoditas}" class="ui-btn ui-mini ui-corner-all ui-icon-delete
ui-btn-icon-left" onclick="return confirm('Yakin ingin menghapus komoditas ${pekerjaanku.jenis_barang}?')">
Delete</a></td>
</tr>
</c:forEach>
</tbody>
</table>
<table>
<tr>
<td colspan="8">Tambah Komoditas Baru</td>
<td colspan="2">Download CSV</td>
</tr>
</table>
</div>
</div>
</c:forEach>
This is my Java code;
#RequestMapping("/jelajahPekerjaanKota")
public ModelAndView getPekerjaanKotaList() {
List<KodeKabupaten> kodeKabupatenList = usersService.getSpecifiedKodeKabupatenList();
Map<String, Object> model = new HashMap<String, Object>();
model.put("kodeKabupatenList", kodeKabupatenList);
for(KodeKabupaten kodeKabupaten : kodeKabupatenList){
model.put("pekerjaankuList" + kodeKabupaten.getID2012(), pekerjaankuService.getPekerjaankuList(kodeKabupaten.getID2012()));
}
return new ModelAndView("/provinsi/jelajahPekerjaanKota", "model", model);
}
To be clear, the objects in Map model is;
model.kodeKabupatenList which has attributes: NAMA, ID2012, PROV, and KAB
model.pekerjaankuList3171 which has attributes: kode_komoditas, jenis_barang, ...
model.pekerjaankuList3172 , the attributes are same with above
model.pekerjaankuList3173 , same with above
my problem is how to get all model.pekerjaankuList** in jsp with looping? I can not run this code in jsp;
<c:forEach var="pekerjaanku" items="${model.pekerjaankuList$wilayahProvinsi.ID2012}">

Struts2 actionError not displaying on jsp when action implemented ModelDriven

I have my Struts2 Action class as below and I am expecting the actionerror message should be displayed in my JSP page using the tag: <s:actionerror />
However the message did not show up, and I've found that if I change in the getModel() method return form; to return null;, the error message could be displayed again! How can I show the error message at the same time returning the form object in getModel() method?
public class StartSearchApplicationAction
extends ActionSupport
implements ModelDriven, SessionAware {
protected Map<String, Object> session;
private Formbean form;
public String execute() {
addActionError("Testing Error Message");
session.put("form", form);
return "success";
}
public Formbean getModel() {
form = (Formbean) session.get("form");
if (form == null) {
form = new Formbean();
}
return form;
}
public void setSession(Map<String, Object> session){
this.session = session;
}
}
Updated on 20-Oct-2015 - My JSP (it is the tiles template page)
Note that even I change the statement <s:if test='%{#session.hasError == "Y"}'> to <s:if test="hasActionErrors()">, the result is the same
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%# page language="java" %>
<%# page contentType="text/html; charset=UTF-8" %>
<%# page errorPage="/jsp/error.jsp" %>
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<html>
<head>
<title><s:text name="global.heading.title"/></title>
</head>
<body>
<table class="main" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="860" valign="top" bgcolor="#FFFFFF">
<table border="0" valign="top" align="left">
<s:if test='%{#session.hasError == "Y"}'>
<tr>
<table width="500" border="0" cellspacing="0" cellpadding="2" align="center" bgcolor="#006600">
<tr>
<td width="16" class="ErrorMessageBoxTitle"><img src="/<s:text name="global.system.root"/>/images/smessage.gif" width="16" height="14" align="absmiddle" alt="System Errors"></td>
</tr>
<tr>
<td colspan="2" class="FixTdSize">
<table width="496" border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
<tr>
<td align="center" class="FixTdSize">
<table border="0" cellspacing="12" cellpadding="0" width="480">
<tr>
<td width="35" class="ErrorMessageTitle"><img src="/<s:text name="global.system.root"/>/images/messager.gif" width="31" height="31" alt="System Errors"></td>
<td class="ErrorMessageTitle" width="409">&nbsp</td>
</tr>
<tr>
<td width="35" class="ErrorMessageBody"> </td>
<td class="label" width="409"><font color=red><s:actionerror/></font></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</tr>
<tr><td> </td></tr>
</s:if>
<tr>
<td height="30"><tiles:insertAttribute name="searchpanel"/></td>
</tr>
<tr>
<td><img src="/<s:text name="global.system.root"/>/images/line.gif" width="100%" height="2"></td>
</tr>
<tr>
<td><tiles:insertAttribute name="message"/></td>
</tr>
<tr>
<td><tiles:insertAttribute name="body"/></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</table>
<tiles:insertAttribute name="menu"/>
</body>
</html>
after investigation I finally did it with referencing to this question:
Passing parameters to action through ModelDriven in Struts 2
I think the reason is Modeldriven interceptor pushes the model on top of the value stack (i.e. 0-index) and thus the jsp could not access the actionError (the original 0-index was the action class).
Instead of using <s:actionerror/>, the page could able to display the actionError using <s:property value="[1].actionErrors[0]"/>, I am not sure whether this is a good approach but it serve the purpose.

how the solve the error Operation not allowed after ResultSet closed

I try to execute this code it will show the following error:
java.sql.SQLException.Operation not allowed after ResultSet closed
This is my code.in this code itself shows the above error..
<table cellspacing="0">
<%
try{
ResultSet rs1=st.executeQuery("select u.post_id,u.userid,u.post_txt from requestdetails as r inner join user_post as u on r.frdname=u.userid where r.userid='"+id+"'");
while(rs1.next()){
int post_id=rs1.getInt(1);
int fid=rs1.getInt(2);
System.out.println("iiii "+fid);
String text=rs1.getString(3);
System.out.println("txttt "+text);
ResultSet rs3=stat.executeQuery("select * from userdetails where userid='"+fid+"'");
if(rs3.next()){
String na=rs3.getString("username");
System.out.println("username "+na);
String img=rs3.getString("profilepic");
System.out.println("imgee "+img);
%>
<tr>
<td width="5%" style="padding-left:25;" rowspan="2"> <img src="images/<%=img%>" height="60" width="55"> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="3" style="padding:7;"><%=rs3.getString("username") %> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td></td>
<td colspan="3" style="padding-left:7;"><%=text %></td>
</tr>
<%
ResultSet rr=st.executeQuery("select * from comment_status where post_id='"+post_id+"'");
while(rr.next()){
int uid=rr.getInt("userid");
String cmt=rr.getString("comment");
ResultSet rrr=stat.executeQuery("select * from userdetails where userid='"+uid+"'");
if(rrr.next()){
%>
<tr>
<td> </td>
<td width="4%" bgcolor="#EDEFF4" style="padding-left:12;" rowspan="2"> <img src="" height="40" width="47"> </td>
<td bgcolor="#EDEFF4" style="padding-left:7;" > <%=rrr.getString("username") %> </td>
<td align="right" rowspan="2" bgcolor="#EDEFF4">
</tr>
<tr>
<td> </td>
<td bgcolor="#EDEFF4" style="padding-left:7;" colspan="2"><%=cmt %></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<%
}
}
%>
<tr>
<td> </td>
<td width="4%" style="padding-left:17;" bgcolor="#EDEFF4" rowspan="2"> <img src="images/" height="33" width="33"> </td>
<td bgcolor="#EDEFF4" colspan="2" style="padding-top:15;">
<form method="post" name="commenting" onSubmit="return blank_comment_check()" action="commentstatus.jsp">
<input type="text" name="comment_txt" placeholder="Write a comment..." maxlength="420" style="width:100px;" id="">
<input type="hidden" name="postid" value="<%=post_id%>">
<input type="hidden" name="userid" value="">
<input type="submit" name="comment" style="display:none;">
</form>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="4"align="right" style="border-top:outset; border-top-width:thin;"> </td>
</tr>
<%
}
}
}catch(Exception e){
e.printStackTrace();
}
%>
</table>
</div>
I don't know what mistake I've done in above code.
After I change the code like this, it's working fine...
Statement stt=con.createStatement();
ResultSet rs1=stt.executeQuery("select u.post_id,u.userid,u.post_txt from requestdetails as r inner join user_post as u on r.frdname=u.userid where r.userid='"+id+"'");
while(rs1.next()){
int post_id=rs1.getInt(1);
int fid=rs1.getInt(2);
System.out.println("iiii "+fid);
String text=rs1.getString(3);
System.out.println("txttt "+text);
Statement st1=con.createStatement();
ResultSet rs3=st1.executeQuery("select * from userdetails where userid='"+fid+"'");
if(rs3.next()){
String na=rs3.getString("username");
System.out.println("username "+na);
String img=rs3.getString("profilepic");
System.out.println("imgee "+img);
I made the mistake of using the same statement for all queries.
When I created different statements object for the solution it works fine.

unable to bind ModelMap through ajax call in Spring MVC

MY JSP PAGE:
<form action="manageparentexamforchildren.htm" class="form-horizontal form-bordered" >
<div class="form-body">
<div class="form-group">
<label class="control-label col-md-1"><spring:message code="label.parent.children"/></label>
<div class="col-md-3">
<select name="parent.children" class="form-control" id="children" required="true" >
<option value="">-<spring:message code="label.select"/>-</option>
<c:forEach items="${studentsOfThisParent}" var="student">
<option value="${student.id}">${student.firstName} ${student.lastName}</option>
</c:forEach>
</select>
</div>
</div>
</div>
<div class="form-actions fluid">
<div class="row">
<div class="col-md-12">
<div class="col-md-offset-3 col-md-9">
<button type="submit" class="btn blue"><i class="icon-ok"></i><spring:message code='button.search'/></button>
<button type="button" onclick="javascript:history.go(-1);" class="btn red default"><spring:message code='button.cancel'/></button>
</div>
</div>
</div>
</div>
</form>
<c:forEach items="${modelList}" var="examination">
<tr>
<td><b><spring:message code="label.examination.schoolexam"/></b>:<c:out value="${examination.schoolExam.name}" /></td>
</tr>
<table border="1" width="1000" height="200" class="table table-bordered table-hover">
<thead> <tr><th class="active"><spring:message code="label.examination.subjects"/></th><th class="active"><spring:message code="label.exam.Date"/></th><th class="active"><spring:message code="label.exam.maxmarks"/></th></tr></thead>
<tbody>
<c:forEach items="${examination.examinationSubjects}" var="examinationSubject" varStatus="status">
<tr>
<td class="success"><c:out value="${examinationSubject.subjects.name}" /></td>
<td class="success" ><c:out value="${examinationSubject.date}" /></td>
<td class="success"><c:out value="${examinationSubject.maxMarks}" /></td>
</tr>
</c:forEach>
</tbody>
</table>
</c:forEach>
MY CONTROLLER:
#RequestMapping(value="/manageparentexam.htm", method=RequestMethod.GET)
public String manageparentexam(ModelMap modelMap,HttpServletRequest request )
{
Parent parent = parentService.getParentById(getLoggedInUser().getId());
List <Student> studentsOfThisParent=parentService.getStudentsOfThisParent(getLoggedInUser().getId());
Student student=null;
student=parent.getStudentAdmission().get(0).getStudent();
modelMap.addAttribute("modelList",student.getStudentAdmission().getClasses().getExamination());
setupCreate(modelMap, request);
return "tileDefinition.manageparentexam-"+getNameSpace();
}
#RequestMapping(value="/manageparentexamforchildren.htm", method=RequestMethod.GET)
public void manageparentexamforchildren(ModelMap modelMap,HttpServletRequest request )
{
Long studentId=Long.valueOf(request.getParameter("parent.children"));
modelMap.addAttribute("modelList",studentService.getById(studentId).getStudentAdmission().getClasses().getExamination());
setupCreate(modelMap, request);
}
here my requirement is,when ever,i select a value in select box,then the table should get updated through ajax,here i am unable to bind the model list through ajax call . . .
What I would do is separate the table in another jsp. yourTable.jsp like this
<table border="1" width="1000" height="200" class="table table-bordered table-hover">
<thead> <tr><th class="active"><spring:message code="label.examination.subjects"/></th><th class="active"><spring:message code="label.exam.Date"/></th><th class="active"><spring:message code="label.exam.maxmarks"/></th></tr></thead>
<tbody>
<c:forEach items="${examination.examinationSubjects}" var="examinationSubject" varStatus="status">
<tr>
<td class="success"><c:out value="${examinationSubject.subjects.name}" /></td>
<td class="success" ><c:out value="${examinationSubject.date}" /></td>
<td class="success"><c:out value="${examinationSubject.maxMarks}" /></td>
</tr>
</c:forEach>
</tbody>
</table>
Then you include this jsp in your main mapge inside a div
<div id="yourTable">
<c:jsp include="yourTable.jsp/>
</div>
Then in your select you have to add a listener and when something has change you need to make a JQuery load lo reload the table rendering the table from the controller again with all the changes that you want
$('#yourTable').load("url of your controller where you will render yourTable.jsp");
And in the controller you will render yourTable.jsp and you will add in the ModelAndView object the examinationSubject

Again Neither BindingResult nor plain target object for bean name

Hi I am getiing the above exception : java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'Projet' available as request attribute. Please find my jsp and controller class.
The code I have is like below.
Controller code:
#Controller
#RequestMapping(value="/directeur")
public class AdminController {
#Autowired
private IAdmin directeur;
#RequestMapping(value="/index")
public String index(Model model)
{
model.addAttribute("Projet",new Projet());
model.addAttribute("Projets",directeur.getAllProjet());
return "Projets";
}
#RequestMapping(value="/saveProjet",method = RequestMethod.POST)
public String saveProjet(Projet p,BindingResult result,Model model) throws IOException
{
if(result.hasErrors())
{
model.addAttribute("Projets",directeur.getAllProjet());
return "Projets";
}
directeur.ajouterProjet(p);
model.addAttribute("Projet",new Projet());
model.addAttribute("Projets",directeur.getAllProjet());
return "Projets";
}
}
Jsp Code:
<%#taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%#taglib uri="http://www.springframework.org/tags/form" prefix="f"%>
<div id="formProjet">
<f:form modelAttribute="Projet" action="saveProjet" method="POST"
enctype="multipart/form-data">
<table>
<tr>
<td>ID Projet</td>
<td><f:input path="idProjet" /></td>
<td><f:errors path="idProjet" cssClass="errors"/></td>
</tr>
<tr>
<td>Nom</td>
<td><f:textarea path="nomProjet" /></td>
<td><f:errors path="nomProjet" cssClass="errors"/></td>
</tr>
<tr>
<td>Description du Projet</td>
<td><f:textarea path="description" /></td>
<td><f:errors path="description" cssClass="errors"/></td>
<tr><td><input type="submit" value="enregistrer"/></td></tr>
</table>
</f:form>
</div>
<div id="">
<table>
<tr>
<th>ID</th>
<th>Nom</th>
<th>Description</th>
<th>Photo</th>
</tr>
<c:forEach items="${Projets }" var="proj">
<tr>
<td>${proj.idProjet }</td>
<td>${proj.nomProjet }</td>
<td>${proj.description }</td>
<td></td>
</tr>
</c:forEach>
</table>
</div>

Categories

Resources