如何在 JSP 中访问 ActionBeanContext?

发布于 2024-10-15 01:50:01 字数 2169 浏览 2 评论 0 原文

我是 Stripes 的新手,很感谢每一个让我更接近正常运行的网络应用程序的提示!

技术设置:java、动态Web项目、stripes、jsp

场景

用户可以登录(index.jsp)。正确的电子邮件地址和密码 (LoginFormActionBean.java) 后,用户将转到欢迎页面 (loggedin.jsp)。 该欢迎页面上的内容类似于“欢迎<用户名>,您已成功登录!”。

实现

我在index.jsp中有一个表单,我在其中获取用户输入并将其传递给LoginFormActionBean.java中的方法 -->作品!

在相应的方法中,我检查用户是否正确,如果正确,我将用户插入到 ActionBeanContext 中:

getContext.setUser(loggedinUser);

之后我转发到loggedin.jsp:

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

loggedin.jsp 包含以下重要行:

<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>
...

LoggedinBean.java 包含 MyActionBeanContext 属性(如 LoginFormActionBean.java)。

为了从我使用的上下文中获取用户对象:

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

此外,LoggedinBean.java 包含一个方法,该方法用 @DefaultHandler 注释并转发到login.jsp(同一页面)

结果

现在,会发生什么是:正确登录后,我被转发到login.jsp, “${loggedinBean.context.user}”行为空,< 也是空的。 s:文本>-字段。

但是单击“PrintUser”按钮后,< s:text > - “TextForm”中的字段 - 表单中填写的是登录用户的用户对象!

结论

我认为发生的情况是,在我手动执行 bean 中的方法之前,不会调用 LoggedinBean.java 的“setContext()”方法。因为在我按下按钮之前,bean 中的“setContext()”方法没有被调用!

在线文档说要在 JSP 中使用上下文属性只需编写“${ actionBean.context.user}”。但上下文为空!

甚至“pragmatic stripes”(2008)一书也没有提供有关使用 ActionBeanContext 的更多信息。

问题

那里发生了什么?

我怎样才能得到“${loggedinBean.context.user}”行来显示登录的用户?

我怎样才能得到< s:text >-field 在加载 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 LoginFormActionBean.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失眠症患者 2024-10-22 01:50:01

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文