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.
Related
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
I have a requirement where passing parameters from one action to another action with No sessions using: Here is my code, I am trying to use scope-interceptor.
here is my code I am not sure what am I doing wrong but I am not able to get the results.
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="HelloWorld" class="com.tutorials4u.helloworld.HelloWorld">
<result name="SUCCESS">/success.jsp</result>
</action>
<action name="FirstAction" class="com.tutorials4u.helloworld.FirstAction">
<interceptor-ref name="basicStack" />
<interceptor-ref name="scope">
<param name="session">myName</param>
<param name="key">person</param>
<param name="type">start</param>
</interceptor-ref>
<result name="SUCCESS">/success1.jsp</result>
</action>
<action name="SecondAction" class="com.tutorials4u.helloworld.SecondAction">
<interceptor-ref name="scope">
<param name="session">myName</param>
<param name="key">person</param>
</interceptor-ref>
<interceptor-ref name="basicStack" />
<result name="SUCCESS">/success2.jsp</result>
</action>
</package>
</struts>
*index.jsp*
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<s:form action="HelloWorld" >
<s:textfield name="userName" label="User Name" />
<s:submit />
</s:form>
</body>
</html>
*Success.jsp*
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<s:property value="person.myName"></s:property>
<s:form action="FirstAction" >
<s:submit value="Click for first Action"></s:submit>
</s:form>
</body>
</html>
*success1.jsp*
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<s:property value="person.myName"></s:property>
<h1><s:property value="message" /></h1>I am in FIRST ACTION
<s:form action="SecondAction">
<s:submit value="click for 2nd action"></s:submit></s:form>
</body>
</html>
*success2.jsp*
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%#taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello World</title>
</head>
<body>
<h1><s:property value="message" /></h1>2ND ACTION EXECUTED
<s:property value="person.myName"></s:property>
</body>
</html>
**ACTIONS**
package com.tutorials4u.helloworld;
import com.opensymphony.xwork2.ActionSupport;
/**
*
*
*/
public class HelloWorld extends ActionSupport{
private static final long serialVersionUID = 1L;
private String message;
private String userName;
public HelloWorld() {
}
public String execute() {
setMessage("Hello " + getUserName());
return "SUCCESS";
}
/**
* #return the message
*/
public String getMessage() {
return message;
}
/**
* #param message the message to set
*/
public void setMessage(String message) {
this.message = message;
}
/**
* #return the userName
*/
public String getUserName() {
return userName;
}
/**
* #param userName the userName to set
*/
public void setUserName(String userName) {
this.userName = userName;
}
}
*FirstAction.java*
package com.tutorials4u.helloworld;
import com.opensymphony.xwork2.ActionSupport;
public class FirstAction extends ActionSupport {
/**
*
*/
private String myString;
private Person person;
private static final long serialVersionUID = 1L;
public FirstAction(){
}
public String execute() {
return "SUCCESS";
}
public String getMyString() {
return myString;
}
public void setMyString(String myString1) {
this.myString = myString1;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
package com.tutorials4u.helloworld;
public class Person {
private String myName;
public String getMyName() {
return myName;
}
public void setMyName(String myName1) {
myName1 = "test string";
this.myName = myName1;
}
}
SecondAction.java
package com.tutorials4u.helloworld;
import com.opensymphony.xwork2.ActionSupport;
public class SecondAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private Person person;
public SecondAction(){
}
public String execute() {
return "SUCCESS";
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
---I would really appreciate if you can help me on where I was doing things wrong..
TIA
so we are instantiating session object in each and every other action
right?
Not at all, we are just retrieving it from the ServletActionContext.
How to put in session
ServletActionContext.getContext().getSession().put("softuser", someUserObject);
How to retrieve from session
ServletActionContext.getContext().getSession().get("softuser");
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.
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
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.