Trigger actions on schedule - struts2

I would need to trigger some action on schedule.
Let's consider this struts.xml :
<action name="myAction" class="app.MyAction">
<result name="success">
myJsp.jsp
</result>
</action>
this java class :
public class MyAction extends ActionSupport{
private String name;
public getName(){ return name; }
public setName(String name){ this.name = name }
#Override
public String execute() throws Exception {
Return SUCCESS;
}
}
And myJsp :
<s:form action="myAction" method="get">
<s:hidden name="name" value="blabla" />
<s:submit key="submit"/>
</s:form>
Now it would be interesting for me to trigger "myAction" every 30 seconds.
Once the jsp loaded, I'd wait 30 seconds and then the page would refresh.
PS: I don't need some ajax technologies

And the winner is... Javascript:
setTimeout(function() {
location.reload();
}, 30000);

Related

displaytag not working with struts 2 use tiles

In my application using struts 2 framework and tiles.
Now, I want to use displaytag to display the data in a database table. I did the following:
in my struts.xml:
<action name="users_admin" class="com.controller.admin.UserAction">
<interceptor-ref name="checkSession" />
<result name="login">/login.jsp</result>
<result type="tiles" name="none">/users_admin.tiles</result>
</action>
In file UserAction:
public class UserAction extends ActionSupport{
private List<Users> listUsers;
private String myActionName;
public String getMyActionName() {
return myActionName;
}
public void setMyActionName(String myActionName) {
this.myActionName = myActionName;
}
public List<Users> getListUsers() {
return listUsers;
}
public void setListUsers(List<Users> listUsers) {
this.listUsers = listUsers;
}
#Override
public String execute() throws Exception {
listUsers = UserServices.selectAll();
return NONE;
}
}
And In file user_admin.jsp(file use in tiles):
<s:actionerror cssStyle="color:red"/>
<div class="well">
<display:table name="listUsers" id="listUsers" requestURI="/UserAction" cellpadding="5px;"
cellspacing="5px;" style="margin-left:50px;margin-top:20px;">
<display:column property="EMAIL" title="Email"/>
<display:column property="NAME" title="Name"/>
</display:table>
</div>
File user model:
package com.model;
public class Users {
private String EMAIL;
private String NAME;
public String getEMAIL() {
return EMAIL;
}
public void setEMAIL(String EMAIL) {
this.EMAIL = EMAIL == null ? null : EMAIL.trim();
}
public String getNAME() {
return NAME;
}
public void setNAME(String NAME) {
this.NAME = NAME == null ? null : NAME.trim();
}
}
When I run the application error occurs in apache tomcat log
org.apache.catalina.core.ApplicationDispatcher.invoke Servlet.service() for servlet jsp threw exception
java.lang.ClassNotFoundException: org.apache.commons.collections.IteratorUtils
Who can help me point out why the error, in the solution to overcome it?
Add commons-collections-3.1.jar to your libraries.
Note that you are using only one Interceptor, unless checkSession is the name of your stack.
EDIT
Interceptor checkSession to check my log on the website. How should I use.
You can define a custom interceptor stack, and then reference it, or include your custom interceptor AND another stack (for example, the default one) for a single action, like this:
<action name="users_admin" class="com.controller.admin.UserAction">
<interceptor-ref name="checkSession" />
<interceptor-ref name="defaultStack" />
<result name="login">/login.jsp</result>
<result type="tiles" name="none">/users_admin.tiles</result>
</action>

Could not access bean in action Struts2

I have index.jsp where I have one link,on clicking on that link that page specific action gets called.Now as on the click of this link,I need to display a page with already populated multiselect list along with few input text fields,in the constructor of the action I populated the TransactionBean which will be bound with fields on next page i.e transactionData.jsp.
The transactionData.jsp page is getting displayed correctly with populated multiselected list.Now user can select values from multiselect list and can enter dates in text field and will click on click button,so that a bar chart is displayed.
On click on Click button ,I am calling another action ,which also has TransactionBean as its property.In the execute method of this action, I am trying to access transactionbean with its getter but it gives me NullPointerException. I got to know that, if we are submitting a page which has fields of bean binded ,then on calling action,bean will be instantiated automatically through interceptors but seems like something is not correct here.
index.jsp
<%# 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>
<title>Hello World</title>
</head>
<body>
<s:form action="displayAction.action">
<h1>Hello World</h1>
Transaction Chart
</s:form>
</body>
</html>
DisplayAction.java
package com.tutorialspoint.struts2;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class DisplayAction extends ActionSupport {
TransactionBean transactionBean;
public TransactionBean getTransactionBean() {
return transactionBean;
}
public void setTransactionBean(TransactionBean transactionBean) {
this.transactionBean = transactionBean;
}
public String execute() {
return SUCCESS;
}
public DisplayAction() {
System.out.println("Inside Constructor");
List<String> leftChannelsList = new ArrayList<String>();
leftChannelsList.add("Channel1");
leftChannelsList.add("Channel2");
//TransactionBean transactionBean = new TransactionBean();
setTransactionBean(new TransactionBean());
getTransactionBean().setLeftChannelsList(leftChannelsList);
//Transaction Type Dta
List<String> leftTransTypesList = new ArrayList<String>();
leftTransTypesList.add("TransType1");
leftTransTypesList.add("TransType2");
getTransactionBean().setLeftTransTypesList(leftTransTypesList);
}
}
transactionData.jsp
<%# 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>
<s:head theme="ajax" debug="true"/>
<title>Hello World</title>
</head>
<body bgcolor="grey">
<s:form action="displayChart.action">
<s:datetimepicker label="Select From" name="transactionBean.fromDate" displayFormat="MM-dd-yy" required="true" /> <s:datetimepicker label="Select To" name="transactionBean.toDate" displayFormat="MM-dd-yy" required="true" />
<s:optiontransferselect
label="Channels"
name="transactionBean.leftChannels"
leftTitle="Unselected Channels"
rightTitle="Selected Channels"
list="transactionBean.leftChannelsList"
multiple="true"
headerKey="-1"
doubleList="transactionBean.rightChannelsList"
doubleName="transactionBean.rightChannels"
doubleHeaderKey="-1"
doubleHeaderValue="Please Select"/>
<!-- Transaction Types -->
<s:optiontransferselect
label="transaction Types"
name="transactionBean.leftTransTypes"
leftTitle="Unselected Transaction Type"
rightTitle="Selected Transaction Type"
list="transactionBean.leftTransTypesList"
multiple="true"
headerKey="-1"
doubleList="transactionBean.rightTransTypesList"
doubleName="transactionBean.rightTransTypes"
doubleHeaderKey="-1"
doubleHeaderValue="Please Select"/>
<s:submit value="click" align="center"/>
</s:form>
</body>
</html>
JfreeChartAction.java
public class JfreeChartAction extends ActionSupport {
private JFreeChart chart;
private TransactionBean transactionBean;
private TransactionDao transactionDao;
public TransactionDao getTransactionDao() {
return transactionDao;
}
public void setTransactionDao(TransactionDao transactionDao) {
this.transactionDao = transactionDao;
}
public TransactionBean getTransactionBean() {
return transactionBean;
}
public void setTransactionBean(TransactionBean transactionBean) {
this.transactionBean = transactionBean;
}
// This method will get called if we specify <param name="value">chart</param>
public JFreeChart getChart() {
return chart;
}
public void setChart(JFreeChart chart) {
this.chart = chart;
}
public JfreeChartAction() {}
public String execute() throws Exception {
System.out.println("Inside Execute: Start");
System.out.println("From date:" + getTransactionBean().getFromDate());
System.out.println("From date:" + getTransactionBean().getToDate());
System.out.println("leftChannelsList:" + getTransactionBean().getLeftChannelsList());
System.out.println("Left Trans type List" + getTransactionBean().getLeftTransTypesList());
DefaultCategoryDataset dataSet = new DefaultCategoryDataset();
dataSet.setValue(0, "01-04-2014", "Channel1");
dataSet.setValue(15000, "01-04-2014", "Channel2");
dataSet.setValue(9000, "01-05-2014", "Channel1");
dataSet.setValue(1500, "01-05-2014", "Channel2");
dataSet.setValue(10000, "01-06-2014", "Channel1");
dataSet.setValue(8000, "01-06-2014", "Channel2");
chart = ChartFactory.createBarChart(
"Demo Bar Chart", //Chart title
"Mobile Manufacturer", //Domain axis label
"TRANSACTIONS", //Range axis label
dataSet, //Chart Data
PlotOrientation.VERTICAL, // orientation
true, // include legend?
true, // include tooltips?
false // include URLs?
);
chart.setBorderVisible(true);
System.out.println("Inside Execute: End");
return SUCCESS;
}
}
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>
<package name="default" namespace="/" extends="struts-default">
<action name="displayAction"
class="com.tutorialspoint.struts2.DisplayAction"
method="execute">
<result name="success">transactionsData.jsp</result>
</action>
</package>
<package name="defaultJfreeChart" namespace="/" extends="jfreechart-default">
<action name="displayChart"
class="com.tutorialspoint.struts2.JfreeChartAction"
method="execute">
<result name="success" type="chart">
<param name="value">chart</param>
<param name="type">jpeg</param>
<param name="width">600</param>
<param name="height">400</param>
</result>
</action>
</package>
</struts>
TransactionBean.java
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.jfree.chart.JFreeChart;
public class TransactionBean {
private Date dateTime;
private Integer volume;
private String leftChannels;
private String rightChannels;
private String toDate;
private String fromDate;
private List<String> leftChannelsList;
private List<String> rightChannelsList;
//Transaction type data
private String leftTransTypes;
private List<String> leftTransTypesList;
private String rightTransTypes;
private List<String> rightTransTypesList;
public Date getDateTime() {
return dateTime;
}
public void setDateTime(Date dateTime) {
this.dateTime = dateTime;
}
public Integer getVolume() {
return volume;
}
public void setVolume(Integer volume) {
this.volume = volume;
}
public TransactionBean(){
//System.out.println("Inside TransactionBean constructor");
}
public String getLeftTransTypes() {
return leftTransTypes;
}
public void setLeftTransTypes(String leftTransTypes) {
this.leftTransTypes = leftTransTypes;
}
public List<String> getLeftTransTypesList() {
return leftTransTypesList;
}
public void setLeftTransTypesList(List<String> leftTransTypesList) {
this.leftTransTypesList = leftTransTypesList;
}
public String getRightTransTypes() {
return rightTransTypes;
}
public void setRightTransTypes(String rightTransTypes) {
this.rightTransTypes = rightTransTypes;
}
public List<String> getRightTransTypesList() {
return rightTransTypesList;
}
public void setRightTransTypesList(List<String> rightTransTypesList) {
this.rightTransTypesList = rightTransTypesList;
}
public String getToDate() {
return toDate;
}
public void setToDate(String toDate) {
this.toDate = toDate;
}
public String getFromDate() {
return fromDate;
}
public void setFromDate(String fromDate) {
this.fromDate = fromDate;
}
public String getLeftChannels() {
return leftChannels;
}
public void setLeftChannels(String leftChannels) {
this.leftChannels = leftChannels;
}
public String getRightChannels() {
return rightChannels;
}
public void setRightChannels(String rightChannels) {
this.rightChannels = rightChannels;
}
public List<String> getRightChannelsList() {
return rightChannelsList;
}
public void setRightChannelsList(List<String> rightChannelsList) {
this.rightChannelsList = rightChannelsList;
}
public List<String> getLeftChannelsList() {
return leftChannelsList;
}
public void setLeftChannelsList(List<String> leftChannelsList) {
this.leftChannelsList = leftChannelsList;
}
}
can you please let me know which version should be used.
Always use the latest version. At the moment it's Struts 2.3.16.3 GA. You can also see Version Notes and Migration Guide.

Input values from jsp are null in Struts action class. How to access the registration form input values in Action class

Registration.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<center>
<%# taglib uri="/struts-tags" prefix="s"%>
<s:form action="getRegistered.action" method="post" validate="true">
<div>
<table>
<s:textfield label="First Name" key="firstname" />
<s:textfield label="Last Name" key="lastname" />
<s:password label="Create your password" key="regpassword" />
<s:password label="Confirm your password" key="conpassword" />
<s:textfield label="Email" key="regemail1" />
<s:textfield label="Re-Type Email" key="conemail" />
<s:textfield label="Phone" key="phone" />
<tr>
<td><s:submit value="Register" theme="simple"/></td>
<td><s:submit value="Cancel" theme="simple" onclick="document.forms[0].action='login.jsp';" /></td>
</tr>
</table>
</div>
</s:form>
</center>
</body>
</html>
I'm passing the register input values to Action class.
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>
<package name="default" extends="struts-default">
<action name="getLogin" class="login.action.LoginAction"
method="login">
<result name="success">/Success.jsp</result>
<result name="input">/LoginError.jsp</result>
</action>
<action name="getRegistered" class="login.action.LoginAction"
method="register">
<interceptor-ref name="validation">
<param name="excludeMethods">register</param>
</interceptor-ref>
<result name="success">/Success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
This is the struts xml
LoginAction.java:
package login.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import com.opensymphony.xwork2.ActionSupport;
import login.service.LoginDao;
import login.service.RegisterDao;
#SuppressWarnings("serial")
public class LoginAction extends ActionSupport implements ServletRequestAware,
ServletResponseAware {
private String username;
private String password;
private HttpServletRequest httpServletRequest;
private String firstname;
private String lastname;
private String regpassword;
private String conpassword;
private String regemail;
private String conemail;
private String phone;
public String register(){
RegisterDao rdao = new RegisterDao();
System.out.println("firstname action::: "+firstname);
rdao.registerdao(firstname,lastname,regpassword,conpassword,regemail,conemail,phone);
return SUCCESS;
}
public String login() {
httpServletRequest.getSession().setAttribute("key", username);
httpServletRequest.getSession().setAttribute("key", password);
LoginDao db = new LoginDao();
Boolean validate = db.loginresult(username, password);
if (validate == true) {
return SUCCESS;
} else {
return INPUT;
}
}
public HttpServletRequest getHttpServletRequest() {
return httpServletRequest;
}
public void setHttpServletRequest(HttpServletRequest httpServletRequest) {
this.httpServletRequest = httpServletRequest;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getRegpassword() {
return regpassword;
}
public void setRegpassword(String regpassword) {
this.regpassword = regpassword;
}
public String getConpassword() {
return conpassword;
}
public void setConpassword(String conpassword) {
this.conpassword = conpassword;
}
public String getRegemail() {
return regemail;
}
public void setRegemail(String regemail) {
this.regemail = regemail;
}
public String getConemail() {
return conemail;
}
public void setConemail(String conemail) {
this.conemail = conemail;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public void setServletRequest(HttpServletRequest request) {
this.httpServletRequest = request;
}
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;
}
#Override
public void setServletResponse(HttpServletResponse arg0) {
// TODO Auto-generated method stub
}
}
When i submit the registration form , the input values pass to Action class is null. How can i access the registration values in action class under register() method.
The above issue is resolved .
Now i'm facing another strange issue .. When i submit the form action = getLogin.action , its always returning INPUT from the interceptor . I dont wish to exclude the method login from the validator.
How could i resolve it ?
When i change the struts xml like below , its redirecting to success jsp . But i dont want to exclude the login method from validation
<action name="getLogin" class="login.action.LoginAction"
method="login">
<interceptor-ref name="defaultStack">
<param name="validation.excludeMethods">login</param>
</interceptor-ref>
<result name="success">/Success.jsp</result>
<result name="input">/LoginError.jsp</result>
</action>
You are using only one Interceptor, you need to use the whole Stack:
Change this:
<interceptor-ref name="validation">
<param name="excludeMethods">register</param>
</interceptor-ref>
to this
<interceptor-ref name="defaultStack">
<param name="validation.excludeMethods">register</param>
</interceptor-ref>

Struts 2.0 error

I am developing a simple HelloWorld struts application. I am doing the configuration using struts.xml file. the welcome page is displayed but when I click on the form submit button, it gives 404 error.
Kindly provide some pointers.
action class:
package client;
public class Hello {
public String name;
public String message;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() {
message = "Hello " + name;
return "success";
}
}
namecollect.jsp
<s:form action="hello" method="post">
<s:textfield name="name">Enter name</s:textfield>
<s:submit></s:submit>
</s:form>
struts.xml
<package name="default" namespace="/" extends="struts-default">
<action name="hello" class="client.Hello">
<result name="success">hello.jsp</result>
</action>
</package>
This is the package structure:
Your config file is in the wrong place.
By default struts.xml is expected to be at the root of your classpath.

Struts2 : Interceptor is being only one time instead of two times

I am working on Struts2 Interceptors .
I have read that Struts2 Interceptors are just like Filters , which execute before the Action class is executed and one more time after processing the result ( Please correct me if i am wrong ) , that is two times
But when i ran the below code , the interceptors are executed only once .
Please correct me if i made any mistake .
Please see my code below :
This is My Struts.xml file
<struts>
<constant name="struts.devMode" value="true" />
<package name="test" extends="struts-default">
<interceptors>
<interceptor name="loginkiran" class="vaannila.MyLoginInterCeptor" />
</interceptors>
<action name="HelloWorld" class="vaannila.HelloWorld" method="kiran">
<interceptor-ref name="loginkiran" />
<result name="SUCCESS">/success.jsp</result>
</action>
</package>
</struts>
This is my Action class
public class HelloWorld
{
public HelloWorld() {
}
public String kiran() {
System.out.println("iNSIDE THE aCTION CLASS");
return "SUCCESS";
}
}
This is my Interceptor class
public class MyLoginInterCeptor implements Interceptor {
#Override
public void destroy() {
// TODO Auto-generated method stub
System.out.println("Destroying Interceptor");
}
#Override
public void init() {
}
#Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
System.out.println("iNSIDE THE iNTERCEPTOR");
return invocation.invoke();
}
}
This is my JSP File :
<html>
<body>
<%
System.out.println("iNSIde THE jsp");
%>
</body>
</html>
The Output for the above code is this :
iNSIDE THE iNTERCEPTOR
iNSIDE THE aCTION CLASS
iNSIde THE jsp
Interceptors are not executed twice (nor are filters): interceptors (and filters) wrap the action (or servlet/etc.)
public String intercept(ActionInvocation invocation) throws Exception {
System.out.println("Before action invocation...");
return invocation.invoke();
System.out.println("After action invocation...");
}

Resources