How to view data in struts2? - java

I am trying to view data from databases from jsp so now I come up with idea by without any scriplet or Java code in jsp .I have learned a modeldriven and some interceptors to use in struts.xml but I don't know how to implement it ?so could some one guide and help to go further am new to strut2 world
Beantest:
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getBook()
{
return Book;
}
public void setBook(String book)
{
Book = book;
}
public String getAuthor()
{
return Author;
}
public void setAuthor(String author)
{
Author = author;
}
public String getAvailbleqty()
{
return Availbleqty;
}
public void setAvailbleqty(String availbleqty)
{
Availbleqty = availbleqty;
}
public String getCategory()
{
return Category;
}
public void setCategory(String category)
{
Category = category;
}
DataAction.java:
public List<Beantest> viewbook()
{
List<Beantest> al=new ArrayList<Beantest>();
Beantest bt = new Beantest();
try
{
String sql = "select * from Bookavaible";
Statement stmt;
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
while (rs.next())
{
bt.setId(rs.getString("id"));
bt.setBook(rs.getString("Book"));
bt.setAuthor(rs.getString("Author"));
bt.setAvailbleqty(rs.getString("Availbleqty"));
bt.setCategory(rs.getString("Category"));
al.add(bt);
}
}
catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return al;
}
Actiontest.java
public class ActionTest {
Beantest bt;
private List<Beantest> beans;
public String viewbookaction()
{
DataAction da = new DataAction();
beans = da.viewbook();
return ActionSupport.SUCCESS;
}
public List<Beantest> getBeans()
{
return beans;
}
Bookview.jsp:
<s:action name="VBA">
<td>id:</td>
<td>Book:</td>
<td>Author:</td>
<td>Availbleqty:</td>
<td>Category:</td>
</s:action>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>BookView.jsp</welcome-file>
</welcome-file-list>
</web-app>
strut2.xml
<package name="a" namespace="/">
<action name="VBA" class="Action.ActionTest" method="viewbookaction">
<result name="success">/BookView.jsp</result>
</action>
</package>

Don't use s:action tag in JSP, until you know how and why to do that. To display values in JSP you are not needed it at all. You should use struts tags to access action bean properties. For example you can print values to the JSP out using s:property tag. For example
<s:iterator value="beans">
id: <s:property value="id"/><br>
Book: <s:property value="book"/><br>
Author: <s:property value="author"/><br>
Availbleqty: <s:property value="availbleqty"/><br>
Category: <s:property value="category"/><br>
<s:/iterator>
The FilterDispatcher is deprecated and you should replace it with StrutsPrepareAndExecuteFilter. See web.xml docs. Also see this answer for
Need suggestions regarding project implementation in Struts and Hibernate.

Change your web.xml and stuts.xml as below.
Struts.xml
<struts>
<constant name="struts.devMode" value="true" />
<package name="a" namespace="/" extends="struts-default">
<action name="" class="Action.ActionTest" method="viewbookaction">
<result name="success">/BookView.jsp</result>
</action>
</package>
web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
That's all. Simply run.

Related

Custom debugging interceptor with JSP in Struts 2

I'm using Struts 2.
I want to create a custom interceptor debug class to display in interceptor debugging mode browser all attributes field value when user click save button.
ProduitDto:
public class ProduitDto {
private String reference;
private String designation;
private double prix;
private int quantite;
private boolean promo;
public ProduitDto(String reference, String designation, double prix, int quantite, boolean promo) {
this.reference = reference;
this.designation = designation;
this.prix = prix;
this.quantite = quantite;
this.promo = promo;
}
public ProduitDto() {
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
public double getPrix() {
return prix;
}
public void setPrix(double prix) {
this.prix = prix;
}
public int getQuantite() {
return quantite;
}
public void setQuantite(int quantite) {
this.quantite = quantite;
}
public boolean isPromo() {
return promo;
}
public void setPromo(boolean promo) {
this.promo = promo;
}
}
ProduitAction:
import com.id.dto.ProduitDto;
import com.id.entites.Produit;
import com.id.service.ICatalogueService;
import com.id.service.SingletonService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class ProduitAction extends ActionSupport implements ModelDriven<Produit> {
private ProduitDto produitDto = new ProduitDto();
private Produit produit = new Produit();
private List<Produit> produits;
private String ref;
private boolean editMode = false;
private ICatalogueService service = SingletonService.getService();
public String index( ) {
produits = service.listProduit();
return SUCCESS;
}
public String save() {
if (!editMode)
service.ajouterProduit(produit);
else
service.miseAjourProduit(produit);
produits = service.listProduit();
return SUCCESS;
}
public String delete() {
service.supprimerProduit(ref);
produits = service.listProduit();
return SUCCESS;
}
public String edit() {
editMode = true;
produit = service.recupererProduit(ref);
service.miseAjourProduit(produit);
produits = service.listProduit();
return SUCCESS;
}
public Produit getProduit() {
return produit;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public void setProduit(Produit produit) {
this.produit = produit;
}
public List<Produit> getProduits() {
return produits;
}
public void setProduits(List<Produit> produits) {
this.produits = produits;
}
public ProduitDto getProduitDto() {
return produitDto;
}
public void setProduitDto(ProduitDto produitDto) {
this.produitDto = produitDto;
}
public boolean isEditMode() {
return editMode;
}
public void setEditMode(boolean editMode) {
this.editMode = editMode;
}
#Override
public Produit getModel() {
return produit;
}
}
JSP:
<%#taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Produits</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<div>
<s:form action="save" method="post">
<s:textfield label="REF" name="produit.reference"></s:textfield>
<s:textfield label="Designation" name="produit.designation"></s:textfield>
<s:textfield label="Prix" name="produit.prix"></s:textfield>
<s:textfield label="Quantite" name="produit.quantite"></s:textfield>
<s:checkbox label="Promo" name="produit.promo"></s:checkbox>
<!-- <s:textfield name="editMode"></s:textfield> permet de voir une valeur du model -->
<s:hidden name="editMode"></s:hidden>
<s:submit value="Save"></s:submit>
</s:form>
</div>
<div>
<table class="table1">
<tr>
<th>REF</th>
<th>DES</th>
<th>PRIX</th>
<th>QUANTITE</th>
<th>PROMO</th>
</tr>
<s:iterator value="produits">
<tr>
<td><s:property value="reference"/></td>
<td><s:property value="designation"/></td>
<td><s:property value="prix"/></td>
<td><s:property value="quantite"/></td>
<td><s:property value="promo"/></td>
<s:url namespace="/" action="delete" var="lien1">
<s:param name="ref">
<s:property value="reference"/>
</s:param>
</s:url>
<s:url namespace="/" action="edit" var="lien2">
<s:param name="ref">
<s:property value="reference"/>
</s:param>
</s:url>
<td><s:a href="%{lien1}">Suppr</s:a></td>
<td><s:a href="%{lien2}">Edit</s:a></td>
</tr>
</s:iterator>
</table>
</div>
</body>
</html>
struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>views/index.jsp</result>
</action>
<action name="produits" class="com.id.web.ProduitAction" method="index">
<result name="success">views/Produits.jsp</result>
</action>
<action name="save" class="com.id.web.ProduitAction" method="save">
<result name="success">views/Produits.jsp</result>
<result name="input">views/Produits.jsp</result>
</action>
<action name="delete" class="com.id.web.ProduitAction" method="delete">
<result name="success">views/Produits.jsp</result>
</action>
<action name="edit" class="com.id.web.ProduitAction" method="edit">
<result name="success">views/Produits.jsp</result>
</action>
</package>
</struts>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="struts_blank" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Interceptor product :
public class ProductCustomInterceptor implements Interceptor
{
private static final long serialVersionUID = 1L;
#Override
public String intercept(ActionInvocation invocation) throws Exception
{
System.out.println("ProductCustomInterceptor intercept() is called...");
System.out.println(invocation.getAction().getClass().getName());
return invocation.invoke();
}
}
How can I intercept all filed value of my class product when I click save button in my jsp page by using interceptor class with debugging mode browser?

Struts2 with maven no Action mapped for namespace and action name associated with context path

Although it is a common problem but I cannot figure it out.
I am running Strut2 with maven project. My project structure is given bellow
is
Product.java
package beans;
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
}
ProductAction.java
package actions;
import beans.Product;
public class ProductAction{
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
private Product product;
public String execute(){
return "success";
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="product" class="actions.ProductAction" method="execute">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<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>
index.jsp
<html>
<body>
<h2>Hello World!</h2>
<div>
<%# taglib uri="/struts-tags" prefix="s" %>
<s:form action="product">
<s:textfield name="product.id" label="Product Id"></s:textfield>
<s:textfield name="product.name" label="Product Name"></s:textfield>
<s:textfield name="product.price" label="Product Price"></s:textfield>
<s:submit value="save"></s:submit>
</s:form>
</div>
</body>
</html>
and welcome.jsp
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title></title>
</head>
<body>
<%# taglib uri="/struts-tags" prefix="s" %>
Product Id:<s:property value="id"/><br/>
Product Name:<s:property value="name"/><br/>
Product Price:<s:property value="price"/><br/>
</body>
</html>
I am using Glassfish server. It runs index.jsp well. But when I fill the form and submit it shows
HTTP Status 404 - There is no Action mapped for namespace [/] and
action name [product] associated with context path [/deploytest].
type Status report
messageThere is no Action mapped for namespace [/] and action name
[product] associated with context path [/deploytest].
descriptionThe requested resource is not available.
GlassFish Server Open Source Edition 4.1.1
My pom.xml has dependency
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.28</version>
</dependency>

How to forward result to another action in Struts2?

I have this action:
package com.test;
import com.opensymphony.xwork2.Action;
public class TestAction implements Action{
private String simpleParam;
public String getSimpleParam() {
return simpleParam;
}
public void setSimpleParam(String simpleParam) {
this.simpleParam = simpleParam;
}
#Override
public String execute() throws Exception {
return SUCCESS;
}
}
When it's executed I want to invoke another action inside struts(e.g. not redirect) and pass to it simpleParam. SecondAction is:
package com.test;
import com.opensymphony.xwork2.Action;
public class HelloAction implements Action {
private String id;
private String result;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getResult() {
return result;
}
#Override
public String execute() throws Exception {
result = "result" + getId();
return SUCCESS;
}
}
I saw working example when in struts.xml in result simply typed another action name and params and it worked. So I'm trying to do this:
<struts>
<package name="main" extends="struts-default">
<action name="test" class="com.test.TestAction">
<result name="success">hello.action?id=${simpleParam}</result>
</action>
<action name="hello" class="com.test.HelloAction">
<result>/hello.jsp</result>
</action>
</package>
</struts>
Idea totally sees this action but in browser I get 404 status. When I simply invoke hello.action from browser it works. Redirect also works. I also tried chain, but my param wasn't passed and it's not very convinient.
Am I doing it right? And if so what could be the cause of 404 status?
this could be helpful: http://struts.apache.org/docs/action-chaining.html
<struts>
<package name="main" extends="struts-default">
<action name="test" class="com.test.TestAction">
<result name="success" type="chain">hello</result>
</action>
<action name="hello" class="com.test.HelloAction">
<result>/hello.jsp</result>
</action>
</package>
</struts>
to pass a parameter from one action to another you could use the HttpServletRequest
(http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2):
TestAction.java:
package com.test;
import com.opensymphony.xwork2.Action;
public class TestAction implements Action{
private String simpleParam;
public String getSimpleParam() {
return simpleParam;
}
public void setSimpleParam(String simpleParam) {
this.simpleParam = simpleParam;
}
#Override
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("id", simpleParam);
return SUCCESS;
}
}
HelloAction.java:
package com.test;
import com.opensymphony.xwork2.Action;
public class HelloAction implements Action {
private String id;
private String result;
public String getId() {
return id;
}
public String getResult() {
return result;
}
#Override
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
id = (String)request.getAttribute("id");
result = "result" + getId();
return SUCCESS;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
<display-name>Struts2 Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</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>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
<display-name>Struts2 Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</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>
struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_9" version="2.4">
<display-name>Struts2 Application</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</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>
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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
View Records all
</body>
</html>
edit.jsp
<%# taglib uri="/struts-tags" prefix="s" %>
<b>id :</b> <s:property value="id"/> <br>
<b>name :</b> <s:property value="name"/> <br>
<b>salary :</b> <s:property value="salary"/> <br>
<s:textfield name="userid" label="phoneno" value="id"/>
UserAction.java
package com.action;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.model.User;
import com.userservice.UserService;
public class UserAction {
private int id;
private String name;
private int salary;
private int phoneno;
ArrayList<User> list=new ArrayList<User>();
public ArrayList<User> getList() {
return list;
}
public void setList(ArrayList<User> list) {
this.list = list;
}
public String execute(){
UserService service= new UserService();
list=service.getdetails();
return "success";
}
public String delete() {
System.out.println("----------");
return "success";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getPhoneno() {
return phoneno;
}
public void setPhoneno(int phoneno) {
this.phoneno = phoneno;
}
}
UserService.java
package com.userservice;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import com.model.User;
public class UserService {
ArrayList<User> list=new ArrayList<User>();
public ArrayList<User> getdetails(){
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection(
"jdbc:oracle:thin:#localhost:1521:xe","oracletest","oracle");
PreparedStatement ps=con.prepareStatement("select * from employee");
ResultSet rs=ps.executeQuery();
while(rs.next()){
User user=new User();
user.setId(rs.getInt(1));
user.setName(rs.getString(2));
user.setSalary(rs.getInt(3));
user.setPhoneno(rs.getInt(4));
list.add(user);
System.out.println(rs.getString(2));
}
con.close();
}catch(Exception e){e.printStackTrace();}
return list;
}
public ArrayList<User> getList() {
return list;
}
public void setList(ArrayList<User> list) {
this.list = list;
}
}
User.java
package com.model;
public class User {
private int id;
private String name;
private int salary;
private int phoneno;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getPhoneno() {
return phoneno;
}
public void setPhoneno(int phoneno) {
this.phoneno = phoneno;
}
}
welcome.jsp
<%# page contentType="text/html; charset=UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts2 Example</title>
</head>
<body>
<table>
<tr>
<th>id</th>
<th>Name</th>
<th>salary</th>
<th>phoneno</th>
</tr>
<s:iterator value="list" var="user">
<tr>
<td><s:property value="id"/> </td>
<td><s:property value="name"/></td>
<td><s:property value="salary"/></td>
<td><s:property value="phoneno"/></td>
<td>edit</td>
<td>edit1</td>
</tr>
</s:iterator>
</table>
</body>
</html>
</html>

404 page when running project through eclipse on Tomcat

Using Struts2 , I have set up a very simplistic webapp but when i run it in eclipese i got 404 error.. here is my all codes and jarfiles
URL : localhost:8985/firststruts/index.jsp
Jar files
commons-fileupload-1.3.1
commons-io-2.2
commons-lang-2.4
commons-logging-1.1.3
commons-logging-api-1.1
freemarker-2.3.19
javassist-3.11.0.GA
ognl-3.0.6
struts2-core-2.3.16.3
xwork-core-2.3.16.3
Struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts
Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="Product" class="com.javatpoint.Product">
<result name="success">welcome.jsp</result>
</action>
</package>
</struts>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>firstjsp</display-name>
<filter>
<filter-name>struts2</filter-name>
<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>
</web-app>
product.java
public class Product {
private int id;
private String name;
private float price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String execute() {
return "success";
}
}
welcome.jsp
<%# taglib uri="/struts-tags" prefix="s" %>
Product Id:<s:property value="id"/><br/>
Product Name:<s:property value="name"/><br/>
Product Price:<s:property value="price"/><br/>
index.java
<%# taglib uri="/struts-tags" prefix="s" %>
<s:form action="product">
<s:textfield name="id" label="Product Id"></s:textfield>
<s:textfield name="name" label="Product Name"></s:textfield>
<s:textfield name="price" label="Product Price"></s:textfield>
<s:submit value="save"></s:submit>
</s:form>
im new to struts2 so please help me to solve this.

How to inject spring beans into xhtml page

I am working with Spring and I have a problem with a bean, here is my code:
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.grh.bo.ICollaborateurBo;
import com.grh.bo.IUserBo;
import com.grh.entities.Collaborateur;
#Component
#Scope("session")
public class CollaborateurBean implements Serializable {
private static final long serialVersionUID = 1L;
#Autowired
private ICollaborateurBo collaborateurBo;
private String nom;
private String prenom;
private String sflag = "";
private int currentProjectIndex;
private int page = 1;
public List<Collaborateur> listOfCollaborateur = new ArrayList<Collaborateur>();
private Collaborateur Collaborateurr = new Collaborateur();
List<Collaborateur> lis = new ArrayList<Collaborateur>(collaborateurBo.getAllCollaborateur());
#PostConstruct
public void init() {
setListOfCollaborateur(recupererCollaborateur());
Collaborateurr=new Collaborateur();
}
public List<Collaborateur> getLis() {
return collaborateurBo.getAllCollaborateur();
}
public void setLis(List<Collaborateur> lis) {
this.lis = lis;
}
public List<Collaborateur> getCollaborateurList() {
return collaborateurBo.getAllCollaborateur();
}
private List<Collaborateur> recupererCollaborateur() {
List<Collaborateur> ps = collaborateurBo.getAllCollaborateur();
return ps;
}
public CollaborateurBean() {
super();
}
public CollaborateurBean(ICollaborateurBo collaborateurBo, String nom,
String prenom, String sflag, int currentProjectIndex, int page,
List<Collaborateur> listOfCollaborateur,
Collaborateur collaborateurr, List<Collaborateur> lis) {
super();
this.collaborateurBo = collaborateurBo;
this.nom = nom;
this.prenom = prenom;
this.sflag = sflag;
this.currentProjectIndex = currentProjectIndex;
this.page = page;
this.listOfCollaborateur = listOfCollaborateur;
Collaborateurr = collaborateurr;
this.lis = lis;
}
public Collaborateur getCollaborateurr() {
return Collaborateurr;
}
public void setCollaborateurr(Collaborateur collaborateurr) {
Collaborateurr = collaborateurr;
}
public List<Collaborateur> getListOfCollaborateur() {
return listOfCollaborateur;
}
public void setListOfCollaborateur(List<Collaborateur> listOfCollaborateur) {
this.listOfCollaborateur = listOfCollaborateur;
}
public String getSflag() {
return sflag;
}
public void setSflag(String sflag) {
this.sflag = sflag;
}
public void setCollaborateurBo(ICollaborateurBo collaborateurBo) {
this.collaborateurBo = collaborateurBo;
}
public ICollaborateurBo getCollaborateurBo() {
return collaborateurBo;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getCurrentProjectIndex() {
return currentProjectIndex;
}
public void setCurrentProjectIndex(int currentProjectIndex) {
this.currentProjectIndex = currentProjectIndex;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public String getPrenom() {
return prenom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
}
in this page collaborateur.xhtml i Can't display data from my DATABASE collaborateur.xhtml:
<h:dataTable value="#{CollaborateurBean.getLis()}" var="item">
<h:column>
<h:outputText value="#{item.nom}" />
</h:column>
<h:column>
<h:outputText value="#{item.prenom}" />
</h:column>
</h:dataTable>
When I test the collaborateurBo i display data from my DATABASE. This is the code that gives data from database:
<h:dataTable value="#{collaborateurBo.getAllCollaborateur()}" var="item">
<h:column>
<h:outputText value="#{item.nom}" />
</h:column>
<h:column>
<h:outputText value="#{item.prenom}" />
</h:column>
</h:dataTable>
This means that the problem is in the Collaborateur bean not in collaborateurBo or CollaborateurDao.
Here my configurations :
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>GRH</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/config/web-application-config.xml
</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>facelets.SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Production</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
</context-param>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/web-application-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/spring/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>spring/main-flow</welcome-file>
</welcome-file-list>
<error-page>
<error-code>403</error-code>
<location>/spring/acces-interdit-flow</location>
</error-page>
<context-param>
<param-name>org.richfaces.skin</param-name>
<param-value>ruby</param-value>
</context-param>
</web-app>
faces-config:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
<resource-bundle>
<base-name>com.grh.prop.messages</base-name>
<var>msg</var>
</resource-bundle>
</application>
<managed-bean>
<managed-bean-name>currentDate</managed-bean-name>
<managed-bean-class>java.util.Date</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
</faces-config>
web-application-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms" xmlns:jee="http://www.springframework.org/schema/jee"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
<!-- Scans for application #Components to deploy -->
<context:component-scan base-package="org.springframework.webflow.samples.booking" />
<context:component-scan base-package="com.grh" />
<!-- Database Configuration -->
<import resource="DataSource.xml" />
<import resource="Hibernate.xml" />
<!-- Spring Configuration -->
<import resource="webmvc-config.xml" />
<import resource="webflow-config.xml" />
<!-- Spring security 3.0.5 -->
<import resource="security-config.xml" />
</beans>
Please help me, thanx in advance,

Categories