如何对具有相同属性名称的两个不同的bean进行jsp:usebean setproperty? [英] How to jsp:usebean setproperty to two different bean having same property name?

查看:295
本文介绍了如何对具有相同属性名称的两个不同的bean进行jsp:usebean setproperty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSP Usebean的新手.当我提交表格时,学生姓名也将设置为大学名称.因为,两个bean具有相同的属性名称.反正有避免这种冲突并设置正确值的方法吗??

I am new to JSP Usebean.. I have tried below scenario. When I submit the form, student name is also setting into college name. Because, both bean has same property name. Is there anyway to avoid this collision and set the correct value.?

Summary.jsp

Summary.jsp

<jsp:useBean id="student" class="demo.jsp.beans.Student" scope="page"></jsp:useBean>
    <jsp:setProperty property="*" name="student"/>
    <jsp:useBean id="college" class="demo.jsp.beans.College" scope="page"></jsp:useBean>
    <jsp:setProperty property="*" name="college"/>

Student.java

Student.java

public class Student {
  private String name;
}

College.java

College.java

public class College {
  private String name;
}

Index.jsp

Index.jsp

<form method="post" action="summary.jsp">
        <table>
            <tr><td> Name</td><td>:</td><td><input name="name"/></td></tr>
            <tr><td> DOB</td><td>:</td><td><input name="dob"/></td></tr>
            <tr><td> College</td><td>:</td><td><input name="name"/></td></tr>
            <tr><td> </td><td></td><td><input type="submit"></td></tr>
        </table>
    </form>

推荐答案

jsp:useBean已过时.只需将豆放入会话中即可:

jsp:useBean is obsolete. Just put beans in the session with:

BeanName beanvar = new BeanName();
beanvar.member = "whatever";
session.setAttribute("sessionvarname", beanvar);

并通过以下方式检索它们:

And retrieve them with:

BeanName var = (BeanName)session.getAttribute("sessionvarname");

在这里甚至没有使用会话范围,而仅使用页面范围的地方,您的代码将是:

Here where you aren't even using session scope but only page scope, your code would be like:

demo.jsp.beans.Student student = new demo.jsp.beans.Student();
student.name = request.getParameter("student_name");

demo.jsp.beans.College college = new demo.jsp.beans.College();
college.name = request.getParameter("college_name");

如果您随后想将它们放入会话中:

If you then wanted to put them in the session:

session.setAttribute("student_bean", student);
session.setAttribute("college_bean", college);

要从会话中检索以便在另一页上使用,请执行以下操作:

To retrieve from session for use on another page:

demo.jsp.beans.Student student = (demo.jsp.beans.Student) session.getAttribute("student_bean");
demo.jsp.beans.College college = (demo.jsp.beans.College) session.getAttribute("college_bean");

您需要将表单更改为输入具有唯一名称的位置:

You need to change your form to where the inputs have unique names:

        <tr><td> Name</td><td>:</td><td><input name="student_name"/></td></tr>
 ...
        <tr><td> College</td><td>:</td><td><input name="college_name"/></td></tr>

这篇关于如何对具有相同属性名称的两个不同的bean进行jsp:usebean setproperty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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