Problem with showing a variable in jsp with <s:property/>
Hi everybody as u might know i'm one more newbie with one more problem. The thing is, i'm doing a simple shopping cart and i can't show the results of my bussines logic. I use Interceptors, Struts 2, Hibernate and Jsp tech to do this. Here is my code:
books.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/gallery.dwt" codeOutsideHTMLIsLocked="false" -->
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Din bok</title>
<link href="indexKundMaster.css" rel="stylesheet" type="text/css" />
<link href="CSS/styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="kundvagn">
<s:include value="kundvagn.jsp"></s:include>
</div>
<s:actionerror/>
<s:iterator value="books" var="book">
<div class="books">
<div class="price">
Pris <s:property value="price"/> kr
</div>
<img style="border-width: 0px; height: 160px; width: 140px;"
src= <s:url value="images/books/%{ISBN}.jpg"/> />
<div id="button-read">
<s:a href="addBookToKundvagn?book.ISBN=%{ISBN}">
<img style="border-width: 0px; height: 35px; margin-left: -15px;"
src="images/lagg_i_kundvagn_green_two.gif"/>
</s:a>
</div>
<div id="lasMerTwo">
<s:a href="readMore?book.ISBN=%{ISBN}">
<img style="border-width: 0px; height: 35px; width: 95px;"
src="images/las_mer_two.gif"/>
</s:a>
</div>
</div>
</s:iterator>
</body>
</html><!-- InstanceEnd -->
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default" namespace="/">
<interceptors>
<interceptor-stack name="baseStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="interceptorCatcher"></interceptor-ref>
</interceptor-stack>
<interceptor name="interceptorCatcher"
class="se.fahme.interceptor.InterceptorCatcherFrom ActionClass"></interceptor>
</interceptors>
<default-interceptor-ref name="baseStack"></default-interceptor-ref>
<action name="addBookToKundvagn"
class="se.fahme.admin.action.BookAction" method="addBookToKundvagn">
<result>books.jsp</result>
</action>
</package>
</struts>
InterceptorAction.java
package se.fahme.admin.action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import se.fahme.domain.Book;
import se.fahme.domain.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class InterceptorAction extends ActionSupport implements SessionAware {
private static final long serialVersionUID = 7068776566954030256L;
private List<Book> books;
protected User user;
private ArrayList<Book> kundvagn;
private int booksQuantity;
protected Book book;
Map<String, Object> realsession = null;
public void saveBookInSession(){
ActionContext.getContext().getSession().put("books InSession", getBooks());
ActionContext.getContext().getSession().put("books InKundvagn", getKundvagn());
// ActionContext.getContext().getSession().put("books Quantity", getbooksQuantity());
setKundvagnElements();
}
@SuppressWarnings("unchecked")
public void getBookFromSession(){
setBooks((List <Book>)ActionContext.getContext().getSession().get ("booksInSession"));
setKundvagn((ArrayList<Book>) ActionContext.getContext().getSession().get("books InKundvagn"));
setKundvagnElements();
// System.out.println("Get books quantity from interceptor action"+getbooksQuantity());
}
public void setKundvagnElements(){
if(kundvagn != null){
//System.out.println("Set kundvagn elements->"+getKundvagn());
setbooksQuantity(kundvagn.size());
System.out.println("Books quantity-->"+booksQuantity);
setKundvagn(kundvagn);
// System.out.println("Setting kundvagn-->"+getKundvagn());
}
}
public void setSession(Map<String, Object> realSession) {
this.realsession = realSession;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public ArrayList<Book> getKundvagn() {
return kundvagn;
}
public void setKundvagn(ArrayList<Book> kundvagn) {
this.kundvagn = kundvagn;
}
public int getbooksQuantity() {
return booksQuantity;
}
public void setbooksQuantity(int booksQuantity) {
this.booksQuantity = booksQuantity;
}
public Book getBook() {
return book;
}
public void setBook(Book book) {
this.book = book;
}
}
InterceptorCatcherFromActionClass.java
package se.fahme.interceptor;
import se.fahme.admin.action.InterceptorAction;
import se.fahme.utilities.HibernateUtil;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterc eptor;
public class InterceptorCatcherFromActionClass extends AbstractInterceptor {
private static final long serialVersionUID = 410217198904230454L;
private InterceptorAction interceptorAction;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
HibernateUtil.init();
interceptorAction = (InterceptorAction) invocation.getAction();
interceptorAction.getUserFromSession();
interceptorAction.getBookFromSession();
invocation.invoke();
HibernateUtil.init();
interceptorAction.saveUserInSession();
interceptorAction.saveBookInSession();
return invocation.getResultCode();
}
}
BookAction.java
package se.fahme.admin.action;
import java.util.ArrayList;
import se.fahme.domain.Book;
import se.fahme.manager.BookManager;
import se.fahme.utilities.HibernateUtil;
public class BookAction extends InterceptorAction {
/**
*
*/
private static final long serialVersionUID = 1353206556533223861L;
// private Book book;
private int ISBN;
private BookManager linkController;
private String pageForwarding;
private String publicMessage;
private String category;
public BookAction() {
linkController = new BookManager();
}
public String addBookToKundvagn(){
int isbn = book.getISBN();
if(getBooks() != null){
for (Book printBook : getBooks()){
if(isbn == printBook.getISBN()){
if(getKundvagn() == null){
setKundvagn(new ArrayList<Book>());
}
setKundvagn(new ArrayList<Book>());
getKundvagn().add(printBook);
}
setKundvagnElements();
System.out.println("Getting books quantity from bookAction"+getbooksQuantity());
}
}
return SUCCESS;
}
kundvagn.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<link href="indexKundMaster.css" rel="stylesheet" type="text/css" />
<s:if test="booksQuantity == 0 | booksQuantity == null">
<div id="kundvagnTum"></div>
<h4 id="kundVagnTumH4">Din kundvagn är tum</h4>
</s:if>
<s:else>
<s:form action="tillKassan" method="post">
<input type="submit" name="button2" value="Till kassan" />
</s:form>
<h4>Din kundvagn innehåller</h4>
<s:property value="booksQuantity"/>
</s:else>
That's it.
Thing is, i'm only getting the if statement in the kundvagn.jsp and i'm hopping to get the else statement intead. I'm not getting a null book anywhere in my code. Can someone help me please?
I hope i posted in the right place
Greetings!!!
me