Struts 2 Drop Down Example Problem - struts2

i am trying to work on a drop down list using struts2 tag. But till now i am unsuccessful: below is the .jsp page, the action class, the struts.xml file and all relevant codes. Any help will be greatly appreciated. Thank you.
Note: I am getting this error :-> tag 'select', field 'list': The requested list key 'country' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]
...........
Index.jsp :
.............
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="/struts-tags" prefix="s" %>
<h3>Struts 2: UI Tag Example - Registration Page Demo </h3><hr>
<s:form action="register">
<s:select label="Country" list="country" listKey="countryAbbr" listValue="countryName" />
<s:submit/>
</s:form>
......................
struts.xml:
......................
<package name="com.uitagdemo" extends="struts-default">
<action name="register" class="com.uitagdemo.RegisterAction" >
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
...........................
RegisterAction.java
...........................
public class RegisterAction extends ActionSupport {
private List<Country> country;
public String execute() {
return SUCCESS;
}
public List<Country> getCountry(){
country = new ArrayList<Country>();
country.add(new Country("IN", "INDIA"));
country.add(new Country("US", "USA"));
country.add(new Country("FR", "FRANCE"));
return country;
}
}
.......................
success.jsp
.......................
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%#taglib uri="/struts-tags" prefix="s" %>
Struts 2: UI Tag Example
Struts 2: UI Tag Example - Registration Page Demo
Country: <s:property value="country" /><br>
</body>
......................
Country.java
......................
public class Country {
private String countryAbbr;
private String countryName;
public Country() {
}
public Country(String countryAbbr, String countryName) {
this.countryAbbr = countryAbbr;
this.countryName = countryName;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getCountryAbbr() {
return countryAbbr;
}
public void setCountryAbbr(String countryAbbr) {
this.countryAbbr = countryAbbr;
}
}

In this case you are using the Country class as a Map. Simplest solution would be to make a Hashmap property called country with appropriate getter/setters and populate it with what you need.
Then in your JSP you could say something like:
<s:select label="Country" list="country.keys">
or maybe you'd rather
<s:select label="Country" list="country.values">

The workflow should be reverse than above. It should be
action that fills select tag(calls getcountry) -> page with form -> action(to which form will be submitted)
but above we have
page with form -> submitted to action that fills the select, but form has already been displayed.

Related

Model object becomes null. Is there any change required for ModelDriven interface when upgrading from struts 2.3.35 to 2.5.26? [duplicate]

I am new to Struts 2 framework. I have made a program for understanding modelDriven interceptor. But I am unable to execute it. Following are the list of files and in the end there is a output (error).
index.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s"%>
<!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>
<%-- <jsp:useBean id="ent" class="pack.Entity" scope="session" /> --%>
<s:form method="get" action="go">
<s:textfield name="t1" label="Name"></s:textfield>
<s:password name="p1" label="Password"></s:password>
<s:submit value="accept"></s:submit>
</s:form>
</body>
</html>
Entity.java:
package actions_pack;
public class Entity {
private String t1;
private String p1;
public String getP1() {
return p1;
}
public void setP1(String p1) {
this.p1 = p1;
}
public Entity() {
super();
// TODO Auto-generated constructor stub
}
public String getT1() {
return t1;
}
public void setT1(String t1) {
this.t1 = t1;
}
}
GoAction.java:
package actions_pack;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor;
import com.opensymphony.xwork2.util.ValueStack;
public class GoAction implements ModelDriven<Entity> {
private Entity en;
public Entity getEn() {
return en;
}
public void setEn(Entity en) {
this.en = en;
}
public String execute(){
System.out.println("inside action");
if(en.getT1().equalsIgnoreCase("nitin")){
return "success";
}
else{
return "failure";
}
}
#Override
public Entity getModel() {
System.out.println("inside model driven....");
en=new Entity();
return en;
}
}
struts.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<package name="dd">
<result-types>
<result-type name="dispatcher"
class="org.apache.struts2.dispatcher.ServletDispatcherResult"
default="true" />
</result-types>
<interceptors>
<interceptor name="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="modelDriven"></interceptor-ref>
</interceptor-stack>
</interceptors>
<action name="go" class="actions_pack.GoAction">
<interceptor-ref name="myStack"></interceptor-ref>
<result name="success" type="dispatcher" >/one/welcome.jsp</result>
<result name="failure" type="dispatcher">/one/error.jsp</result>
</action>
</package>
</struts>
welcome.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib uri="/struts-tags" prefix="s" %>
<!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>
Welcome, <s:property value="t1"/>
</body>
</html>
web-page output(500 error):
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
actions_pack.GoAction.execute(GoAction.java:24)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:450)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:289)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:252)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:100)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:246)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:54)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:563)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:99)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.34 logs.
Apache Tomcat/7.0.34
In struts.xml file I don't want to use/extend struts-default package. Although, when I include params interceptor entry along with modelDriven interceptor in struts.xml, problem solves. What is the reason behind this. Can any one guide me?
You have a property in the action class that needs initialize prior to the action execution. You can do it in many ways.
The way you have chosen relies on modelDriven interceptor which is running before your action is executed. It invokes getModel() to push it on top of the valueStack. So, your entity property is being initialized. If you remove this interceptor you will get NullPointerException when the action is executed.
If your Entity is a simple POJO that you can instantiate yourself then simply do it inline instead of in getModel().
private Entity en = new Entity();
#Override
public Entity getModel() {
System.out.println("inside model driven....");
return en;
}
Next part is about params interceptor. It uses OGNL to populate a top object that is a model if you have used modelDriven interceptor prior params interceptor.
Living it along with your action allows to initialize some properties you reference in the execute().
For example t1 should be initialized.

Struts2 Global Result and global exception not working

I am trying to get the exception via struts2 to display the global results jsp but its not working and instead I am getting a java exception show in console. Also would like to add that If i use interceptor-ref exception individually to an action its working fine but globally not working as intended.
Here is my struts.xml, just addded a simple global results and exceptions.
<?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="true"></constant>
<package name="user" extends="struts-default">
<global-results>
<result name="myresult">globalresult.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping result="myresult" exception="java.lang.Exception"></exception-mapping>
</global-exception-mappings>
<action name="UserAction" class="actionclasses.UserAction"
method="execute">
<interceptor-ref name="timer"></interceptor-ref>
<interceptor-ref name="params"></interceptor-ref>
<result name="input">index.jsp</result>
<result name="success">success.jsp</result>
</action>
<action name="LoginAction">
<result type="redirect">login.jsp</result>
</action>
</package>
</struts>
My UserAction class from where exception is raised.
package actionclasses;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
String userName;
String passWord;
public String execute()
{
System.out.println(userName);
int a=10/0;
return "success";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
My login.jsp, my login 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>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>User Login</title>
</head>
<body>
<h1>Login to Web page</h1>
<s:form action="UserAction" method="post">
<s:textfield name="userName" label="Enter UserName" />
<s:password name="passWord" label="Enter Password" />
<s:submit value="submit" />
</s:form>
</body>
</html>
Once you specify any interceptors you must specify all interceptors.

Struts 2 JQuery Grid is not getting displayed

I am trying to show the data from struts2 action class into JQuery Grid. but when i try to achieve that, I am not even getting the Grid, it is just showing the JSON string as output, as shown below, tried all the other answers posted here but no luck..:(
{
"dummyData": [
{
"email": "ABC",
"name": "DEF"
}
],
"page": null,
"records": 1,
"resDetailObj": {
"email": "ABC",
"name": "DEF"
},
"rows": 0,
"searchField": null,
"searchOper": null,
"searchString": null,
"sidx": null,
"sord": null,
"total": 2147483647
}
I am using struts2-jquery-grid-plugin-3.5.1.jar, struts2-jquery-plugin-3.2.1.jar and struts2-json-plugin-2.1.8.jar
My 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" %>
<%# taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags" %>
<html>
<h1>In JQueryGrid</h1>
<head>
<sj:head jqueryui="true" jquerytheme="redmond"/>
</head>
<body>
<s:url var="remoteurl" action="grid"/>
<sjg:grid
id="mygrid"
caption="My Details"
dataType="json"
href="%{remoteurl}"
pager="true"
gridModel="dummyData"
>
<sjg:gridColumn name="name" index="name" title="Name"
sortable="true"/>
<sjg:gridColumn name="email" index="email" title="Email"
sortable="true"/>
</sjg:grid>
</body>
</html>
and my struts.xml file is
<package name="JQueryGrid" namespace="/" extends="struts-default">
<action name="grid class="com.digi.crystal.resourceutilization.actions.GridAction">
<result name="success" type="json">
/JqueryGrid.jsp
</result>
</action>
</package>
and finally my Action class is
public class GridAction extends ActionSupport
{
private List<CResourceDetails> dummyData;
private Integer rows=0;
private Integer page;
private String sord;
private String sidx;
private String searchField;
private String searchString;
private String searchOper;
private Integer total;
private Integer records;
private CResourceDetails resDetailObj = new CResourceDetails();
public GridAction()
{
}
public String execute()
{
List<CResourceDetails> temp = new ArrayList<CResourceDetails>();
resDetailObj.setEmail("ABC");
resDetailObj.setName("DEF");
temp.add(resDetailObj);
setDummyData(temp);
System.out.println("The temp is:"+"in execute"+dummyData.size()+","+rows);
setRecords(temp.size());
total =(int) Math.ceil((double)records / (double)rows);
System.out.println("the total is:"+total/1000000000);
return Action.SUCCESS;
}
// getters and setters of attributes
Here CResourceDetails is nothing but a plain POJO, with name and email.
If anyone could help me with this grid, that would be a great favor to me.
Thanks a lot in advance,
C.S.Siddartha
I think you are accessing directly to the action grid:
http://localhost:8080/JQueryGrid/grid.action
Try to access to the jsp:
http://localhost:8080/JQueryGrid/JqueryGrid.jsp
The grid will then call the action and retrive the result.
Siddarth first write an action in struts with result mapping only to the JSP page containing the grid And then wirte ur action for grid with only json
<package name="JQueryGrid" namespace="/" extends="struts-default,json-default">
<action name="opengridJSP" class="com.digi.crystal.resourceutilization.actions.GridAction" method ="SomeMethod">
<result name="success">/JqueryGrid.jsp</result>
</action>
<action name="grid class="com.digi.crystal.resourceutilization.actions.GridAction">
<result name="success" type="json"/>
</action>
</package>

Struts2 validation is not even working

I also configured all the setting for Struts2 basic validation, but nothing is working for me.
My login.jsp file
<%#taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Login Page</title>
<s:head />
</head>
<body>
<s:actionerror/>
<s:form action="Login" method="post">
<s:textfield name="userName" label="User Name" />
<s:password name="password" label="Password" />
<s:submit value="Login" />
<s:fielderror></s:fielderror>
</s:form>
</body>
</html>
My loginAction class is,
package com.test;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String userName;
private String password;
public LoginAction() {
}
public String execute() {
return "SUCCESS";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
my struts2.xml file is,
<package name="default" namespace="/" extends="struts-default">
<action name="Login" class="com.test.LoginAction" method="execute">
<result name="SUCCESS">login.jsp</result>
<result name="input">login.jsp</result>
</action>
</package>
</struts>
And I place my LoginAction-validation.xml file in the same package where the Action class is placed.
Please lookout my code and help me to resolve the validation issue.
You are returning same login.jsp page in your execute method in action class.
So this results in displaying login page with empty field every time you hit enter.
Try creating an additional jsp to which you are redirected to based on String returned from action class.
other than that i dont see any problem.

Struts2 parameters is coming null in action class

i am not able to get the value of the text entered in the textbox in jsp into my action class. below are my files. can anyone please help as this is very urgent.
my jsp is
<%# taglib uri="/struts-tags" prefix="s" %>
<html>
<body>
<s:form action="login" method="post">
<s:textfield name="userName" label="User Name" />
<s:submit type="button" id="submit" label="Submit"/>
<s:reset id="reset" label="Reset"></s:reset>
</s:form>
</body>
</html>
my action class is
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String userName;
public String getUserName() {
return userName;
}
public void setUsername(String username) {
this.userName = username;
}
public String execute(){
System.out.println("the userName is "+this.getUserName());
return SUCCESS;
}
}
and my struts.xml is
<?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>
<include file="struts-default.xml" />
<package name="default" extends="struts-default">
<action name="login" class="core.login.LoginAction">
<result name="error">>/webpages/login/Error.jsp</result>
<result name="success">/webpages/home/Home.jsp</result>
</action>
</package>
</struts>
Case-sensitivity is important.
public void setUsername(String username) {
this.userName = username;
}
Should be:
public void setUserName(String username) {
this.userName = username;
}
In your struts.xml, you have written
class="core.login.LoginAction"
but you are not declaring any package in your Action class.
Also setter and getter method are of different names. Remember they are case-sensitive as said by Steven.
Try removing minor mistakes like an extra ">"
In your textfield add value attribute like this:
<s:textfield name="userName" label="User Name" value="${userName}" />
That directly maps it to member variable in action class.
In Eclipse,
to solve case sensitivity problem right click on code writing area and go to source -> Generate getter and setter method
It will add getter and setter method for selected member variables.

Resources