开发者

How do I access the ActionBeanContext within a JSP?

开发者 https://www.devze.com 2023-02-07 04:44 出处:网络
I\'m new to Stripes and appreciate every hint that brings me nearer to a functioning web-app! technological setup: java, dynamic web project, stripes, jsp

I'm new to Stripes and appreciate every hint that brings me nearer to a functioning web-app!

technological setup: java, dynamic web project, stripes, jsp

scenario:

users can login (index.jsp). After correct email-adress and password (LoginFormActionBean.java), the user is forwarded to a welcoming page (loggedin.jsp). The content on this welcoming page is something like "welcome < username >, you've been successfully logged in!".

implementation:

i have a form in the index.jsp where i take the user input and pass it to a method in the LoginFormActionBean.java --> works!

in the corresponding method i check whether the user is correct and if so, i insert the user in the ActionBeanContext:

getContext.setUser(loggedinUser);

after that i forward to the loggedin.jsp:

return new ForwardResolution("/loggedin.jsp");

the loggedin.jsp contains following important lines:

<jsp:useBean id="loggedinBean" class="mywebapp.controller.LoggedinBean" scope="session" />
...
${loggedinBean.context.user} //show the whole user object
...
<s:form beanclass="mywebapp.controller.LoggedinBean" name="ButtonForm">
    <s:submit name="foo" value="PrintUser" />
</s:form>

<s:form beanclass="mywebapp.controller.LoggedinBean" name="TextForm">
    <s:text name="user" />
</s:form>
...

the LoggedinBean.java contains a MyActionBeanContext attribute (like the LoginFo开发者_StackOverflow中文版rmActionBean.java).

to get the userobject out of the context i use:

public String getUser(){
    return getContext().getUser().toString();
}

furthermore the LoggedinBean.java contains a method, which is annotated with @DefaultHandler and forwards to loggedin.jsp (the same page)

result:

now, what happens is: after logging in correctly, i'm forwarded to the loggedin.jsp, the line "${loggedinBean.context.user}" is empty and so is the < s:text >-field.

BUT after clicking the "PrintUser" Button, the < s:text >-field in the "TextForm"-form is filled with the user object of the logged in user!

conclusion:

what i think happens, is that the "setContext()" method of the LoggedinBean.java is not called before i manually execute a method in the bean. Because the "setContext()" method in the bean is not called before i press the button!

the online documentation says to use a context attribute in a JSP just write "${actionBean.context.user}". But the context is null!

even the book "pragmatic stripes"(2008) gives no more information about using the ActionBeanContext.

question:

what happens there?

how can i get the "${loggedinBean.context.user}" line to display the logged in user at all?

and how can i get the < s:text >-field to display the user object after loading the JSP, but without pressing the button?

i hope my problem is clear and my remarks are satisfying


I would like to recommend the usage of the MVC pattern. This pattern will lead to an implementation were the Action Beans will act as controllers that handle all http requests and the JSP pages will become passive views with little logic, only accessible via the Action Bean controllers (no direct access to JSP pages any more!).

If you use this pattern, you always have an "actionBean" available in your JPS and thus you can refer to ${actionBean.context} (see: getContext).

0

精彩评论

暂无评论...
验证码 换一张
取 消