Struts2获取表单数据

1:属性驱动

前端页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
    <form action="login.action" method="post">
        UserName:<input type="text" name="username"/>
        <br/>
        Password:<input type="password" name="password"/>
        <br/>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>
View Code

Action:

package com.zhao.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @ParentPackage:用来指定struts.xml中的package
 * @Action注解将一个普通Java类标识为可以处理用户请求的Action类
 * @Namespace是命名空间
 * @author zhao
 *
 */
@ParentPackage("default")
@Action
@Namespace("/")
public class LoginAction extends ActionSupport {
    /*
     * 属性驱动
     */
    private String username;
    private String password;

    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;
    }

    @Override
    public String execute() throws Exception {
        System.out.println("UserName: "+this.username);
        System.out.println("Password: "+this.password);
        return SUCCESS; 
    }
}
View Code

2:模型驱动

前端页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
    <form action="login.action" method="post">
        UserName:<input type="text" name="username"/>
        <br/>
        Password:<input type="password" name="password"/>
        <br/>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>
View Code

User:

package com.zhao.entity;

import java.io.Serializable;

public class User implements Serializable {
    private String username;
    private String password;

    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;
    }

}
View Code

Action:

package com.zhao.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhao.entity.User;

/**
 * @ParentPackage:用来指定struts.xml中的package
 * @Action注解将一个普通Java类标识为可以处理用户请求的Action类
 * @Namespace是命名空间
 * @author zhao
 *
 */
@ParentPackage("default")
@Action
@Namespace("/")
public class LoginAction extends ActionSupport implements ModelDriven<User>{
    private User user=new User();

    @Override
    public String execute() throws Exception {
        System.out.println("UserName: "+user.getUsername());
        System.out.println("Password: "+user.getPassword());
        return SUCCESS; 
    }

    @Override
    public User getModel() {
        if (user==null) {
            user=new User();
        }
        return user;
    }
}
View Code

3:不完全模型驱动

前端页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Index Page</title>
</head>
<body>
    <form action="login.action" method="post">
        UserName:<input type="text" name="user.username"/>
        <br/>
        Password:<input type="password" name="user.password"/>
        <br/>
        <input type="submit" value="Submit"/>
    </form>
</body>
</html>
View Code

User:

package com.zhao.entity;

import java.io.Serializable;

public class User implements Serializable {
    private String username;
    private String password;

    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;
    }

}
View Code

 

Action:

package com.zhao.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zhao.entity.User;

/**
 * @ParentPackage:用来指定struts.xml中的package
 * @Action注解将一个普通Java类标识为可以处理用户请求的Action类
 * @Namespace是命名空间
 * @author zhao
 *
 */
@ParentPackage("default")
@Action
@Namespace("/")
public class LoginAction extends ActionSupport{
    private User user=new User();

    @Override
    public String execute() throws Exception {
        System.out.println("UserName: "+user.getUsername());
        System.out.println("Password: "+user.getPassword());
        return SUCCESS; 
    }

    public User getUser() {
        return user;
    }

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

}
View Code

 

posted @ 2016-04-14 16:29  假寐的我  阅读(248)  评论(0编辑  收藏  举报