本文介绍了当我在其他 2 个 POJO 类中声明字段变量时,我想在 struts2 Action 类中自动设置 jsp 文本字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想再在 Action 类中声明那些变量

I don't want to declare those variables in Action class again

员工 POJO:

package com.pojo;

import java.io.Serializable;

public class Employee{

    String name;
    Address address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

}

地址POJO:

package com.pojo;

import java.io.Serializable;

public class Address{


    String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }               
}

JSP:

<body>
<s:form action="beanEx">

<s:textfield label="name" name="name"/>
<s:textfield label="email" name="email"/>

<s:submit value="Submit"/>

</s:form>

</body>

我想自动填充值在Action类中设置值

I want to populate the values automatically set the values in Action class

谁能帮帮我........

Can anybody help me out........

推荐答案

使用 getter/setter 在你的 action 类中有两个对象

Have two objects in your action class with getter/setter

private Address address = new Address();
private Employee employee = new Employee();;

然后在你的jsp中这样做:

then in your jsp do like this:

<body>
    <s:form action="beanEx">    
        <s:textfield name="employee.name" label="name"/>
        <s:textfield name="address.email" label="email"/>    
        <s:submit value="Submit"/>    
    </s:form>    
</body>

基本上之前您指向的是字段,现在您指向的是对象内部的字段.

Basically earlier you were pointing to the field now you are pointing to the field inside an object.

这篇关于当我在其他 2 个 POJO 类中声明字段变量时,我想在 struts2 Action 类中自动设置 jsp 文本字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 08:49