I faced with such problem. I write such jstl code:
<c:forEach var="package" items="${hotel.packages}">
<c:forEach var="product_item" items="${package.items}">
//some inputs and so on
</c:forEach>
</c:forEach>
my model class look like this:
Hotel class
private java.util.Set<com.acmecorp.acmeproject.model.catalog.hotel.HotelPackage> packages;
public java.util.Set<com.acmecorp.acmeproject.model.catalog.hotel.HotelPackage> getPackages() { /* compiled code */ }
public void setPackages(java.util.Set<com.acmecorp.acmeproject.model.catalog.hotel.HotelPackage> packages) { /* compiled code */ }
HotelPackage
public java.util.Set<com.acmecorp.acmeproject.model.catalog.hotel.HotelPackageItem> getItems() { /* compiled code */ }
public void setItems(java.util.Set<com.acmecorp.acmeproject.model.catalog.hotel.HotelPackageItem> items) { /* compiled code */ }
private java.util.Set<com.acmecorp.acmeproject.model.catalog.hotel.HotelPackageItem> items;
I'm geting this message
org.apache.jasper.JasperException: /WEB-INF/view/controls/hotelPackages/hotelPackagesView.jsp (line: 165, column: 24) "${package.items}" contains invalid expression(s): javax.el.ELException: Failed to parse the expression [${package.items}]
while trying to open my jsp.
line 164 it is this line <c:forEach var="product_item" items="${package.items}">
So, may be someone know what is the problem?
I believe it is because 'package' is a reserved Java keyword. Try renaming it to basically anything else.
Here is your answer!
your class Hotel
public class Hotel {
public java.util.HashSet<HotelPackage> packages;
public java.util.HashSet<HotelPackage> getPackages() {
return packages;
}
public void setPackages(java.util.HashSet<HotelPackage> packages) {
this.packages = packages;
}
}
your class
public class HotelPackage {
public java.util.HashSet<String> items;
public java.util.HashSet<String> getItems() {
return items;
}
public void setItems(java.util.HashSet<String> items) {
this.items = items;
}
}
your jstl code
<%
HashSet items = new HashSet();
Hotel hotel = new Hotel();
HashSet hotelPackSet = new HashSet();
HotelPackage hp1 = new HotelPackage();
HashSet items1 = new HashSet();
items1.add("Buffet");
items1.add("Luxury Rooms");
hotelPackSet.add(hp1);
HotelPackage hp2 = new HotelPackage();
HashSet items2 = new HashSet();
hp2.setItems(items2);
items2.add("Swimming Pool");
items2.add("Common Rooms");
hotelPackSet.add(hp1);
hotelPackSet.add(hp2);
hotel.setPackages(hotelPackSet);
%>
<c:set property="hotel" scope="page" value="<%= hotel%>" var="hotel"/>
<c:forEach var="pack" items="${hotel.packages}">
<c:set property="packItem" scope="page" value="${pack.items}" var="pack"/>
<c:forEach var="product_item" items="${pack}">
${product_item}
</c:forEach>
</c:forEach>
So you are doing a big mistake by not setting up var in set variable.Cheers :)
Related
I am developing one sample web application using JSP and Servlets, in this application i have set some object in the Servlets, i can retrieve that value in JSP by using request.getAttribute("Object"). Here i want to iterate that array of value in JSP. How can i achieve this any one help me.
My servlet code:
ArrayList<Performer> Performerobj=new ArrayList<Performer>();
ResultSet rst = stm1.executeQuery("some query");
while (rst.next())
{
Performer obj=new Performer();
obj.setProject(projectname);
obj.setCount(rst.getString("COUNT"));
obj.setDate(rst.getString("DATE"));
obj.setEmpid(rst.getString("empid"));
Performerobj.add(obj);
}
request.setAttribute("Performer", Performerobj);
Performer.java
public class Performer {
private String project;
private String empid;
private String date;
private String count;
public String getProject() {
return project;
}
public void setProject(String project) {
this.project = project;
}
/*setter and getter...... for all*/
Perform.jsp
<% List<Performer>obj1=List<Performer>)request.getAttribute("Performerobj"); %>
<script>
var obj=<%=obj1%>
for(obj object : list)
{
/*IS it correct way or how can i iterate*/
}
</script>
You can do that if you transform the ArrayList object into a JSON using a library like Jackson:
<% List<Performer>obj1 = (List<Performer>) request.getAttribute("Performerobj"); %>
<script>
var obj=<%=new ObjectMapper().writeValueAsString(obj1)%>;
for(obj object : list)
{
/*IS it correct way or how can i iterate*/
}
</script>
Another option is to use JSTL:
<c:forEach var="performer" items="${Performerobj}">
<c:out value="${performer.project}"/>
</c:forEach>
My question is: how to give dynamic param to an url using <s:itarator> in struts2?
I have some data stored in a MySQL database, I put the data (Id and Name) in two ArrayList
package Model;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class Film {
private String id;
private String titolo;
private String trama;
private int i=0;
private ArrayList<String> puntate = new ArrayList<String>();
private ArrayList<Integer> idPuntate = new ArrayList<Integer>();
protected String DRIVER = "com.mysql.jdbc.Driver";
protected String url = "jdbc:mysql://localhost/SitoWeb";
protected String user = "root";
protected String psw = "";
private Connection con;
public String execute(){
Connessione();
Query();
return "success";
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public String getId() {return id;}
public void setId(String id) {this.id = id;}
public String getTitolo(){return titolo;}
public void setTitolo(String titolo) {this.titolo=titolo;}
public String getTrama(){return trama;}
public void Connessione(){
try{
con=null;
Class.forName(DRIVER);
con = DriverManager.getConnection(url,user,psw);
}catch(Exception e){}
}
public void Query(){
try {
PreparedStatement cmd = con.prepareStatement("SELECT Film.Titolo, Film.Trama, Episodi.Nome, Episodi.idEpisodio FROM Film INNER JOIN Episodi ON Film.Id = Episodi.id_Film WHERE id = ?");
cmd.setString(1, id);
ResultSet rs = cmd.executeQuery();
while(rs.next()){
titolo = rs.getString("titolo");
trama = rs.getString("trama");
idPuntate.add(i, rs.getInt("idEpisodio"));
puntate.add(i,rs.getString("Nome"));
i++;
}
rs.close();
cmd.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public ArrayList<Integer> getIdPuntate() {
return idPuntate;
}
public void setIdPuntate(ArrayList<Integer> idPuntate) {
this.idPuntate = idPuntate;
}
public ArrayList<String> getPuntate() {
return puntate;
}
public void setPuntate(ArrayList<String> puntate) {
this.puntate = puntate;
}
}
Now I want to show these data in my JSP page. I tried something like this:
<s:url action="episodi" var="episodi">
<s:param name="id"></s:param> // I don't know what to put here...
</s:url>
<ol>
<s:iterator value="puntate">
<li><s:property/></li>
</s:iterator>
</ol>
I don't know what to put in <s:param name="id"></s:param> because the param is an ArrayList. I tried to put <s:iterator value="idPuntate"> but it returns 0..
There are two problems:
If you are generating an url for each element of a Collection, then you need to put the url generation inside the iteration of that Collection, otherwise you will have always the same url:
<ol>
<s:iterator value="puntate">
<s:url action="episodi" var="episodio">
<s:param name="id">something here</s:param>
</s:url>
<li><s:property/></li>
</s:iterator>
</ol>
You have not used OOP, that would suggest to create a bean named Movie (or Film), with a List<Episode>, and Episode should be another bean with for example Long id, Integer season, Integer number, and String description.
Since you've used two different lists, one for the episode description and another one for the episode id, and assuming they're aligned (it is not guaranteed, so I strongly suggest you to refactor your code the OOP way), you can access the IDs by their index basing on the episode description index, during the iteration:
<ol>
<s:iterator value="puntate" status="ctr">
<s:url action="episodi" var="episodio">
<s:param name="id">
<s:property value="idPuntate[%{#ctr.index}]"/>
</s:param>
</s:url>
<li><s:property/></li>
</s:iterator>
</ol>
P.S: Film should be a bean declared in your action, not the action itself... otherwise it is not portable... what if you need the Film class in other actions ?
I want to make a page which displays which Users of my website have the current "Role". Each users have a set of Roles, and a Role is affected to 0 ~ n users.
I'm displaying a basic list with Users's name, and a checkbox for each User. And then a submit button.
When the form is submitted, it will delete the Role from the selected Users.
Most of the time, it's working fine. But there is a problem if the DB' data changed between the page loading and the form submit.
If we are into this situation, it deletes the role from the wrong user!
I've tried different ways, and right now I'm using something like this :
import java.util.LinkedList;
import java.util.List;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
public class Test extends WebPage {
private static final long serialVersionUID = 1L;
/**
* An item used to link the checkbox to the Database Object
*/
public static class Item {
private boolean selected = false;
private int num;
public Item(final int num) { this.num = num; }
public final boolean isSelected() { return selected; }
public final int getNum() { return num; }
public final void setSelected(boolean selected) { this.selected = selected; }
public final void setNum(int num) { this.num = num; }
}
public Test(final PageParameters params) {
// This list will hold items we display
final List<Item> values = new LinkedList<Item>();
// Our stateless form
final StatelessForm<Void> form = new StatelessForm<Void>("form") {
private static final long serialVersionUID = 1L;
#Override
protected void onSubmit() {
// On submit, we check our items to see which ones are selected
String toDelete = "";
for (final Item it : values) {
if (it.selected) {
toDelete += it.getNum() + ", ";
}
}
System.out.println(toDelete);
}
};
add(form);
form.add(new Button("submit"));
// We retrieve our data from the Database
final List<Integer> things = getFromDatabase();
// We print them
final RepeatingView rp = new RepeatingView("li");
form.add(rp);
for (int num : things) {
final AbstractItem item = new AbstractItem(rp.newChildId());
rp.add(item);
// We create our holder
final Item it = new Item(num);
values.add(it);
item.add(new Label("name", num));
// We use the property of our holder as model
item.add(new CheckBox("checkbox", new PropertyModel<Boolean>(it, "selected")));
}
}
/**
* Simulate a fetch from the database by swapping our data at each requests
*/
public static int num = 0;
private static List<Integer> getFromDatabase() {
final List<Integer> list = new LinkedList<Integer>();
if (num++ %2 == 0) {
list.add(1);
list.add(2);
}
else {
list.add(2);
list.add(1);
}
return list;
}
}
and :
<body>
<form wicket:id="form">
<ul>
<li wicket:id="li">
<span wicket:id="name"></span>
<input type="checkbox" wicket:id="checkbox" />
</li>
</ul>
<input type="submit" valud="submit" wicket:id="submit" />
</form>
</body>
I'm simulating a changing DB by swapping values each time the page is requested.
And what happens is that if I select "1" and press submit, it says that it'll delete "2"!
I understand why, but I've no idea how to fix it.
The best way to fix it would be to have something like this:
<input type="checkbox" name="toDelete[]" value="<the_id>" />
<input type="checkbox" name="toDelete[]" value="<the_id>" />
etc.
But I've no idea how to do it with Wicket, and I can't find anything on the Internet =/
So yeah.. any examples / ideas?
I am trying to display a very simple cellTable on click of a button in my page.
However, the celltable is not getting rendered.
Giving below code snippets for more understanding:
preview.ui.xml file
<g:HTMLPanel>
<div class="{bundle.css.roundedBorder}">
<table style='width:100%;'>
<tr>
<td>
<c:CellTable pageSize='15' ui:field='cellTable' width="100%">
</c:CellTable>
</td>
</tr>
</table>
</div>
</g:HTMLPanel>
Correspoding Java Class:
public class Preview extends Composite {
.
.
. // other generic GWT code to bind UIBinder XML with this class
.
.
.
#UiField
CellTable<Contact> cellTable;
#UiHandler("button")
void handleClickOnSearchButton(ClickEvent e) {
cellTable = configureCellTable();
}
private CellTable<Contact> configureCellTable() {
// The list of data to display.
List<Contact> CONTACTS = Arrays.asList(new Contact("John", "123 Fourth Road"), new Contact("Mary", "222 Lancer Lane"), new Contact("Zander", "94 Road Street"));
// Create a CellTable.
CellTable<Contact> table = new CellTable<Contact>();
// Create name column.
TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
#Override
public String getValue(Contact contact) {
return contact.name;
}
};
// Create address column.
TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
#Override
public String getValue(Contact contact) {
return contact.address;
}
};
// Add the columns.
table.addColumn(nameColumn, "Name");
table.addColumn(addressColumn, "Address");
// Create a data provider.
ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();
// Connect the table to the data provider.
dataProvider.addDataDisplay(table);
// Add the data to the data provider, which automatically pushes it to the widget.
List<Contact> list = dataProvider.getList();
for (Contact contact : CONTACTS) {
list.add(contact);
}
return table;
}
private static class Contact {
private final String address;
private final String name;
public Contact(String name, String address) {
this.name = name;
this.address = address;
} } }
Please guide!
Thanks,
Akshay
You have two different cellTable objects. One is created by UiBinder and one is created in your configureCellTable Method.
Try to add a SimplePanel in your UiBinder file instead of the table:
<td>
<g:SimplePanel ui:field="simplePanel"/>
</td>
And in your Java code you add the table on it:
#UiField
SimplePanel simplePanel;
...
private CellTable<Contact> configureCellTable() {
...
simplePanel.add(table);
}
I'm winding throught this Yabe tutorial and have been happily get bugs and solving them on my own.... until now.
in
http://localhost:9000/#documentation/guide9
This is the part about customizable edit window..
For whatever reason, when I post a new message, via
http://localhost:9000/admin/new
I receive a null pointer around tags...
In /app/controllers/Admin.java (around line 48)
44: post.content = content;
45: post.tags.clear();
46: }
47: //Set tags list
48: for(String tag : tags.split("\\s+")){
49: if(tag.trim().length() > 0) {
50: post.tags.add(Tag.findOrCreateByName(tag));
51: }
52: }
53: // Validate
54: validation.valid(post);
I looked at Admin.java and Tag.java and compared them line for line with the samples and tests copy. The only difference is an inclusion of validation on aAdmin.java for what I imagine is some test scripts written later down the road..
Any ideas?
here is my admin...
package controllers;
import play.*;
import play.mvc.*;
import java.util.*;
import models.*;
#With(Secure.class)
public class Admin extends Controller {
#Before
static void setConnectedUser() {
if(Security.isConnected()) {
User user = User.find("byEmail", Security.connected()).first();
renderArgs.put("user", user.fullname);
}
}
public static void index() {
List<Post> posts = Post.find("author.email", Security.connected()).fetch();
render(posts);
}
public static void form(Long id) {
if(id != null) {
Post post = Post.findById(id);
render(post);
}
render();
}
public static void save(Long id, String title, String content, String tags) {
Post post;
if(id == null) {
// Create post
User author = User.find("byEmail", Security.connected()).first();
post = new Post(author, title, content);
} else {
// Retrieve post
post = Post.findById(id);
post.title = title;
post.content = content;
post.tags.clear();
}
//Set tags list
for(String tag : tags.split("\\s+")){
if(tag.trim().length() > 0) {
post.tags.add(Tag.findOrCreateByName(tag));
}
}
// Validate
validation.valid(post);
if(validation.hasErrors()) {
render("#form", post);
}
//Save
post.save();
index();
}
}
here is my tag.java
package models;
import java.util.*;
import javax.persistence.*;
import play.db.jpa.*;
import play.data.validation.*;
#Entity
public class Tag extends Model implements Comparable<Tag> {
#Required
public String name;
private Tag(String name) {
this.name = name;
}
public static Tag findOrCreateByName(String name) {
Tag tag = Tag.find("byName", name).first();
if(tag == null) {
tag = new Tag(name);
}
return tag;
}
public static List<Map> getCloud() {
List<Map> result = Tag.find(
"select new map(t.name as tag, count(p.id) as pound) from Post p join p.tags as t group by t.name"
).fetch();
return result;
}
public String toString() {
return name;
}
public int compareTo(Tag otherTag) {
return name.compareTo(otherTag.name);
}
}
In the form that calls the save() method you might be missing an input with name 'tags'. Something like:
<input id="tags" name="tags" type="text" value="" />
In the tutorial there is a template with:
<p>
#{field 'tags'}
<label>Enter some tags:</label>
<input type="text" size="50"
name="${field.name}" value="${post?.tags?.join(' ')}" />
#{/field}
</p>
Check that you have it.