【问题标题】:Struts 2.0 request getparameter values to Bean objectStruts 2.0 向 Bean 对象请求 getparameter 值
【发布时间】:2014-06-19 20:50:27
【问题描述】:

我有一个 jsp 页面,其中包含以下员工详细信息。

AddEmp.jsp

<th>First Name :</th>
<td><input type = "text" id ="firstName" name = "firstName" value= ""></td>
<th>Middle Name :</th>
<td><input type = "text" id ="middleName" name = "middleName" value= ""></td>
<th>Last Name :</th>
<td><input type = "text" id ="lastName" name = "lastName" value= ""></td>

提交表单后,我得到了控制器中的值

public class contTest extends ActionSupport{
  public String firstName;
  public String lastName;
  public String execute(){
    System.out.println("firstName-->>>"+firstName);
    System.out.println("lastName-->>>"+lastName);
    return SUCCESS;
  }
}

Struts 2.0 自动设置值(定义为 Public)并可以访问它。

我的问题是——!!我有一个 Employee 的 DAO,其所有详细信息如下。

 public class Employee implements java.io.Serializable{
     public String firstName;
     public String lastName;

    public String getFirstName(){
     return firstName;
    }
    public void setFirstName(String firstName){
     this.firstName = firstName;
    }
    public String getLastName(){
     return lastName;
    }
    public void setLastName(String lastName){
     this.lastName = lastName;
    }
 }

一旦我的 HTML 表单从 JSP 提交,我希望将所有字段设置为 DAO,并将直接调用控制器内部的 DAO,而不是手动定义控制器中的每个字段。有人可以帮忙吗?提前谢谢。

P.S : 我是 struts 2.0 的新手。

更新 1: 谢谢你们的回复。我嫉妒你的知识。

我尝试了 方式,这正是我想要的结果。 我接受 回复作为答案。 你能说出我们实现的概念的名称吗?我喜欢学习并知道我们的朋友 建议的模型驱动逻辑的好处。

谁能用一些简单的例子详细说明一下?

【问题讨论】:

  • 哇,我在标签中拼错了名字。我不建议使用模型驱动。
  • @AleksandrM:我有一个问题,我们不能使用模型驱动吗?
  • @abhijeet:可以并不意味着我们应该。
  • @AleksandrM 我明白了 :) 但如果 bean 类有很多属性,那不是更好的方法吗?
  • @abhijeet:这有什么改变?

标签: msr msr msr alexander java jsp struts2


【解决方案1】:
public class contTest extends ActionSupport{
  public Employee  emp1;
  public String execute(){
    System.out.println("firstName-->>>"+emp1.getFirstName());
    System.out.println("lastName-->>>"+emp1.getLastName());
    return SUCCESS;
  }

  //Create Setter and Getter of emp1 object
}

在 AddEmp.jsp 中

<th>First Name :</th>
<td><input type = "text" id ="firstName" name = "emp1.firstName" value= ""></td>

<th>Last Name :</th>
<td><input type = "text" id ="lastName" name = "emp1.lastName" value= ""></td>

【讨论】:

    【解决方案2】:

    你必须为你的动作类实现模型 ModelDriven 接口。它有方法 getModel() 方法来返回 Employee 的对象。参考这个链接:http://struts.apache.org/release/2.3.x/docs/model-driven.html

    使用ModelDriven方法时需要初始化Model对象((Employee)),框架会自动将表单数据传入Employee类

    【讨论】:

    • 不。不是模型驱动的界面。
    • 模型驱动的接口不是为了同样的目的吗?链接:struts.apache.org/release/2.3.x/docs/model-driven.html
    • 除非你知道自己在做什么,否则不要乱用模型驱动。
    • 他的要求是模型驱动接口做什么。
    • 当您不想要冗余时使用模态驱动接口,因为在您的操作类中您必须创建 getter 和 setter,因为将值从表单设置为属性的参数拦截器会在其中查找 getter 和 setter value stack ,因此通过模态拦截器(意味着通过实现模型驱动接口并使用 getmodel() )您将 bean 类的对象直接推送到 value stack 中,因此您不需要在您的操作类中创建那些 getter 和 setter。就是这样:)
    猜你喜欢
    • 2016-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多