java 从 JSP 访问 Struts ActionForm 中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13552826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:12:13  来源:igfitidea点击:

Accessing a value in Struts ActionForm from JSP

javastrutsstruts-action

提问by Mr Morgan

I am a Struts 1.3.10 newbie and I have an issue where I have an Actioncalled RegistrationActionas follows:

我是一个Struts 1.3.10新手,我有一个问题,我有一个ActionRegistrationAction如下:

    public final class RegistrationAction extends Action{

        @Override
        public ActionForward execute(ActionMapping mapping,
                                     ActionForm form,
                                     HttpServletRequest request,
                                     HttpServletResponse response)
        throws Exception{

           RegistrationForm formBean = (RegistrationForm) form;
           String userid = formBean.getUserid();
           String pwd = formBean.getPassword();

The userid and password are then saved to a HashMap<String, String>with attributes _useridand pwd.

然后将用户 ID 和密码保存到HashMap<String, String>具有属性_useridpwd.

RegistrationActionthen calls a JSP if the validation of the useridand passwordare successful. But what I am finding is that in the JSP, the userid is not being displayed using the following code:

RegistrationAction然后调用JSP如果的验证useridpassword成功。但我发现在 JSP 中,没有使用以下代码显示用户 ID:

    <h1>Hello  <bean:write name="RegistrationForm" property="_userid" />!</h1>

The matching ActionForm RegistrationFormcontains the _useridfield as below:

匹配ActionForm RegistrationForm包含如下_userid字段:

    public final class RegistrationForm extends ActionForm{

        private String _userid = null;

        public String getUserid(){ return _userid; }
        public void   setUserid(String userid){ _userid = userid; }

        ...

I know that an instance of RegistrationFormis being populated because I can retrieve the entered _useridvia:

我知道RegistrationForm正在填充一个实例,因为我可以_userid通过以下方式检索输入:

    if(err == RegistrationModel.OK){
            System.out.println("Here " + model.getUserid()); 

I thought that a reference to the RegistrationFormin the JSP such as:

我认为RegistrationForm在 JSP 中引用了诸如:

<h1>Hello <bean:write name="RegistrationForm" property="_userid" />!</h1>

<h1>Hello <bean:write name="RegistrationForm" property="_userid" />!</h1>

Would work.

会工作。

Can anyone suggest where I'm wrong?

谁能建议我错在哪里?

Thanks to the respondents. The page works now.

感谢答主。该页面现在工作。

采纳答案by JB Nizet

The JSP tags, and the JSP EL, access bean properties, and not bean fields. So if you pass _userId, it will look for a getter method called get_userId(). Since you want to access the getter getUserId(), you need to use userIdinside the tag.

JSP 标记和 JSP EL 访问 bean属性,而不是 bean字段。所以如果你通过了_userId,它会寻找一个名为 的getter 方法get_userId()。由于要访问 getter getUserId(),因此需要userId在标签内使用。

回答by Udo Held

Try

尝试

<h1>Hello  <bean:write name="RegistrationForm" property="userid" />!</h1>

it doesn't matter what the internal name / coding of your formbean is. All what matters is how the getters and setters are named. Your getter is named getUserid()to the javabean property is userid.

您的 formbean 的内部名称/编码是什么并不重要。重要的是如何命名 getter 和 setter。您的 getter 被命名getUserid()为 javabean 属性 is userid

回答by someone

1) Add your RegistrationForm to request

1)添加您的注册表格以请求

request.setAttribute("RegistrationForm",formBean);

2) Remove _ from your _userId variable

2)从您的 _userId 变量中删除 _