0

我有这个表格并想处理它:

<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 与使用两个参数的表单正确连接?

4

1 回答 1

0

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

  1. 用作初始化的构造函数
  2. 吸气剂
  3. 二传手

在以下几行中,您将找到一个基于您上面发布的表单的简单示例。记住 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%-->
于 2013-05-10T13:42:52.567 回答