• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to redirect to same page in struts 2

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
Am new to Struts 2 so please help me with this.

Am having a page called 1.jsp in that am having a text field called Name and button SUBMIT.
On click of submit button am taking name value to action class from there i connected to database and get a value called ID.
Now I have to take that ID to the same jsp(1.jsp) and I have to display over there.

So what exactly has to be written in struts.xml?
And after redirecting to same page will the text field will have the name which i had entered or it will show blank. I need that text filed to show the value
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what happens if you return the InputForward() from your action?
 
Kartik Tal
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi omar thanks for the reply, I havent got your point. Please see the code which i have written
In action class am setting 24 which i need to show in same jsp

Struts.xml
-------------------
<action name="doLogin" class="com.wham.action.LoginAction">
<result name="success">1.jsp</result>
</action>

1.JSP
-------------
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Login Success</title>
</head>
<body>
<s:form action="doLogin" method="POST" theme="simple">
Hello
<s:textfield name="customerName" id="customerName" theme="simple" />
<s:textfield name="myId" id="myId" theme="simple" />
<input type="submit" value="Save" class="button" />
</s:form>
</body>
</html>

Action
-----------
public class LoginAction extends ActionSupport implements org.apache.struts2.util.ServletContextAware,
ServletRequestAware,ServletResponseAware
{
ServletContext servletContext;

public String execute() throws Exception
{
ServiceUtil.setServletContext(servletContext);
ApplicationContext ctx = ServiceUtil.getApplicationContext();
PlannedTaskService plannedService = (PlannedTaskService)ctx.getBean("plannedService");

if(getCustomerName.equals("Kartik")))
{
setMyId("24");
}

return SUCCESS;
}

private String customerName = null;
private String myId = null;
private HttpServletRequest request;
private HttpServletResponse response;

/**
* @return the customerName
*/
public String getCustomerName() {
return customerName;
}

/**
* @param customerName the customerName to set
*/
public void setCustomerName(String customerName) {
this.customerName = customerName;
}

public String getMyId() {
return myId;
}
public void setMyId(String myId) {
this.myId = myId;
}

/* (non-Javadoc)
* @see org.apache.struts2.util.ServletContextAware#setServletContext(javax.servlet.ServletContext)
*/
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;

}

/**
* @param request the request to set
*/
public void setServletRequest(HttpServletRequest request){
this.request = request;
}

/**
* @return the request
*/
public HttpServletRequest getServletRequest(){
return request;
}

/**
* @param response the response to set
*/
public void setServletResponse(HttpServletResponse response){
this.response = response;
}

/**
* @return the response
*/
public HttpServletResponse getServletResponse(){
return response;
}

}
 
Omar Al Kababji
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
return INPUT instead of SUCCESS
 
Kartik Tal
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thaks Omar it worked fine.............
 
Kartik Tal
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi the above suggestion had really helped me a lot, but right now am including Ajax functionality to this .
This is the link which i followed
http://javachamp.blogspot.com/2008/06/struts-2-ajax-drop-down-example.html
This code is working perfectly for populating ajax results.

But when i am redirecting the page to same page am not getting the dropdowns.
Could anyone help me in this.
 
reply
    Bookmark Topic Watch Topic
  • New Topic