I have a code that should display data on the page of the model
<% if (patients) { %>
<% patients.each { %>
<tr>
<td>${ ui.format(it.status) }</td>
<td align="center">
<% def linkClaim="patientView.page?patientId=' + ${patientId}+ '&claimUuid=" + ${it.uuid} %>
<button onclick="location.href='${linkClaim}'" type="button">Details</button>
</td>
</tr>
<% } %>
Data from the model:
patients
patientId
Error:
groovy.lang.MissingMethodException: No signature of method: SimpleTemplateScript171.$()
Related
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"> </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.
AI have this code:
fileOne.java:
...
em.persist(new Item("A1","B1","C1","A.jpg","abc"));
em.persist(new Item("A2","B2","C2","B.jpg","abc"));
em.persist(new Item("A3","B3","C3","C.jpg","abc"));
...
fileTwo.jsp:
#SuppressWarnings("unchecked")
List<Item> myList = (List<Item>)request.getAttribute("Item");
%>
<span style="font-size: 150%; color: black; text-decoration: underline;">List:</span>
<table id="Table" style="display:block;">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<% if(myList != null){
int counter = 0;
for (Item Citem: myList ) { %>
<tr>
<td id="viewA<%=counter%>" ><%= Citem.getA()%></td>
<td id="viewB<%=counter%>" ><%= Citem.getB()%></td>
<td id="viewC<%=counter%>" ><%= Citem.getC()%></td>
<td id="viewImage<%=counter%>"><img alt="itemimg" src="ItemsImage<%= Citem.getImageUrl() %>" width="52" height="52"></td>
</tr>
<% counter++;
}
}
else{
%> Empty;
<%}
%>
</table>
...
And I want to view the List<Item> in a table on my HTML page.
and I get only "Empty" in the page (and the header "List").
what am I doing wrong?
I think you should try it
JAVA code
List<Map> em = null;
em.add( new HashMap<String, String>(){{
put("A","A1");
put("B","B1");
put("C","C1");
put("url","url1 here");
put("msg","message1 here");
}});
em.add( new HashMap<String, String>(){{
put("A","A2");
put("B","B2");
put("C","C2");
put("url","url2 here");
put("msg","message2 here");
}});
em.add( new HashMap<String, String>(){{
put("A","A3");
put("B","B3");
put("C","C3");
put("url","url3 here");
put("msg","message3 here");
}});
jsp code
<table>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
</tr>
<c:forEach items="${Item}" var="subItem" varStatus="theCount">
<tr>
<td id="viewA<c:out value="${theCount.count}" />" ><c:out value="${subItem.A}" /></td>
<td id="viewB<c:out value="${theCount.count}" />" ><c:out value="${subItem.B}" /></td>
<td id="viewC<c:out value="${theCount.count}" />" ><c:out value="${subItem.C}" /></td>
<td id="viewImage<c:out value="${theCount.count}" />"><img alt="itemimg" src="<c:out value="${subItem.url}" />" width="52" height="52"></td>
</tr>
</c:forEach>
</table>
try it even you don't need to check for null if its null then it returns blanck table not throw any exception.
and add following taglib in jsp
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
If you are using the entity manager, you will have to write a finder method that returns a list. For an ex
Query query = em.createQuery("FROM Item item WHERE item.name = ?");
query.setParameter(1, "abc");
List<Item> items = query.getResultList();
and set the items to the request.
request.setAttribute("Items", items);
em.persist will just persist the object.
Please help me to solve a problem!
I have table created in JSP and filled with data from my DB, and the problem is - I have not idea how to access that JSP data from my controller.
For example - I need to pass appropriate ID (just String) to my controller from JSP to execute Delete method.
My jsp:
<body>
<form action="/editCategory" method="POST">
<h3>Existing categories</h3>
<%
List<Category> categories = (List<Category>) request.getAttribute("model");
if (categories != null) {
%>
<table border="1">
<tr>
<th width="24">ID</th>
<th width="80">Name</th>
<%--<th></th>--%>
</tr>
<%
for (Category category : categories) {
%>
<tr>
<td><%= category.getId() %> <% request.setAttribute("id", category.getId());%>
</td>
<td><%= category.getName() %>
</td>
<td>
<input type="submit" name="delete" value="Delete"/>
</td>
</tr>
<%
}
%>
</table>
<%
} else {
%>
<b>Categories list is empty :(</b>
<%
}
%>
</form>
</body>
My controller methods:
public Model getModel(HttpServletRequest req, HttpServletResponse resp) throws DBException
{
if (req.getParameter("submit")!=null){
addCategory(req);
}
if (req.getParameter("delete")!=null){
deleteCategory(req);
}
categories = categoryDAO.getCategories();
return new Model("/editCategory.jsp", categories);
}
private void deleteCategory(HttpServletRequest req) {
System.out.println(req.getAttribute("id") + " printed");
}
For now I want just to see that correct ID is taken!
Please help!
you could do it this way:
<%
for (Category category : categories) {
%>
<tr>
<td><%= category.getId() %>
<input type="hidden" name="allIds" value="<%= category.getId() %>" /></td>
<td><%= category.getName() %>
</td>
<td>
<input type="submit" name="delete" value="Delete"/>
</td>
</tr>
<%
}
%>
as the type says hidden is not visible.
after that read the values in your servlet with:
String[] lAllIds = request.getParameterValues("allIds");
I encountered this error while working on a web application for google app engine,through this tutorial.http://www.vogella.com/tutorials/GoogleAppEngineJava/article.html. Would like to hear more advice from you. Thank you.
HTTP ERROR 500
Problem accessing /. Reason:
Unable to compile class for JSP:
An error occurred at line: 34 in the jsp file: /TodoApplication.jsp
Type mismatch: cannot convert from List<Todo> to List<Todo>
31: if (user != null){
32: url = userService.createLogoutURL(request.getRequestURI());
33: urlLinktext = "Logout";
34: todos = dao.getTodos(user.getUserId());
35: }
36:
37: %>
Stacktrace:
Caused by:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 34 in the jsp file: /TodoApplication.jsp
Type mismatch: cannot convert from List<Todo> to List<Todo>
31: if (user != null){
32: url = userService.createLogoutURL(request.getRequestURI());
33: urlLinktext = "Logout";
34: todos = dao.getTodos(user.getUserId());
35: }
36:
37: %>
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<%# page import="java.util.List" %>
<%# page import="com.google.appengine.api.users.User" %>
<%# page import="com.google.appengine.api.users.UserService" %>
<%# page import="com.google.appengine.api.users.UserServiceFactory" %>
<%# page import="de.vogella.gae.java.todo.model.Todo" %>
<%# page import="de.vogella.gae.java.todo.dao.Dao" %>
TodoApplication.jsp
<!DOCTYPE html>
<%#page import="java.util.ArrayList"%>
<html>
<head>
<title>Todos</title>
<link rel="stylesheet" type="text/css" href="css/main.css"/>
<meta charset="utf-8">
</head>
<body>
<%
Dao dao = Dao.INSTANCE;
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
String url = userService.createLoginURL(request.getRequestURI());
String urlLinktext = "Login";
List<Todo> todos = new ArrayList<Todo>();
if (user != null){
url = userService.createLogoutURL(request.getRequestURI());
urlLinktext = "Logout";
todos = dao.getTodos(user.getUserId());
}
%>
<div style="width: 100%;">
<div class="line"></div>
<div class="topLine">
<div style="float: left;"><img src="images/todo.png" /></div>
<div style="float: left;" class="headline">Todos</div>
<div style="float: right;"><%=urlLinktext%> <%=(user==null? "" : user.getNickname())%></div>
</div>
</div>
<div style="clear: both;"/>
You have a total number of <%= todos.size() %> Todos.
<table>
<tr>
<th>Short description </th>
<th>Long Description</th>
<th>URL</th>
<th>Done</th>
</tr>
<% for (Todo todo : todos) {%>
<tr>
<td>
<%=todo.getShortDescription()%>
</td>
<td>
<%=todo.getLongDescription()%>
</td>
<td>
<%=todo.getUrl()%>
</td>
<td>
<a class="done" href="/done?id=<%=todo.getId()%>" >Done</a>
</td>
</tr>
<%}
%>
</table>
<hr />
<div class="main">
<div class="headline">New todo</div>
<% if (user != null){ %>
<form action="/new" method="post" accept-charset="utf-8">
<table>
<tr>
<td><label for="summary">Summary</label></td>
<td><input type="text" name="summary" id="summary" size="65"/></td>
</tr>
<tr>
<td valign="description"><label for="description">Description</label></td>
<td><textarea rows="4" cols="50" name="description" id="description"></textarea></td>
</tr>
<tr>
<td valign="top"><label for="url">URL</label></td>
<td><input type="url" name="url" id="url" size="65" /></td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" value="Create"/></td>
</tr>
</table>
</form>
<% }else{ %>
Please login with your Google account
<% } %>
</div>
</body>
</html>
I built and ran the very good Google App Engine Tutorial for Java (Todo list with JPA) with no problems at all. The platform was GAE SDK 1.8.9, Eclipse 3.8, OpenJDK 7u21 and Debian Testing (jessie). This result verifies the correctness of all the tutorial source code. Your problem is somewhere in your local installation.
Probably, the instance returned by your dao.getTodos method, is not from a class that implements java.util.List interface.
The line
34: todos = dao.getTodos(user.getUserId());
clobbers the reference to ArrayList assigned earlier to todos. Instead try
todos.addAll( dao.getTodos(user.getUserId()) );
Im getting a curious error with an ArrayList, below is the code (note that this code was copied from an online servlet example, I am a JAVA novice).
the JSP:
<%#page import="p.SecondExample"%>
<%# page language="java" import="java.util.*;"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Servlet Application</TITLE>
<script language="javascript">
function editRecord(id){
window.location.href="editServlet/"+id;
}
function deleteRecord(id){
window.location.href="deleteUser/"+id;
}
</script>
</HEAD>
<BODY>
<br>
<table align="center">
</table>
<br>
<table width="600px" align="center" style="background-color:#EDF6EA;border:1px solid #000000;">
<tr><td colspan=9 align="center" height="10px"></td></tr>
<tr><td colspan=9 align="center"><!-- Add New User--></td></tr>
<tr><td colspan=9 align="center" height="10px"></td></tr>
<tr style="background-color:#7BA88B;font-weight:bold;">
<td>Sector Segment</td><td>Color</td>
</tr>
<%
String bgcolor="";
int count=0;
List viewList = new ArrayList();
Iterator viewItr;
SecondExample se = new SecondExample();
se.doPost(request, response);
if(request.getAttribute("userList")!=null && request.getAttribute("userList")!="")
{
List userList = (ArrayList)request.getAttribute("userList");
Iterator itr = userList.iterator();
System.out.println(userList);
while(itr.hasNext())
{
if(count%2==0)
{
bgcolor = "#C8E2D1";
}
else
{
bgcolor = "#EAF8EF";
}
viewList = (ArrayList)itr.next();
int id = Integer.parseInt(viewList.get(0).toString());
viewItr = viewList.iterator();
%>
<tr style="background-color:<%=bgcolor%>;">
<%
while(viewItr.hasNext())
{
%>
<td><%=viewItr.next()%></td>
<%
}
count++;
%>
<td><input type="button" name="edit" value="Edit" style="background-color:#49743D;font-weight:bold;color:#ffffff;" onclick="editRecord(<%=id%>);" ></td>
<td><input type="button" name="delete" style="background-color:#ff0000;font-weight:bold;;color:#ffffff;" value="Delete" onclick="deleteRecord(<%=id%>);"></td>
</tr>
<%
}
}
if(count==0)
{
%>
<tr><td colspan="9" align="center"> </td></tr>
<tr><td colspan="9" align="center">No Record Avaliable</td></tr>
<%
}
%>
<tr><td colspan=9 align="center" height="2px"></td></tr>
</table>
</BODY>
</HTML>
in debug, the error appears to occur with:
viewList = (ArrayList)itr.next();
int id = Integer.parseInt(viewList.get(0).toString());
viewItr = viewList.iterator();
where my error appears as:
org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.String cannot be cast to java.util.ArrayList
I'm not quite sure why or how to make next a string. Any help is greatly appreciated.
Yep, itr.next() returns a String, which cannot be cast into an ArrayList.
String value = (String) itr.next();
int id = Integer.parseInt(value);
The following few lines of code in which you're iterating over your viewArray can be removed now, too.