Struts2 validation message appearing twice - java

I am trying struts validation but the error messages are getting printed twice. my action class is as follow.OSAction.java . I am so using hibernate in it. I think the validate method is called twice,
package net.ajeet.os.view;
import java.util.List;
import net.ajeet.os.controller.OSManager;
import net.ajeet.os.model.OSDetail;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class OSAction extends ActionSupport {
private static final long serialVersionUID = 9149826260758390091L;
public OSDetail osdetail= new OSDetail();
private List<OSDetail> osdetails_list;
public OSDetail getOsdetail() {
return osdetail;
}
public void setOsdetail(OSDetail osdetail) {
this.osdetail = osdetail;
}
private Long id;
private OSManager linkController= new OSManager();
/* #Override
public OSDetail getModel() {
return osdetail;
}*/
/* public OSAction() {
linkController = new OSManager();
}
public String execute() {
this.osdetails_list = linkController.list();
return SUCCESS;
}
*/
public String add() {
try {
linkController.add(getOsdetail());
//linkController.add(osdetail);
} catch (Exception e) {
e.printStackTrace();
}
this.osdetails_list = linkController.list();
return SUCCESS;
}
/* public String delete() {
linkController.delete(getid());
return SUCCESS;
}*/
public List<OSDetail> getOsdetails_list() {
return osdetails_list;
}
public void setOsdetails_list(List<OSDetail> osdetails_list) {
this.osdetails_list = osdetails_list;
}
/* public Long getid() {
return id;
}
public void setid(Long id) {
this.id = id;
}
*/
public void validate()
{
if (osdetail.getOSname() == null || osdetail.getOSname().trim().equals(""))
{
addFieldError("osdetail.OSname","The OS Name is required");
}
if (osdetail.getOSversion() == null || osdetail.getOSversion().trim().equals(""))
{
addFieldError("osdetail.OSversion","The OS Version is required");
}
}
}
My Index.jsp is below
<%# page contentType="text/html; charset=UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title>OS Manager - Struts2 Hibernate Example</title>
</head>
<body>
 
<h1>OS Manager</h1>
<s:actionerror/>
 
<s:form action="add" method="post" >
<s:hidden name="OSid" value="%{id}" />
    <s:textfield name="osdetail.OSname" label="name" />
    <s:textfield name="osdetail.OSversion" label="version"/>
    <s:textfield name="osdetail.OSnotes" label="notes"/>
    <s:submit value="Add OS Details" align="center"/>
<s:reset value="Reset" />
</s:form>
 
 
<h2>OS Details</h2>
<table>
<tr>
    <th>OS Name</th>
    <th>OS Version</th>
    <th>OS Notes</th>
</tr>
<s:iterator value="osdetails_list" var="osdetail">
    <tr>
        <td><s:property value="OSname"/></td>
        <td><s:property value="OSversion"/></td>
        <td><s:property value="OSnotes"/></td>
    </tr>
</s:iterator>
</table>
</body>
</html>
Struts.xml is below
![<?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="add"
class="net.ajeet.os.view.OSAction" method="add">
<result name="success" type="chain">index</result>
<result name="input" type="chain">index</result>
</action>
<action name="index"
class="net.ajeet.os.view.OSAction">
<result name="success">index.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>][2]
enter code here

This is because you are incorrectly using a "chain" result. Don't use it unless you know what are you doing. To fix error you should change the configuration like this
<action name="add" class="net.ajeet.os.view.OSAction" method="add">
<result type="redirectAction">index</result>
<result name="input">/index.jsp</result>
</action>
<action name="index" class="net.ajeet.os.view.OSAction">
<result>/index.jsp</result>
</action>

Just implement interceptor-ref name="defaultStack" before your action in struts.xml

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?

Struts delete action setters not called [duplicate]

This question already has an answer here:
How to set the property value for model object in action class using ModelDriven in Struts 2? [closed]
(1 answer)
Closed 5 years ago.
I have a Struts action class like so:
public class OrderDetailAction extends BaseActionSupport {
private String ID = new OID().toString();
private Collection<OrderDetail> orderdetailList;
private String orderStatus;
private String shippingAddressId;
private java.util.Date createdDate;
private java.util.Date updatedDate;
private String billingAddressId;
public void setOrderStatus(String orderStatus) {
this.orderStatus = orderStatus;
}
public String getOrderStatus() {
return orderStatus;
}
public void setShippingAddressId(String shippingAddressId) {
this.shippingAddressId = shippingAddressId;
}
public String getShippingAddressId() {
return shippingAddressId;
}
public void setCreatedDate(java.util.Date createdDate) {
this.createdDate = createdDate;
}
public java.util.Date getCreatedDate() {
return createdDate;
}
public void setUpdatedDate(java.util.Date updatedDate) {
this.updatedDate = updatedDate;
}
public java.util.Date getUpdatedDate() {
return updatedDate;
}
public void setBillingAddressId(String billingAddressId) {
this.billingAddressId = billingAddressId;
}
public String getBillingAddressId() {
return billingAddressId;
}
public String getID() {
return ID;
}
public void setID(String ID) {
this.ID = ID;
}
public Collection<OrderDetail> getOrderDetailList() {
return orderdetailList;
}
public void setOrderDetailList(Collection<OrderDetail> orderdetailList) {
this.orderdetailList = orderdetailList;
}
// some more logic
}
Whenever I run a create action, a display action or an edit action, struts calls all of the setters. However, when I run my delete action struts fails to call all the setters EXCEPT setID().
Is there a reason why this happens?
Here is my struts.xml for reference:
<struts>
// some other stuff here
<action name="getOrderDetails" class="presentation.OrderDetailAction" method="getOrderDetails">
<result name="success">OrderDetailList.jsp</result>
</action>
<action name="displayOrderDetail" class="presentation.OrderDetailAction" method='displayOrderDetail'>
<result name="success">OrderDetail.jsp</result>
</action>
<action name="displayCreateOrderDetail" class="presentation.OrderDetailAction" method='displayCreate'>
<result name="success">CreateOrderDetail.jsp</result>
</action>
<action name="createOrderDetail" class="presentation.OrderDetailAction" method='create'>
<result name="success" type="chain">getOrderDetails</result>
<result name="input">CreateOrderDetail.jsp</result>
<result name="error">CreateOrderDetail.jsp</result>
</action>
<action name="displayEditOrderDetail" class="presentation.OrderDetailAction" method='displayUpdate'>
<result name="success">EditOrderDetail.jsp</result>
</action>
<action name="editOrderDetail" class="presentation.OrderDetailAction" method='update'>
<result name="success" type="chain">getOrderDetails</result>
<result name="input">EditOrderDetail.jsp</result>
<result name="error">EditOrderDetail.jsp</result>
</action>
<action name="deleteOrderDetail" class="presentation.OrderDetailAction" method='delete'>
<result name="success" type="chain">getOrderDetails</result>
</action>
// some more stuff here
</sturts>
There is no difference between what the input looks like form the JSP either:
<input name="action:displayEditOrderDetail" class="btn btn-success" value="Edit" type="submit" id="displayOrderDetail_displayEditOrderDetail"/>
<input name="action:deleteOrderDetail" class="btn btn-danger" value="Delete" type="submit" id="displayOrderDetail_deleteOrderDetail"/>
<input name="action:getOrderDetails" class="btn btn-default" value="Cancel" type="submit" id="displayOrderDetail_getOrderDetails"/>
For every other CRUD operation, Struts successfully calls all the setters. Except in the case of Delete where it only calls setID(). Is there something different I am supposed to be doing for Delete?
After deleting an object you should return redirectAction result.
<result name="success" type="redirectAction">getOrderDetails</result>

Struts2 action always returning success

I'm a beginner in STRUTS2 and I want to test two Actions.
The first Action Product work correctly. When I enter "hello" return success otherwise error.
But the second Action CatererTyp return always success.
Can somebody please explain me why?
Here is Product.java
package com.javatpoint;
import com.opensymphony.xwork2.ActionSupport;
public class Product extends ActionSupport {
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() {
if(name.equals("hallo")){
return SUCCESS;
}
else{
return ERROR;
}
}
}
CatererType.java
package com.javatpoint;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.opensymphony.xwork2.ActionSupport;
public class CatererType extends ActionSupport {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String excute() {
if (description.equals("hello")) {
return ERROR;
} else {
return SUCCESS;
}
}
}
accessdiened.jsp
<%# page contentType="text/html; charset=UTF-8" %>
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Access Denied</title>
</head>
<body>
You are not authorized to view this page.
</body>
</html>
welcome.jsp
<%# taglib uri="/struts-tags" prefix="s" %>
The Caterer Type: <s:property value="description"/> was inserted <br/>
adminpage.jsp
<%# taglib uri="/struts-tags" prefix="s" %>
<html>
<center>
<s:form action="caterertype">
<s:textfield name="description" label="Caterer Type"></s:textfield>
<s:submit value="save"></s:submit>
</s:form>
</center>
</html>
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="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="default" extends="struts-default">
<action name="product" class="com.javatpoint.Product" method="execute">
<result name="success">welcome.jsp</result>
<result name="error">/AccessDenied.jsp</result>
</action>
<action name="caterertype" class="com.javatpoint.CatererType" method="execute">
<result name="success">welcome.jsp</result>
<result name="error">AccessDenied.jsp</result>
</action>
</package>
</struts>

how to action fire in Struts2

This is My JSP Page .
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<style type="text/css">
b {
color: navy;
background-color: orange;
}
</style>
<title>Struts2-Spring-Tiles integration | dineshonjava.com</title>
</head>
<body>
<h2>Add User</h2>
<b> <s:form action="addUsermenu">
<s:textfield name="userName" key="user.name" />
<s:textfield name="userAge" key="user.age" value="" />
<s:radio name="userGender" key="user.gender" list="{'Male','Female'}" />
<s:select name="userJob" key="user.job"
list="%{#{'Software':'Software','Hardware':'Hardware','Networking':'Networking','Marketing':'Marketing'}}" />
<s:checkboxlist name="userHobbies" key="user.hobby"
list="{'Cricket','Football','Drawing','Cooking','Driving','Movie'}" />
<s:submit key="submit" align="center" />
</s:form> </b>
<s:if test="%{users.isEmpty()}">
</s:if>
<s:else>
<b>List of Users</b>
<table border="1">
<tr>
<td><b>Name</b></td>
<td><b>Age</b></td>
<td><b>Gender</b></td>
<td><b>Job Type</b></td>
<td><b>Hobbies</b></td>
</tr>
<s:iterator value="users">
<tr>
<td><s:property value="userName" /></td>
<td><s:property value="userAge" /></td>
<td><s:property value="userGender" /></td>
<td><s:property value="userJob" /></td>
<td><s:property value="userHobbies" /></td>
<td>Delete
<td>Update
</td>
</tr>
</s:iterator>
</table>
</s:else>
</body>
</html
This is My StrutsConfig.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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<constant name="struts.custom.i18n.resources" value="myapp" />
<package name="user" extends="struts-default" namespace="/">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<action name="user" class="user" method="execute">
<result name="user" type="tiles">mainTemplate</result>
</action>
<action name="*menu" class="user" method="{1}">
<result name="user" type="tiles">mainTemplate</result>
<result name="madhuri" type="tiles">madhuri</result>
<result name="alia" type="tiles">alia</result>
<result name="addUser" type="tiles">mainTemplate</result>
<result name="deleteUser" type="tiles">mainTemplate</result>
</action>
</package>
</struts>
this is my USerAction.jsp:
public class UserAction extends ActionSupport implements ModelDriven<UserBean>{
private static final long serialVersionUID = 1L;
#Autowired
private UserBean userBean;
#Autowired
private UserService userService;
private List<UserBean> users;
public String execute() {
users = CommonUtility.createUserBeanList(userService.getUserList());
return "user";
}
public String addUser(){
userService.saveUser(CommonUtility.createModel(userBean));
users = CommonUtility.createUserBeanList(userService.getUserList());
return "addUser";
}
public String deleteUser(){
userService.deleteUser(CommonUtility.createModel(userBean));
users = CommonUtility.createUserBeanList(userService.getUserList());
return "deleteUser";
}
public String listUser(){
users = CommonUtility.createUserBeanList(userService.getUserList());
return "users";
}
#Override
public UserBean getModel() {
return userBean;
}
public String alia() {
return "alia";
}
public String madhuri() {
return "madhuri";
}
public String user() {
return "user";
}
public List<UserBean> getUsers() {
return users;
}
public void setUsers(List<UserBean> users) {
this.users = users;
}
}
public String deleteUser(){
userService.deleteUser(CommonUtility.createModel(userBean));
users = CommonUtility.createUserBeanList(userService.getUserList());
return "deleteUser";
}
function is not working i am stucking where am doing wrong Please help me i think there is minor mistake so i Could not able to find that Error .
The form is submiting an action name as addUsermenu.
In your struts.xml, you have an action tag to point to all the methods, that matches *menu. So your addUsermenu will find its corresponding method. ie addUser().
But in the case of delete you must change the action name to deleteUsermenu inorder to find the function deleteUser()
Solution
Change the delete Link to this
Delete
to this
Delete

The list attribute in select tag can't be resolved when using ModelDriven interface in Struts 2

I use the interface ModelDriven in my Struts 2 application. I have a problem rendering the page because I always get an error:
19 nov. 2013 11:23:12 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: "Servlet.service()" pour la servlet jsp a généré une exception
tag 'select', field 'list': The requested list key 'listeItems' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
at org.apache.struts2.components.Component.fieldError(Component.java:240)
at org.apache.struts2.components.Component.findValue(Component.java:333)
at org.apache.struts2.components.ListUIBean.evaluateExtraParams(ListUIBean.java:80)
I don't know where is the error so I call distress to the community.
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>
<constant name="struts.devMode" value="false" />
<constant name="struts.action.extension" value="do" />
<constant name="struts.custom.i18n.resources" value="com.omb.i18n.StrutsResourceBundle" />
<constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
<constant name="struts.objectFactory.spring.autoWire" value="name" />
<constant name="struts.i18n.encoding" value="ISO-8859-1" />
<constant name="struts.i18n.reload" value="false" />
<constant name="struts.configuration.xml.reload" value="false" />
<constant name="struts.locale" value="fr" />
<constant name="struts.multipart.maxSize" value="100000000000" />
<constant name="struts.enable.SlashesInActionNames" value="true" />
<constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>
<constant name="struts.codebehind.classSuffix" value="Controller"/>
<constant name="struts.codebehind.action.checkImplementsAction" value="false"/>
<constant name="struts.codebehind.action.checkAnnotation" value="false"/>
<constant name="struts.codebehind.action.defaultMethodName" value="index"/>
<constant name="struts.configuration.classpath.defaultParentPackage" value="rest-default" />
<package name="default" extends="tiles-default" namespace="/">
<interceptors>
<interceptor name="params-filter"
class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />
<interceptor-stack name="defaultStack">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
<param name="maximumSize">11204928</param>
</interceptor-ref>
<interceptor-ref name="staticParams" />
<interceptor-ref name="conversionError" />
<interceptor-ref name="params" />
<interceptor-ref name="prepare" />
<interceptor-ref name="basicStack"/>
<interceptor-ref name="validation" />
<interceptor-ref name="workflow" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack" />
<global-results>
<result name="technicalError" type="chain">
errorAction
</result>
<result name="sessionInvalidError" type="tiles">
sessionInvalid
</result>
<result name="blank" type="tiles">blank</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception"
result="technicalError" />
<exception-mapping
exception="com.omb.service.exception.UserSessionInvalidException"
result="sessionInvalidError" />
</global-exception-mappings>
</package>
<package name="omb" extends="default" namespace="/omb">
<action name="*Action" class="myAction" method="{1}">
<result name="success" type="redirectAction">
<param name="namespace">/omb</param>
<param name="actionName">displayResult</param>
</result>
<result name="error" type="redirectAction">
<param name="namespace">/error</param>
<param name="actionName">displayError</param>
</result>
</action>
</package>
</struts>
MyAction.java:
package com.omb.actions;
public class MyAction extends ActionSupport implements ModelDriven<MyModel>{
private MyModel myModel = new MyModel();
public MyModel getModel() {
return myModel;
}
public String execute() throws Exception {
myModel.add(new Item("A", "Item A"));
myModel.add(new Item("B", "Item B"));
return SUCCESS;
}
public String doAction() {
// do something
return "SUCCESS";
}
public MyModel getMyModel() {
return this.myModel;
}
public void setMyModel(MyModel myModel) {
this.myModel = myModel;
}
}
MyModel.java:
package com.omb.modele;
import java.util.ArrayList;
import java.util.List;
import com.omb.item.Item;
public class MyModel {
private String idItem;
private List<Item> listeItems = new ArrayList<Item>();
public String getIdItem() {
return this.idItem;
}
public void setIdItem(String idItem) {
this.idItem = idItem;
}
public List<Item> getListeItems() {
return this.listeItems;
}
public void setListeItems(List<Item> listeItems) {
this.listeItems = listeItems;
}
}
Item.java:
package com.omb.item;
import java.io.Serializable;
public class Item implements Serializable {
private static final long serialVersionUID = 1L;
private String id;
private String label;
public Item() {
super();
}
public Item(String id, String label) {
this.id = id;
this.label = label;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
JSP file :
<%# taglib prefix="s" uri="/struts-tags"%>
<table width="100%">
<tr>
<td><label><s:property value="%{getText('label')}" /></label></td>
</tr>
<tr>
<td><s:select id="idSelectItem"
emptyOption="true" list="listeItems" value="idItem"
listKey="id" listValue="label" />
</td>
</tr>
</table>
Remove method="{1}" because you don't not use wildcard mapping and your action doesn't have methods other than execute and only this method initializes the list. If the list is not initialized the error like above occur. If you have other methods didn't show there you should implement Preparable for the action and move the code that initializes the list there.
public class MyAction extends ActionSupport implements ModelDriven<MyModel>, Preparable {
public void prepare() {
myModel = new MyModel();
myModel.add(new Item("A", "Item A"));
myModel.add(new Item("B", "Item B"));
}
...
}

Categories