Action封装:属性驱动

丶灬走出姿态 提交于 2020-08-10 23:35:28

User:

package com.example.struts.domain;

import java.util.Date;

public class User {
    private String username;
    private String password;
    private Date date;
    private Integer age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", date=" + date +
                ", age=" + age +
                '}';
    }
}

Action:

package com.example.struts;

import com.example.struts.domain.User;
import com.opensymphony.xwork2.Action;

public class UserAction implements Action {

    private User user;

    private String code;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(this.user);
        System.out.println(this.code);
        return NONE;
    }
}

jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户信息</title>
</head>
<body>
    <form method="post" action="${ pageContext.request.contextPath }/user/add1">
        <input type="text" name="user.username" />
        <input type="password" name="user.password" />
        <input type="number" name="user.age" />
        <input type="date" name="user.date" />
        <input type="text" name="code" />
        <button type="submit">提交</button>
    </form>
</body>
</html>

struts.xml:

<constant name="struts.enable.SlashesInActionNames" value="true"/>
<action name="user/add1" class="com.example.struts.UserAction">
</action>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!