Struts2 parameters is coming null in action class when using Interceptors - struts2

When I am using Interceptors, the values on Action class coming as null. I have removed the Interceptors, the values coming perfect from the JSP page.
Login.jsp
<s:form id="loginFrm" name="loginFrm" action="LoginAction">
<s:textfield key="username"/>
<s:password key="password"/>
<s:submit/>
</s:form>
LoginAction.java
public class LoginAction {
private static Logger LOGGER = Logger.getLogger(LoginAction.class);
private String username;
private String password;
public String execute() throws Exception {
LOGGER.info("LoginAction : authenticate()");
LOGGER.info("LoginAction : {[" + username + "],["+password+ "]}");
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;
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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>Bulk Fund Switching</display-name>
<context-param>
<param-name> org.apache.tiles.impl.BasicTilesContainer.DEFINITIONS_CONFIG </param-name>
<param-value>/WEB-INF/tiles.xml</param-value>
</context-param>
<listener>
<listener-class>org.apache.struts2.tiles.StrutsTilesListener </listener-class>
</listener>
<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>jsp/Login.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml (working version)
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="BFSView,BFSMessages" />
<package name="home-default" extends="struts-default" namespace="/">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<interceptors>
<interceptor name="AuthenticationInterceptor" class="com.lv.bfs.controller.interceptor.AuthenticationInterceptor"></interceptor>
<interceptor-stack name="SecureStack">
<interceptor-ref name="AuthenticationInterceptor" />
</interceptor-stack>
</interceptors>
<action name="LoginAction" class="com.lv.bfs.controller.action.LoginAction">
<result name="success">jsp/Welcome.jsp</result>
<result name="error">/jsp/Login.jsp</result>
</action>
</package>
</struts>
struts.xml (Not working version )
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="BFSView,BFSMessages" />
<package name="home-default" extends="struts-default" namespace="/">
<result-types>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult" />
</result-types>
<interceptors>
<interceptor name="AuthenticationInterceptor" class="com.lv.bfs.controller.interceptor.AuthenticationInterceptor"></interceptor>
<interceptor-stack name="SecureStack">
<interceptor-ref name="AuthenticationInterceptor" />
</interceptor-stack>
</interceptors>
<action name="LoginAction" class="com.lv.bfs.controller.action.LoginAction">
<interceptor-ref name="SecureStack"></interceptor-ref>
<result name="success">jsp/Welcome.jsp</result>
<result name="error">/jsp/Login.jsp</result>
</action>
</package>
</struts>
AuthenticationInterceptor.java
public class AuthenticationInterceptor extends AbstractInterceptor {
private static Logger LOGGER = Logger.getLogger(AuthenticationInterceptor.class);
private static final long serialVersionUID = 1844249996954274967L;
public String intercept(ActionInvocation invocation) throws Exception {
LOGGER.info("intercept : START");
return invocation.invoke();
}
}
Logs statements (both cases)
12:39:05,533 INFO [AuthenticationInterceptor] intercept : START
12:39:05,533 INFO [LoginAction] LoginAction : authenticate()
12:39:05,533 INFO [LoginAction] LoginAction : {[null],[null]}
12:09:05,533 INFO [LoginAction] LoginAction : authenticate()
12:09:05,533 INFO [LoginAction] LoginAction : {[admin],[admin]}
Any suggestions?

When you define an interceptor stack for a specific action you must define all the interceptors for that action. In this case, you are only defining your custom interceptor, meaning the "params" interceptor doesn't run, so the action properties won't be set.
Either define a new stack that includes the normal S2 interceptors, or include those interceptors (or stack) in your action configuration.

Related

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.

Populating drop down in Struts2

I am using struts2 for implementing my application. In my application I have to implement two drop downs, value of the second drop down is dependent on first drop down. I got reference from an example and implemented my code accordingly, but still not getting the result. Please give your suggestion to make this code work.
SupporterAction.java
package action;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import entityBean.TicketDataBean;
import entityBean.UserBean;
import entityListener.SupporterListener;
import entityManager.Application;
#SuppressWarnings("serial")
public class SupporterAction extends ActionSupport{
private String application;
private String number;
private List<String> applicationNames;
List<String> ticketNumber;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public List<String> getApplicationNames() {
return applicationNames;
}
public void setApplicationNames(List<String> applicationNames) {
this.applicationNames = applicationNames;
}
public List<String> getTicketNumber() {
return ticketNumber;
}
public void setTicketNumber(List<String> ticketNumber) {
this.ticketNumber = ticketNumber;
}
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
public String getJSON() {
return execute();
}
#SuppressWarnings("unchecked")
public String execute()
{
#SuppressWarnings("rawtypes")
Map session = ActionContext.getContext().getSession();
System.out.println(session.get("currentSessionUser"));
applicationNames = new ArrayList<String>();
UserBean userBean = (UserBean)session.get("currentSessionUser");
List<Application> applicationObj = userBean.getApplication();
for (Application obj : applicationObj)
{
System.out.println(obj.getApplicationName());
applicationNames.add(obj.getApplicationName());
}
session.put("ApplicationNames",applicationNames);
System.out.println("Hello");
System.out.println(application);
if(application != null)
{
ticketNumber=new ArrayList<String>();
System.out.println("Hello 1");
SupporterListener sl = new SupporterListener();
List<TicketDataBean> tdbList = sl.getPendingTickets(application);
if(!tdbList.isEmpty())
{
for(TicketDataBean td : tdbList)
{
ticketNumber.add(td.getNumber());
}
session.put("logined","true");
session.put("TicketNumber",ticketNumber);
}
}
return "success";
}
}
Supporter.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="s" uri="/struts-tags" %>
<%# taglib prefix="sj" uri="/struts-jquery-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>Insert title here</title>
</head>
<body>
<s:form id="formSelectReload" theme="simple" cssClass="yform" action="getApplicationList">
<fieldset>
<div class="type-text">
<label for="application">Application : </label>
<s:url var="remoteurl" action="getApplicationList" namespace="/"/>
<sj:select href="%{remoteurl}" id="application" name="application"
list="applicationNames" onChange="reloadsecondlist" emptyOption="false"
headerKey="-1" headerValue="Please Select a Application" />
</div>
<s:property value="#session.ApplicationNames"/>
<div class="type-text">
<label for="number">Ticket Number: </label>
<s:url var="remoteurl" action="getApplicationList" namespace="/"/>
<sj:select href="%{remoteurl}" id="ticketNumber" formIds="formSelectReload"
reloadTopics="reloadsecondlist" name="number"
list="ticketNumber"
emptyOption="false"
headerKey="-1" headerValue="Please Select a Ticket Number"/>
</div>
<div class="type-button">
<sj:submit
id="submitFormSelectReload"
/>
</div>
</fieldset>
</s:form>
</body>
</html>
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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="login" extends="struts-default">
<action name="login"
class="action.LoginAction"
method="execute">
<result name="success">/Home.jsp</result>
<result name="error">/InvalidLogin.jsp</result>
<result name="INVALID SESSION">/Home.jsp</result>
</action>
<action name="logout"
class="action.LoginAction"
method="logout">
<result name="success">/Login.jsp</result>
</action>
<action name="register">
<result>/Register.jsp</result></action>
<action name="importexcel">
<result>/ImportExcel.jsp</result></action>
<action name="supporter" class="action.SupporterAction"
method="execute">
<result name="success">/Supporter.jsp</result></action>
<action name="registeruser"
class="action.RegisterAction"
method="register">
<result name="success">/UserLogged.jsp</result>
<result name="error">/InvalidLogin.jsp</result>
<result name="INVALID SESSION">/Register.jsp</result>
</action>
<action name="importExcel"
class="action.ImportExcelAction"
method="importExcel">
<result name="success">/Home.jsp</result>
<result name="error">/InvalidLogin.jsp</result>
<result name="INVALID SESSION">/Home.jsp</result>
</action>
</package>
<package name="default" extends="struts-default,json-default" namespace="/">
<action name="getApplicationList"
class="action.SupporterAction" >
<result type="json" />
</action>
</package>
</struts>
jar files
Image of jar files
Result I am getting is
Image of result
As we can see in Image of result, list is being populated but I am not able to get it in drop down. Please suggest something.
Thank you in advance for your answers.
The prepare() interceptor is your friend.
https://struts.apache.org/docs/prepare-interceptor.html

How to invoke prepare method of Preparable Interface after my own custom interceptor

I have created my own Interceptor, MyInterceptor, as follows:
public class MyInterceptor extends MethodFilterInterceptor
{
String inv=null;
#Override
protected String doIntercept(ActionInvocation invocation)
{
System.out.println("MyInterceptor fired");
try
{
inv=invocation.invoke();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return inv;
}
}
My action class is this:
public class MyStruts2Class extends ActionSupport implements Preparable
{
#Override
public void prepare()
{
System.out.println("HI Prepare");
}
public String execute()
{
System.out.println("Hi Execute");
return "success";
}
}
... and my struts.xml is this:
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/> </interceptors>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<interceptor-ref name="myIntercept"/> <result name="success">success.jsp</result>
</action>
</struts>
My problem is when I fire my defaultStrutsAction class, MyInterceptor runs fine but the prepare method in the Action class is not invoked which it should, as I have implemented the Preparable interface.
If I omit this line:
<interceptor-ref name="myIntercept"/>
... in struts.xml, the prepare method in the Action class runs fine.
I am new in Struts2, but can't figure out what is wrong here.
You have to add the prepare interceptor to the interceptor stack for the prepare method to get called in the action. You can do this in a few ways, one is to define the action itself to have the prepare interceptor. Like so. The order matters.
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/> </interceptors>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<interceptor-ref name="myIntercept"/>
<interceptor-ref name="prepare"/>
<result name="success">success.jsp</result>
</action>
</struts>
You can also define the interceptor stack in the interceptors tag and use the default-interceptor-ref tag
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="myIntercept"/>
<interceptor-ref name="prepare"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<result name="success">success.jsp</result>
</action>
</struts>
And finally, what I think your interceptor stack should look like,
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/>
<interceptor-stack name="myStack">
<interceptor-ref name="myIntercept"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<result name="success">success.jsp</result>
</action>
</struts>
In this example, I ref defaultStack instead of just prepare, because defaultStack already has prepare defined along with other common interceptors that you may want to use.
in your example when you omit this line
<interceptor-ref name="myIntercept"/>
the prepare interceptor gets called because it is in the defaultStack already defined by struts2.
Take a look at the stacks defined in link provided by Steven Benitez.
Under default configuration.
http://struts.apache.org/release/2.3.x/docs/interceptors.html
Hope that helps.
here is an example of my reply to your comment.
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="myIntercept" class="org.sagar.intercetors.MyInterceptor"/> </interceptors>
<action name="defaultStrutsAction" class="org.sagar.struts.MyStruts2Class" method="execute">
<interceptor-ref name="myIntercept"/>
<interceptor-ref name="defaultStack"/>
<result name="success">success.jsp</result>
</action>
</struts>

Struts 2 server side validation using XML not working for struts action

I am using Struts 2 Validator framework with XML. But the server side validations are not working. Following is the code snippet.
Struts.xml
<interceptor-stack name="MyStack">
<interceptor-ref name="alias"/>
<interceptor-ref name="servletConfig"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="scopedModelDriven"/>
<interceptor-ref name="modelDriven"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="staticParams"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*,^struts\..*,.*\\.*,.*\(.*,.*\).*,.*#.*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse,reset</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse,reset</param>
</interceptor-ref>
</interceptor-stack>
<action name="process" method="process" class="org.web.action.MyAction">
<interceptor-ref name="MyStack" />
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
<result name="wait">wait.jsp</result>
<result name="input">Index.jsp</result>
</action>
MyAction.java
public class MyAction extends ActionSupport {
private String amount;
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
public String process() throws Exception {
//some processing done here
}
}
MyAction-process-validation.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"##xwork.validator.dtd##">
<validators>
<field name="amount">
<field-validator type="requiredstring" short-circuit="true">
<message key="order.amount.required"/>
</field-validator>
<field-validator type="amountValidator" short-circuit="true">
<message key="order.amount.invalid" />
</field-validator>
</field>
</validators>
AmountValidator.java
public class AmountValidator extends FieldValidatorSupport {
public Struts2DonationAmountValidator() {
super();
}
public void validate(Object object) throws ValidationException {
//some validation check here
}
}
validators.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator Config 1.0//EN"
"https://s3.amazonaws.com/static/xwork-validator-config-1.0.dtd">
<validators>
<validator name="required" class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator" />
<validator name="amountValidator" class="org.web.validator.AmountValidator"/>
</validators>
Index.jsp
<input type="text" id="amountTemp" name="amount">
I have put sysouts at the first line of validator as well as in action method. The sysout in AmountValidator.java is not called. Even while debugging, the validation doesn't get called and the control jumps to action method. There is no hint of any exception in console. The action method flow works fine.
Thanks in advance!!!
I found a other solution on http://struts.apache.org/release/2.1.x/docs/validation.html. I changed the name of my MyAction-process-validation.xml to MyAction-processAction-validation.xml and named my action as processAction instead of process. That means instead of method I used action name.
Thanks for your support.

Struts 2 using tokenSession Interceptor unable to get form values after submit

I have following simple code for login & secured login. For secured login I am using tokenSession interceptor at the time of form submit . In success.jsp I am unable to get the userId value if I submit secureLogin but fine with Login . Can anyone pls help me what is the reason behind it.
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<body><LOGIN.JSP>
<s:form action="login" >
<s:textfield name="userId" label="Login Id"/><br>
<s:password name="password" label="Password"/><br>
<s:submit value="Login" align="center"/>
</s:form>
<s:form action="secureLogin" >
<s:textfield name="userId" label="Login Id"/><br>
<s:password name="password" label="Password"/><br>
<s:token />
<s:submit value="secureLogin" align="center"/>
</s:form>
</body>
</html>
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="tokenSession" class = "org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
</interceptors>
<global-results>
<result name="invalid.token">/error.jsp</result>
</global-results>
<action name="secureLogin" class="com.actions.HelloAction" method="secureLogin">
<result name="success">/success.jsp</result>
<result name="failure">/login.jsp</result>
<interceptor-ref name="tokenSession" />
</action>
<action name="login" class="com.actions.HelloAction" method="login">
<result name="success">/success.jsp</result>
<result name="failure">/login.jsp</result>
</action>
</package>
</struts>
package com.actions;
public class HelloAction {
private String userId;
private String password;
public String execute() {
return "success";
}
public String login(){
return "success";
}
public String secureLogin(){
return "success";
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>SUCCESS.JSP</title>
</head>
<body>
Welcome <b><s:property value="userId"/></b> , you have loged in. <br />
</body>
</html>
you need a interceptor-stack
<struts>
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="tokenSession" class = "org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
<interceptor-stack name="yourStack">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="tokenSession"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="yourStack"/>
<global-results>
<result name="invalid.token">/error.jsp</result>
</global-results>
</package>
</struts>
Here is a link: Interceptors

Resources