JSP - 使用具有多个参数的Bean方法处理表单

时间:2013-03-22 22:25:35

标签: java html eclipse google-app-engine jsp

我有这个表格,想要处理它:

<form id="myForm" class="form-horizontal" action="/user" method="post">
  <fieldset>
    <div class="control-group">
      <!-- Text input-->
      <label class="control-label" for="input01">Email:</label>
      <div class="controls">
        <input name="email" placeholder="email" class="input-xlarge" type="text"
          value="<%=?????????">
      </div>
    </div>

    <div class="control-group">
      <!-- Text input-->
      <label class="control-label" for="input01">Password:</label>
      <div class="controls">
        <input name="password" placeholder="password" class="input-xlarge" type="text"
          value="<%=request.getParameter("password")%>">
      </div>
    </div>
  </fieldset>
</form>

</div>

<div class="modal-footer">
  <a href="#" class="btn" data-dismiss="modal">Close</a> 
    <input class="btn btn-primary" type='button' value='Save Changes'
      onclick='document.forms["myForm"].submit();'>
</div>

</div>

但是,我有一个带有两个参数的bean方法,我尝试使用:

来处理这个问题
public void insert(String email, String password) {
    User entry = new User(email, password);
    PersistenceManager pm = PMF.get().getPersistenceManager();
    pm.makePersistent(entry);
}

我的问题是,如何正确地将Bean连接到使用两个参数的表单?

1 个答案:

答案 0 :(得分:0)

您是否创建了bean java文件来连接这两个?功能bean文件由以下三部分组成:

  1. 用作初始化的构造函数
  2. 吸气剂
  3. 塞特斯
  4. 在以下几行中,您将找到一个基于您在上面发布的表单的简单示例。记住System.out.println的旧技巧,检查你写的所有内容是否正确。

    Bean文件,我们称之为userData.java

    package blabla;
    
    import java.io.Serializable;
    
    public class UserData implements Serializable {
    
    private String email;
    private String password;
    
    //1.Constructor
    public UserData()
    {
        email="";
        password="";
    
    }
    
    //2.Getters
    public String getEmail(){
    return email;
    }
    
    public String getPassword(){
    return password;
    }
    
    //3.Setters - Caution: we use different variables here
    
    public void setEmail(String eml)
    {
    this.email=eml;
    }
    
    public void setPassword(String pswd)
    { 
    this.password=pswd;
    }   }
    

    另一方面,.jsp文件应该像这样 的的index.jsp

    <%--Blablabla headers and stuff, I will only write the things needing to be added--%>
    <jsp:useBean id="entry" class="packagePath.UserData" scope="application"/>
    <%--For analytic info about the parameters of the jsp call above, check:         http://www.javatpoint.com/jsp-useBean-action--%>
    <jsp:setProperty name="entry" property="*"/>
    <%--Code to invoke all the setters of the bean--%>
    <p><jsp:getProperty name="entry" property="email"/></p>
    <p><jsp:getProperty name="entry" property="password"/></p>
    <%--The two lines are not obligatory to use; you will need them only if you wan tto print the results on the page--%>
    <%-- rest of the code%-->