Avoid a loop in index page - struts2

I have this 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>
</head>
<body>
<h2>Simple Iterator</h2>
<ol>
<s:iterator value="comboMeals">
<li><s:property /></li>
</s:iterator>
</ol>
<h2>Iterator with IteratorStatus</h2>
<table>
<s:iterator value="comboMeals" status="comboMealsStatus">
<tr>
<s:if test="#comboMealsStatus.even == true">
<td style="background: #CCCCCC"><s:property/></td>
</s:if>
<s:elseif test="#comboMealsStatus.first == true">
<td><s:property/> (This is first value) </td>
</s:elseif>
<s:else>
<td><s:property/></td>
</s:else>
</tr>
</s:iterator>
</table>
</body>
</html>
This is my Java Class:
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class IteratorKFCAction extends ActionSupport{
private List<String> comboMeals;
public List<String> getComboMeals() {
return comboMeals;
}
public void setComboMeals(List<String> comboMeals) {
this.comboMeals = comboMeals;
}
public String execute() {
comboMeals = new ArrayList<String>();
comboMeals.add("Snack Plate");
comboMeals.add("Dinner Plate");
comboMeals.add("Colonel Chicken Rice Combo");
comboMeals.add("Colonel Burger");
comboMeals.add("O.R. Fillet Burger");
comboMeals.add("Zinger Burger");
return SUCCESS;
}
}
My idea was call directly the action that fill the index page, so i put this line inside head tags of index.jsp
<META HTTP-EQUIV="Refresh" CONTENT="0;URL='start.do'">
But with with this fix, i get that the page enter in a "refresh loop". Is there any way to call directly from the code the action, in this way I don't have to set it manually via URL in the browser.
I've tried also a second solution adding to the body of index.jsp this code:
<s:action name="iteratorKFCAction" executeResult="true" />
where iteratorKFCAction is the action specified in struts.xml that recall the IteratorKFCAction. In this case the action loop.

The main purpose of Struts2 (and all other MVC frameworks) is to route the URL to an Action (the Controller) that prepare data (in your case comboMeals) and determine a result (in your case always SUCCESS) that is mapped to a template (the View), in your case index.jsp
In your struts.xml you should have something like
<action name="start.do" class="com.xxx.IteratorKFCAction">
<result name="SUCCESS">/WEB-INF/.../index.jsp</result>
</action>

Related

how we can add one by one row in table by using struts

I want to show data in tabular format when we click add button one by one keep adding in table . only use struts 2 not Ajax and Jquery.
home.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sx" uri="/struts-dojo-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
<table>
<tr>
<td><s:textfield label="Sainumber"
name="stockcreationbean.sainumber" /></td>
<td><table><tr><td><sx:datetimepicker
name="stockcreationbean.saidate" displayFormat="MM-dd-yy"
label="SaiDate" /></td></tr></table></td>
<td><s:textfield label="Challan"
name="stockcreationbean.challan"></s:textfield></td>
<td><table><tr><td><sx:datetimepicker name="stockcreationbean.challandate"
displayFormat="MM-dd-yy" label="ChallanDate"></sx:datetimepicker></td></tr></table></td>
</tr>
<tr>
<td><s:textfield label="BillNumber"
name="stockcreationbean.billnumber"></s:textfield></td>
<td><table><tr><td><sx:datetimepicker name="stockcreationbean.billdate"
displayFormat="MM-dd-yy" label="BillDate"></sx:datetimepicker></td></tr></table></td>
<td><s:textfield name="stockcreationbean.ponumber"
label="PoNumber"></s:textfield></td>
<td><table><tr><td><sx:datetimepicker name="stockcreationbean.podate"
displayFormat="MM-dd-yy" label="PoDate"></sx:datetimepicker></td></tr></table></td>
</tr><tr><td><s:textfield name="stockcreationbean.filenumber"
label="FileNumber"></s:textfield></td>
</tr>
</table>
<table>
<s:iterator value="toparraylist">
<tr>
<s:iterator value="tablelist">
<td><s:property value="saidate"/></td>
</s:iterator>
</tr>
</s:iterator>
</table>
<s:submit value="Submit" onclick="formSubmit(this.form)"></s:submit>
</body>
</html>
This is action class that we can use for adding data in array list .One is local array list and other is global array list then call getData() method when we click add button then add local arraylist object to global arraylist object
DisplayAction.java
public class DisplayAction extends ActionSupport {
ArrayList<Object> toparraylist = new ArrayList<Object>();
public String execute() {
return "success";
}
public String getData() throws NamingException, Exception {
ArrayList<Object> tablelist=new ArrayList<Object>();
tablelist.add(stockcreationbean);
toparraylist.add(tablelist);
return "success";
}
public ArrayList<Object> getToparraylist() {
return toparraylist;
}
public void setToparraylist(ArrayList<Object> toparraylist) {
this.toparraylist = toparraylist;
}
}
#Mack Struts 2 is a server-side framework for java. If I decode your requirement it tells me that you want to update something on table(at client) and it gets updated on-by-one by using struts 2(at server).
Ajax was fits your requirement perfectly and is the only solution for your requirement, as updating table is an asynchronous call to server.

Struts2: sx:autocompleter not working

i have a sx:autocompleter tag in welcome.jsp as shown below. Although, the autocompleter action is getting invoked, the result is not rendering properly and autocomplete is not working.(i have included struts2-dojo-plugin-2.3.8.jar in WEB-INF/lib.)
(DEBUG output in browser shows following message.
DEBUG: please consider using a mimetype of text/json-comment-filtered to avoid potential security issues with JSON endpoints
Details at end of question.)
Following is my code.
welcome.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="/struts-tags" prefix="s"%>
<%# taglib prefix="sx" uri="/struts-dojo-tags"%>
................................
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
<sx:head debug="true"/>
</head>
<body>
<s:form action="sub">
.........
<s:url var="fruitsurl" action="fruits"></s:url>
<sx:autocompleter label="Fruits" name="autocompleter1" href="%{fruitsurl}" list="fruitsList"></sx:autocompleter>
..........
<s:submit></s:submit>
</s:form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
....................
<struts>
<package name="some-default" extends="struts-default">
<action name="fruits" class="mypack.FruitsAction" method="display">
<result name="success">welcome.jsp</result>
</action>
<action name="sub" class="mypack.SubmitAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
FruitsAction.java
package mypack;
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
public class FruitsAction extends ActionSupport{
private List fruitsList;
public List getFruitsList() {
return fruitsList;
}
public void setFruitsList(List fruitsList) {
this.fruitsList = fruitsList;
}
public String display() throws Exception {
fruitsList = new ArrayList();
fruitsList.add("apples");
fruitsList.add("oranges");
fruitsList.add("mangoes");
return SUCCESS;
}
}
i have set debug="true" in sx:head and the following DEBUG output is shown in browser when
welcome.jsp is invoked.
DEBUG: please consider using a mimetype of text/json-comment-filtered to avoid potential security issues with JSON endpoints
DEBUG: [SyntaxError: Syntax error]
DEBUG:<!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>Welcome</title><script language="JavaScript" type="text/javascript">
// Dojo configuration djConfig = { isDebug: true,
bindEncoding: "UTF-8"
,baseRelativePath: "/Suburbs/struts/dojo/"
,baseScriptUri: "/Suburbs/struts/dojo/" ,parseWidgets : false
};</script> <script language="JavaScript" type="text/javascript"
src="/Suburbs/struts/dojo/struts_dojo.js"></script>
<script language="JavaScript" type="text/javascript"
src="/Suburbs/struts/ajax/dojoRequire.js"></script>
<script language="JavaScript" type="text/javascript">
dojo.hostenv.writeIncludes(true);</script>
<link rel="stylesheet" href="/Suburbs/struts/xhtml/styles.css" type="text/css"/>
<script language="JavaScript" src="/Suburbs/struts/utils.js" type="text/javascript"></script>
<script language="JavaScript" src="/Suburbs/struts/xhtml/validation.js" type="text/javascript"></script>
<script language="JavaScript" src="/Suburbs/struts/css_xhtml/validation.js" type="text/javascript"></script>
</head><body>
<form id="sub" name="sub" action="/Suburbs/sub.action" method="post">
<table class="wwFormTable"><tr>
<td class="tdLabel"><label for="sub_autocompleter1" class="label">Fruits:</label></td>
<td
> <input dojoType="struts:ComboBox" dataUrl="/Suburbs/fruits.action" id="sub_autocompleter1" name="autocompleter1" keyName="autocompleter1Key" visibleDownArrow="true" />
<option value="apples">apples</option>
<option value="oranges">oranges</option>
<option value="mangoes">mangoes</option> </select></td></tr>
<script language="JavaScript" type="text/javascript">djConfig.searchIds.push("sub_autocompleter1");</script>
<tr>
<td colspan="2"><div align="right"><input type="submit" id="sub_0" value="Submit"/>
</div></td></tr></table></form></body></html>
please guide me as to what could be wrong with the code.
thanks,
Your action needs to return JSON, as described on the Ajax and JavaScript Recipes page. (Link to previous version due to a wiki export issue). Nutshell: use the JSON plugin.
<action name="fruits" class="mypack.FruitsAction" method="display">
<result type="json">
<param name="root">fruits</param>
</result>
</action>
Note that I'm explicitly setting the "root" element to your list of fruits. I'm also eliminating the redundancy in the name of the list of fruits; it's a list of fruits–its name should be fruits.
Lastly, the Dojo plugin has been deprecated for at least a couple of years now, for a variety of reasons. I'd strongly consider using something like the Struts 2 jQuery Plugin or simply using raw Dojo if you have a need for Dojo explicitly.
Thanks to Dave Newton, i have below solution to my issue.
i modified the code to use JSON along with autocompleter and
included struts2-json-plugin-2.3.8.jar in WEB-INF/lib.
providing modified code below:
welcome.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="/struts-tags" prefix="s"%>
<%# taglib prefix="sx" uri="/struts-dojo-tags"%>
...........
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
<sx:head/>
</head>
<body>
<s:form action="sub">
.......................
<s:url var="fruitsurl" namespace="/autocompleter" action="getfruits"></s:url>
<sx:autocompleter label="Fruits" name="autocompleter1" href="%{fruitsurl}"></sx:autocompleter>
.......................
<s:submit></s:submit>
</s:form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
............
<struts>
<package name="autocompleter" namespace="/autocompleter" extends="json-default">
<action name="getfruits" class="mypack.FruitsAction">
<result type="json">
<param name="root">fruits</param>
</result>
</action>
</package>
<package name="some-default" extends="struts-default">
<action name="sub" class="mypack.SubmitAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
FruitsAction.java
package mypack;
import java.util.HashMap;
import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
public class FruitsAction extends ActionSupport{
Map<String,String> fruits;
public Map<String, String> getFruits() {
fruits=new HashMap<String,String>();
fruits.put("apples", "ap");
fruits.put("oranges", "or");
fruits.put("mangoes", "ma");
return fruits;
}
public void setFruits(Map<String, String> fruits) {
this.fruits = fruits;
}
}
Follwing is the solution using jquery plugin instead of dojo plugin.
(Replaced dojo plugin with struts2-jquery-plugin-3.6.0.jar in WEB-INF/lib
in Eclipse.)
welcome.jsp
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%#taglib uri="/struts-tags" prefix="s"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
..................
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Welcome</title>
<sj:head jqueryui="true"/>
</head>
<body>
<s:form action="sub">
.......................
<s:url var="fruitsurl" namespace="/autocompleter" action="getfruits"></s:url>
<sj:autocompleter label="Fruits" name="autocompleter1" href="%{fruitsurl}"></sj:autocompleter>
......................
<s:submit></s:submit>
</s:form>
</body>
</html>
struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
................
<struts>
<package name="autocompleter" namespace="/autocompleter" extends="json-default">
<action name="getfruits" class="mypack.FruitsAction">
<result type="json" name="success">
<param name="root">fruitNames</param>
</result>
</action>
</package>
<package name="some-default" extends="struts-default">
<action name="sub" class="mypack.SubmitAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
FruitsAction.java
package mypack;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.opensymphony.xwork2.ActionSupport;
#SuppressWarnings("serial")
public class FruitsAction extends ActionSupport{
private String term;
List<String> fruitList=new ArrayList<String>();
private String[] fruitNames;
#Override
public String execute() throws Exception {
fruitList.add("apples");
fruitList.add("mangoes");
fruitList.add("pears");
fruitList.add("grapes");
if (StringUtils.isNotBlank(term)){
ArrayList<String> subList=new ArrayList<String>();
for (int i=0;i<fruitList.size();i++){
if (StringUtils.startsWithIgnoreCase(fruitList.get(i), term)){
subList.add(fruitList.get(i));
}
}
fruitNames=subList.toArray(new String[subList.size()]);
}else{
fruitNames=null;
}
return SUCCESS;
}
public String[] getFruitNames() {
return fruitNames;
}
public void setTerm(String term) {
this.term = term;
}
}
in FruitsAction.java, the string 'term' is what we type into the autocompleter.
the action gets invoked everytime we type in a new letter.
i referred the following urls for the above solution.
http://code.google.com/p/struts2-jquery/wiki/AutocompleterTag
http://www.coderanch.com/t/537518/Struts/struts-jquery-autocompleter

Load ASP-MVC View with Ext.Panel loader

I have following layout, related to the one i am describing in this question
<ext:Viewport runat="server">
<Items>
<ext:Panel Region="North" runat="server" ID="TopBar" />
<ext:Panel Region="West" runat="server" ID="Nav" >
<Items>
<ext:Button runat="server" ID="loaderComponentFooBar">
<DirectEvents>
<Click OnEvent="loadFooBar" />
</DirectEvents>
</ext:Button>
//Some more Buttons each with his own loader Function in Code-Behind
</Items>
</ext:Panel>
<ext:Panel runat="server" Region="center" ID="MainPanel" />
</Items>
</ext:Viewport>
on Button-Click i now want to load my View # "/Foo/Bar/" to the MainPanel
How can i achieve this in CodeBehind?
I already tried:
public void loadFooBar(object sender, DirectEventArgs e)
{
MainPanel.Loader.Url = "/Foo/Bar/"; //Throws null reference Exception
MainPanel.Loader.Url = "~/Foo/Bar/"; // same as above
MainPanel.Loader.GetUrlByRoute("Foo/Bar/"); //again the same
MainPanel.Loader.LoadContent("/Foo/Bar/");
//and after each of these attempts except the last went:
MainPanel.Loader.Mode = LoadMode.Frame;
MainPanel.Loader.LoadMask.ShowMask = false;
MainPanel.Loader.LoadContent();
}
Just by the way, i am using aspx-web-application views
I think you should not use a page's code behind for DirectEvents' handlers. It is going out of the MVC concept. You should refer a controller's actions.
Here is an example.
Example View
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%# Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html>
<html>
<head runat="server">
<title>Ext.Net.MVC v2 Example</title>
</head>
<body>
<ext:ResourceManager runat="server" />
<ext:Viewport runat="server" Layout="BorderLayout">
<Items>
<ext:Panel runat="server" Region="West" Width="100">
<Items>
<ext:Button
runat="server"
Text="Load View 1"
DirectClickUrl=<%# Url.Action("LoadView1") %>
AutoDataBind="true" />
<ext:Button
runat="server"
Text="Load View 2"
DirectClickUrl=<%#Url.Action("LoadView2") %>
AutoDataBind="true" />
</Items>
</ext:Panel>
<ext:Panel ID="MainPanel" runat="server" Region="Center">
<Loader runat="server" Mode="Frame" AutoLoad="false">
<LoadMask ShowMask="true" />
</Loader>
</ext:Panel>
</Items>
</ext:Viewport>
</body>
</html>
Example Controller
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
namespace Work2MVC.Controllers
{
public class AspxController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult View1()
{
return View();
}
public ActionResult View2()
{
return View();
}
public ActionResult LoadView1()
{
Panel p = X.GetCmp<Panel>("MainPanel");
p.LoadContent(Url.Action("View1"));
return this.Direct();
}
public ActionResult LoadView2()
{
Panel p = X.GetCmp<Panel>("MainPanel");
p.LoadContent(Url.Action("View2"));
return this.Direct();
}
}
}
Also you can initiate a load request on client side via JavaScript. Then you avoid an additional request.

Problems with Login application in struts2

I am trying to write small login application in struts 2.Session is b eing created successfully.In welcome.jsp "logout" option is given.On logout control will be redirected to Logout.jsp.
My problem is after logout session variables are destroyed but pages are storing in browser cache.If click back button of browser i am able to see welcome.jsp.
For clearing cache "ClearCacheInterceptor" is used.I don't understand where i am making mistake.
Instead of clearing browser every time is there any to overcome this prooblem ? Is my approach correct ? Please suggest me.
Login.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#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=UTF-8">
<title>Login</title>
</head>
<body>
<s:form action="login">
<s:textfield name="myname"></s:textfield>
<s:submit value="submit"></s:submit>
</s:form>
</body>
</html>
Struts.xml
<interceptors>
<interceptor name="clear-cache" class="ActionClasses.ClearCacheInterceptor" />
</interceptors>
<action name="login" class="ActionClasses.LoginAction" >
<interceptor-ref name="clear-cache" />
<result name="success">Welcome.jsp</result>
<result name="error">Login.jsp</result>
</action>
<action name="logout" class="ActionClasses.Logout">
<interceptor-ref name="clear-cache" />
<result name="success">Logout.jsp</result>
</action>
LoginAction.java
package ActionClasses;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.ValidatorType;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.Interceptor;
public class LoginAction extends ActionSupport implements SessionAware
{
/**
*
*/
private static final long serialVersionUID = 1L;
private String myname;
private Map<String , Object> s;
public String execute()throws Exception
{
s=ActionContext.getContext().getSession();
s.put("login", myname);
return "success";
}
public void setMyname(String s)
{
myname=s;
}
public String getMyname()
{
return myname;
}
#Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
s=arg0;
}
}
ClearcacheInterceptor.java
package ActionClasses;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.StrutsStatics;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class ClearCacheInterceptor extends AbstractInterceptor{
/**
*
*/
private static final long serialVersionUID = 1L;
#Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext context=(ActionContext)invocation.getInvocationContext();
HttpServletResponse response=(HttpServletResponse)context.get(StrutsStatics.HTTP_RESPONSE);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setDateHeader("Expires", 0);
String result=invocation.invoke();
System.out.println("check result="+result);
return result;
}
}
Logout.java
package ActionClasses;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class Logout extends ActionSupport {
public String execute(){
Map<String,Object> s=ActionContext.getContext().getSession();
s.remove("login");
ActionContext.getContext().getSession().clear();
return "success";
}
}
Welcome.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# page contentType="text/html; charset=UTF-8"%>
<%# taglib prefix="s" uri="/struts-tags"%>
<s:include value="CheckLogin.jsp"></s:include>
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
</head>
<body>
<font color="white"></font>
Welcome<s:property value="#session['login']"/>
<s:a href="logout">Logout</s:a>
</body>
</html>
Logout.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
log out successful !!
</body>
</html>
CheckLogin.jsp
<%# taglib prefix="s" uri="/struts-tags" %>
<%# page language="java" contentType="text/html" import="java.util.*"%>
<html>
<head>
<title>Check validate!</title>
</head>
<body>
This is session validation page!
<s:if test="#session.login != 'Jagan'">
<jsp:forward page="Login.jsp" />
</s:if>
</body>
</html>
Well that's a very common issue and this is with respect to your browser cache issue rather than struts2 or any other framework at all.
we have face same problem since when you hit the back button of browser the request is not being send to the server rather it is being serves from the browser cache.you will only notice things when you try to do some work and it will come up with error that you are no longer being logged in.
though you can use certain header like no-cache etc but they are being obeyed by the browser is not certain.
only workaround to this problem as per my understanding is to use https (secure browsing) for your work and than use the header (no-cache. cache-expiry etc) since when you browse application under secure mode these header will be followed by the server and browser.
i hope this will try to give you an idea, just to check redirect to https protocol when your logout and it will solve your problem

file upload in strurts 2

i need file upload in strurts 2 and store it to server location,i used by googles no use, can anybody give better idea. advance thanks
To google, it might help to check what you type: it's "struts2", no "strurts"
Anyway, you dont need to google, the official docs are enough
http://struts.apache.org/2.2.1/docs/file-upload-interceptor.html
http://struts.apache.org/2.2.1/docs/handling-file-uploads.html
http://cwiki.apache.org/WW/file-upload.html
Here is whole code of struts2 file upload.
Action file (fileupload.java)
package com.tutorialspoint.struts2;
import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.IOException;
import com.opensymphony.xwork2.ActionSupport;
public class uploadFile extends ActionSupport{
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;
public String execute()
{
/* Copy file to a safe location */
destPath = "C:/apache-tomcat-6.0.33/work/";
try{
System.out.println("Src File name: " + myFile);
System.out.println("Dst File name: " + myFileFileName);
File destFile = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);
}catch(IOException e){
e.printStackTrace();
return ERROR;
}
return SUCCESS;
}
public File getMyFile() {
return myFile;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getMyFileContentType() {
return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
this.myFileFileName = myFileFileName;
}
}
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>File Upload</title>
</head>
<body>
<form action="upload" method="post" enctype="multipart/form-data">
<label for="myFile">Upload your file</label>
<input type="file" name="myFile" />
<input type="submit" value="Upload"/>
</form>
</body>
</html>
success.jsp
<%# page contentType="text/html; charset=UTF-8" %>
<%# taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>File Upload Success</title>
</head>
<body>
You have successfully uploaded <s:property value="myFileFileName"/>
</body>
</html>
Cheers.

Resources