Hi all,
I am new to struts. I have written a struts-hibernate program to display records of DOG table. When I run the program only web page is displayed with titles and no error is shown. Please find my code below:
index.jsp:
<%@
page
language
=
"java"
contentType
=
"text/html; charset=ISO-8859-1"
pageEncoding
=
"ISO-8859-1"
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@
taglib
prefix
=
"s"
uri
=
"/struts-tags"
%>
<%@
page
import
=
"java.util.*;"
%>
ã<
html>
<
head
>
<
title>Pet Store</title>
</
head>
<
body>
<
h1>Pet Store</h1>
<
body>
<
h2> Dog</h2>
<table>
<tr>
<th>Id</th>
<th>Type</th>
<th>Age</th>
<th>Sex</th>
<th>Price</th>
<th>Select</th>
</tr>
<
s:iterator
value
=
"dogList"
var
=
"dog"
>
<tr>
<
td
><
s:property
value
=
"id"
/>
<s:property
value
=
"type"
/></
td
>
<
td
><
s:property
value
=
"age"
/></
td
>
<
td
><
s:property
value
=
"sex"
/></
td
>
<
td
><
s:property
value
=
"price"
/></
td
>
<
td
><
input
type
=
"checkbox"
value
=
"select"
></
td
>
</tr>
</s:iterator>
</table>
</
body
>
</
html
>
<?
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
>
<
constant
name
=
"struts.enable.DynamicMethodInvocation"
value
=
"false"
/>
<
constant
name
=
"struts.devMode"
value
=
"false"
/>
<
package
name
=
"default"
extends
=
"struts-default"
namespace
=
"/"
>
<
action
name
=
"display"
class
=
"com.petstore.view.PetStoreAction"
>
<
result
name
=
"success"
>
index.jsp
</
result
>
</action>
</package>
</
struts
>
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
web-app
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
xmlns:web
=
xsi:schemaLocation
=
id
=
"WebApp_ID"
version
=
"2.5"
>
<display-name>Struts2 Application</display-name>
<
filter>
<
filter-name>struts2</filter-name>
<!-- <filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class> -->
<
filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</
filter>
<
filter-mapping>
<
filter-name>struts2</filter-name>
<
url-pattern>/*</url-pattern>
</
filter-mapping>
<
welcome-file-list>
<
welcome-file>index.jsp</welcome-file>
</
welcome-file-list
>
</
web-app>
PetManger.java:
package com.petstore.controller;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import com.petstore.model.Dog;
import com.petstore.util.HibernateUtil;
public class PetManager extends HibernateUtil {
public List<Dog> getDogList() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Dog> dogList = null;
try {
dogList = (List<Dog>)session.createQuery("from Dog").list();
} catch (HibernateException e) {
e.printStackTrace();
session.getTransaction().rollback();
}
session.getTransaction().commit();
return dogList;
}
}
PetStoreAction.java:
package com.petstore.view;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.petstore.model.Dog;
import com.petstore.controller.PetManager;
public class PetStoreAction extends ActionSupport {
private Dog dog;
private List<Dog> dogList;
private Long id;
private PetManager manager;
public PetStoreAction() {
manager = new PetManager();
}
public String execute() {
this.dogList = manager.getDogList();
return SUCCESS;
}
public String display (){
this.dogList = manager.getDogList();
return SUCCESS;
}
}
Dog.java:
package com.petstore.model;
public class Dog {
private
int
id
;
private
String
type
;
private
String
sex
;
private
long
price
;
private
int
age
;
Dog (){}
public
void
setId(
int
i){
this
.
id
=i;
}
public
int
getid(){
return
id
;
}
public
void
setType(String dogType){
this
.
type
= dogType;
}
public
String getType(){
return
type
;
}
public
void
setSex( String dogSex){
this
.
sex
= dogSex;
}
public
String getSex(){
return
sex
;
}
public
void
setPrice(
long
dogPrice){
this
.
price
= dogPrice;
}
public
long
getPrice(){
return
price
;
}
public
void
setAge(
int
dogAge){
this
.
age
= dogAge;
}
public
int
getAge(){
return
age
;
}
}
HibernateUtil.java:
package com.petstore.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure()
.buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Any help would be appreciated.
Thanks.
Sailaja