struts2 下拉 [英] struts2 drop down

查看:34
本文介绍了struts2 下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 List 在 struts2 中创建一个下拉菜单.我如何创建一个动作类,以便在加载 .jsp 页面时,它首先填充列表?

I need to create a drop down menu in struts2 from List. How can I create an action class so that when a .jsp page is loaded, it populates the list first?

我找到了这个链接 http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/ ,它也能工作,但用户必须访问 http://localhost:8080/Struts2Example/selectAction.action 使其工作,但如果有人直接转到 select.jsp 怎么办.>

i found this link http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/ , it works too but for it work user must go to http://localhost:8080/Struts2Example/selectAction.action to make it work but what if someone were to directly go to select.jsp.

推荐答案

由于您使用的是 .jsp,因此您可以在渲染 标签.

Since you're using a .jsp, you could load the dropdown with a scriptlet before you render the <s:select> tag.

但是,更好的做法是允许操作执行加载并将 .jsp 文件隐藏在/WEB-INF 下,这样它们就不能直接访问.执行此操作的常用方法是准备拦截器.

However, it's better practice to allow the action to perform the loading and hide the .jsp files under /WEB-INF so they're not directly accessible. A common approach to perform this is the Prepare interceptor.

如果您的拦截器堆栈中有它,它会在您的操作中自动调用具有以下名称的任何方法在调用请求的方法之前:

If you've got it in your interceptor stack, it will automatically invoke any method with the following name in your action before invoking the requested method:

  • 准备{MethodName}()
  • prepareDo{MethodName}()
  • 准备()

这意味着您可以在 Action 中执行以下操作:

That means you can do something like the following in your Action:

public class YourAction extends ActionSupport {

    public String prepare(){
         // populate your drop down object
    }   

    public String view(){
         // forward to your jsp
         return SUCCESS;
    }

}

然后你所要做的就是调用你的 action 的 view() 方法,而准备将首先被 Struts 调用.

Then all you have to do is call your action's view() method and prepare will be called first by Struts.

这篇关于struts2 下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆