Bean set属性

发布于 2024-10-16 12:01:00 字数 1095 浏览 4 评论 0原文

大家好:) 我在使用 Bean 时对 setPropety 有误解吗? 当我这样制作时,

 <jsp:setProperty name="myBean" property="*">

我必须使表单的字段名称与 myBean 中的属性名称相同 为了使 HTTPRequest 的值与 myBean 的属性之间匹配, 如果我在表单字段中使用其他名称,则值将达到空值。 但是如果我想在同一个jsp页面中使用表单值,并定义保存表单值的Bean,那么如果我在表单字段中使用其他名称而不是bean属性的相同名称,那就没有意义了!!这是为什么 ??

看到这里,密码字段的名称是“password”,myBean的属性名称是pass,甚至它可以工作!

//index.jsp page
<form method="post" action="index.jsp">
        Enter Your email:<input type="text" name="email"/>
        <br/>
        Enter Your Password :<input type="password" name ="pass">
         <br/>
        <input type="submit" name ="submit"/>
    </form>

    <jsp:useBean id="info" class="beans.info" scope="page">
        <jsp:setProperty name="info" property="*"/>
    </jsp:useBean>
        Your email is : <jsp:getProperty name="info" property="email"/>
          <br/>
         Your Pass is : <jsp:getProperty name="info" property="pass"/>
</body>

有人可以告诉我发生了什么事吗?

编辑:我对代码进行了修改。

Hello everybody :)
I have misunderstanging in setPropety in using Bean ?
when I make like this

 <jsp:setProperty name="myBean" property="*">

I have to make the field names of the form the same as attribute names in myBean
in order to make match between values is HTTPRequest and the attributes of myBean,
if I use onther names in the form fields, values are reached null values.
but If I ever want to use the form values in the same jsp page , and define Bean that holds the values of the form, it doesn't make sence if I use other names in the form fields rather than the same name of bean attributes !! why is that ??

see here, the name of password field is "password" , and the name of attribute of myBean is pass, and even that it works !

//index.jsp page
<form method="post" action="index.jsp">
        Enter Your email:<input type="text" name="email"/>
        <br/>
        Enter Your Password :<input type="password" name ="pass">
         <br/>
        <input type="submit" name ="submit"/>
    </form>

    <jsp:useBean id="info" class="beans.info" scope="page">
        <jsp:setProperty name="info" property="*"/>
    </jsp:useBean>
        Your email is : <jsp:getProperty name="info" property="email"/>
          <br/>
         Your Pass is : <jsp:getProperty name="info" property="pass"/>
</body>

can some one tell me what has happened?

Edit: I make modification to the code.

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

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

发布评论

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

评论(2

等待圉鍢 2024-10-23 12:01:00
<input type="password" name ="pass">

您的密码字段的名称也是pass

其他原因可能是您没有运行您认为正在运行的代码。请注意,bean 名称 infoo 是错误的。

<input type="password" name ="pass">

The name of your password field is also pass.

Other cause could be that you aren't running the code you think you're running. Note that the bean name infoo is wrong.

五里雾 2024-10-23 12:01:00

我遇到了和你一样的问题,我刚刚做了这个例子,它有效!

HTML 页面

<form name="registrationForm" id="registrationForm" method="post" action="registerBean.jsp" >
Full Name:* 
<input type="text" name="fullname" id="fullname"/> 
  </form>

registerBean.jsp

<jsp:useBean id="userBean" scope="session" class="Code.UserBean" />
<jsp:setProperty name="userBean" property="fullName" param="fullname" />

重点关注 (param="") 属性,因为它一方面负责 html 表单字段名称,另一方面负责 javabean 之间的匹配。


UserBean.java

    public class UserBean
    {
        private String fullName;
 /**
     * @return the fullName
     */
    public String getFullName()
    {
        return fullName;
    }

    /**
     * @param fullName the fullName to set
     */
    public void setFullName(String fullName)
    {
        this.fullName = fullName;
    }
}

I faced the same problem as you, and I just made this example and it works!

HTML page

<form name="registrationForm" id="registrationForm" method="post" action="registerBean.jsp" >
Full Name:* 
<input type="text" name="fullname" id="fullname"/> 
  </form>

registerBean.jsp

<jsp:useBean id="userBean" scope="session" class="Code.UserBean" />
<jsp:setProperty name="userBean" property="fullName" param="fullname" />

Focus on (param="") attribute, because it's the one that will make the matching between the html form field name on one hand and javabean on the other hand.


UserBean.java

    public class UserBean
    {
        private String fullName;
 /**
     * @return the fullName
     */
    public String getFullName()
    {
        return fullName;
    }

    /**
     * @param fullName the fullName to set
     */
    public void setFullName(String fullName)
    {
        this.fullName = fullName;
    }
}

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