Problems with Login application in struts2 - 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

Related

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

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

Avoid a loop in index page

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>

Struts2 Pass parameters with NO Session

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");

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

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