Struts JSP previous link - java

Does anyone know how I can retrieve the previous JSP URL that a page has come from within a JSP?
Can I retrieve this from the session/ request/ response object?
Hope this makes sense, Thank you

Many thanks - apologies for not being able to mark my correct answer as the site seems to have changed and i cant see how to do this but i used

Even simpler:
<%= request.getHeader("Referer") %>

(assuming Struts 1)
Well in the struts-config.xml there is a input parameter in the action element (in the action-mappings section)
So in your action (java class) you can access this value like this :
public ActionForward action(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
//do some stuff
return mapping.getInputForward(); //return to the caller
}
so if you want an action to return to its originating page, you can define
multiple action entry pointing to the same action but with a different name and input value.
Hope that helps.

Assuming that you are talking about navigating within your own application and assuming struts 2 you can pass a parameter to the action, lets call it next, with the name of the next action. Lets say you have an action defined in struts.xml:
<action name="myaction" class="com.me.MyActionClass">
<result name="success">${next}</result>
</action>
In MyActionClass you will have to declare the property next with its getters and setters. On invoking this action you must provide the value for next which could be the same name as the page from which you are invoking the action.

for example in referer i got the uri as http://www.sun.com/questions?pnr=18&value=gg
while i pront referer at that time i got this string ok. now i need to get the parameter value how to get it. from that string

Related

How to replace an execute ActionForward method

I'm using struts for the very first time,
The thing is, I've an ActionForward execute method in my java class,
and in my jsp page I invoke another action fooAction that it's also declared in my java class
tag:page namePag="<%=pagnum%>"
urlAccion="/foo.do?dispatch=fooAction"
idParam="numPage" />
Despite this, every action in my jsp is directed to the execute method, and if I change the name of the execute method, the page redirects to a blank page,
So how can I remove/rename the execute method without affecting the functionality
In struts V1.1, by default it executes the "Execute Method". If you are very particular to redirect the request to a defined page.
Do this change in action mapping attribute
<action path="/fooAction" type="org.apache.struts.actions.ForwardAction" parameter="/pages/fooActionJsp.jsp"/>
Point the jsp to the appropriate page using the parameter

Struts 2:How to load values to a <s:select >from a list in session

I am new to struts. I want to load a list of data in the session to a select tag <s:select> which equals to pure html <select><option>values..</option></select> . The data might be loaded from database and put them to a list. I looked for Internet. But it all didn't work for me. Please any one let me know how to do this or provide any link with working example.( including the action class,struts.xml and jsp page. most needed codes are enough.)
As long as you have the list of values in a java.util.List on the stack, you should be fine with something like this:
<s:select label="Some label"
list="yourList"
name="somName" />
You can find a sample here: http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/
I am not sure why you want to place List in the session?
Struts2 provides a clean way to put your request/response data in Valuestack and its OGNL system provides a very clean way to access those data from the value stack.All you need to have a list in your action class with its getter and setters and at UI use build in struts2 tag to access those data.Here is a simple code to accomplish this
Action Class
public Class MyAction extends ActionSupport{
private List<String> myList;
//getter and setter for myList
public String execute() throws Exception{
myList=new ArrayList<String>();
// fill the list
return SUCCESS;
}
}
At UI level you need to use S2 select tag like
JSP
<s:select label="MyList"
name="myList"
headerKey="-1" headerValue="Select Value"
list="myList"
/>
This is all you need to do. For mapping this in struts.xml its quite straight forwards and all you need to configure your action name and its respected class.Hope this will help you.
For more details about S2, i suggest to refer official doc.
Still if you want to put the list in session in your java class and want to access it in jsp here is the JSP code
%{#session.MyList}
Struts 2.3.1 Documenation

How to access a request attribute set by a servlet in JSP?

I'm trying to retrieve attribute values set by a servlet in a JSP page, but I've only luck with parameters by ${param}. I'm not sure about what can I do different. Maybe its simple, but I couldn't manage it yet.
public void execute(HttpServletRequest request, HttpServletResponse response) {
//there's no "setParameter" method for the "request" object
request.setAttribute("attrib", "attribValue");
RequestDispatcher rd = request.getRequestDispatcher("/Test.jsp");
rd.forward(request,response);
}
In the JSP I have been trying to retrieve the "attribValue", but without success:
<body>
<!-- Is there another tag instead of "param"??? -->
<p>Test attribute value: ${param.attrib}
</body>
If I pass a parameter through all the process (invoking page, servlets and destination page), it works quite good.
It's available in the default EL scope already, so just
${attrib}
should do.
If you like to explicitly specify the scope (EL will namely search the page, request, session and application scopes in sequence for the first non-null attribute value matching the attribute name), then you need to refer it by the scope map instead, which is ${requestScope} for the request scope
${requestScope.attrib}
This is only useful if you have possibly an attribute with exactly the same name in the page scope which would otherwise get precedence (but such case usually indicate poor design after all).
See also:
Our EL wiki page
Java EE 6 tutorial - Expression Language
Maybe a comparison between EL syntax and scriptlet syntax will help you understand the concept.
param is like request.getParameter()
requestScope is like request.getAttribute()
You need to tell request attribute from request parameter.
have you tried using an expression tag?
<%= request.getAttribute("attrib") %>
If the scope is of request type, we set the attribute using request.setAttribute(key,value) in request and retrieve using ${requestScope.key} in jsp .

accessing the values of class in jsp page from the action class

I am doing one project on Struts 2 that I am getting knowledge little by little bit..
I have the action like this
<action name="backaction" class="HandlerAction">
<result name="user_profile" type="redirect">hai.jsp</result>
In the class Handler action I have the object userprofile in which the name and age are members.
In the execute function
log.info(userprofile.getName())//it is giving name xyz
return "user_profile"
I am getting hai.jsp but I am unable to retreive the value in that object userprofile in jsp.
hai.jsp is as follows..
<%#taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<s:textfield name="user_name" value="%{userprofile.name}"/>
</body>
</html>
I tried by putting as value="%{name}" also but i am not getting the value xyz..
The problem is with
<result name="user_profile" type="redirect">hai.jsp</result>
since you are using redirect result type which means that framework will create a new Request and response object and discarding old request/response Object.So when you are returning from your action your user-object is theirs in the value stack till you tell S2 that you want to use redirect result type.
On seeing redirect result type, it will discard any existing data and will create a new request for you place its content in value stack and that's why this is not working for you.I am not sure why you using redirect result type since you can do the same using any build in result type say success.
If you still want to use redirect result, i suggest you to save the user-profile data in either Session and retrieve it in your next action or use scope-interceptor
Struts2 Redirect Result
Calls the {#link HttpServletResponse#sendRedirect(String) sendRedirect} method to the location specified. The response is told to redirect the browser to the specified location (a new request from the client). The consequence of doing this means that the action (action instance, action errors, field errors, etc) that was just executed is lost and no longer available. This is because actions are built on a single-thread model. The only way to pass data is through the session or with web parameters (url?name=value) which can be OGNL expressions.

How to lookup configured Struts action

I want to do the following:
final Action myAction = getActionDefinedInStrutsConfig(param);
myAction.execute(params);
Is there a way to lookup the actions that the ActionServlet has initialized ?
I can create a new one like so:
final Action myAction = new ActionImpl();
myAction.execute(params);
but this way the new action is not properly initialized, the attached servlet is not set and getServlet() returns null.
A little clarification on why I need this:
The problem is that I currently have 2 login pages. One for normal users and one for admins. They should be separate systems completely, but the fact is they're currently not. I need to make a 'proxy' login page which decides which login page to redirect to according to the request. If I redirect to the URL however the UI would be drawn. I need to call the either the user or admin login actions to process my proxy page request. Also moving the logic inside a service, while being the correct approach, is not currently an option.
Ok, since I see what you mean here's what I would suggest:
Use your action for validatory purposes, i.e., retrieval of data from ActionForm and checking for validity. Once all information is done, send the info to a service.
The service (need not to be a web service, but a simple POJO) will have the business logic of the application, with relevant exceptions and return types. Once you call the relevant service with its method, retrieve the result and, finally,
Populate your necessary ActionForm or do a mapping.findForward().
This way, if you need another business logic that is used by another Struts Action, rather call the service that the 2nd action uses. This is an effective way for code-reuse and good OOP practise.
Hope this helps.
The hackable way would be to do this:
final Action myAction = new ActionImpl();
myAction.setServlet(getServlet());
/* ONLY if your form enctype is "multipart/request-data". */
myAction.setMultipartRequestHandler(getMultipartRequestHandler());
//Finally
myAction.execute(params);
You can define actions in your forwards:
<action parameter="command" path="/firstAction"
input="firstAction.tiles" name="someForm" scope="session" validate="true" type="com.mycompany.FirstAction">
<forward name="toSecond" path="/secondAction.do?command=someMethod" redirect="true"/>
</action>
<action parameter="command" path="/secondAction" input="secondAction.tiles" name="someForm"
scope="session" validate="true" type="com.mycompany.SecondAction">
<forward name="backToFirst" path="/firstAction.do?command=myMethod" redirect="true" />
</action>
Now you can use mapping.findForward("toSecond"), in your first action and mapping.findForward("backToFirst") on the other.

Categories