aspose file tools*
The moose likes Struts and the fly likes How to redirect to same page in struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to redirect to same page in struts 2 " Watch "How to redirect to same page in struts 2 " New topic
Author

How to redirect to same page in struts 2

Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
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
Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357
what happens if you return the InputForward() from your action?


Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
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

Joined: Jan 13, 2009
Posts: 357
return INPUT instead of SUCCESS
Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
Thaks Omar it worked fine.............
Kartik Talasu
Ranch Hand

Joined: Aug 21, 2009
Posts: 98
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to redirect to same page in struts 2