Struts2中action接受参数的方法,属性:DomainModel、ModelDriver和ActionSupport

来源:互联网 发布:叶部长 工资 知乎 编辑:程序博客网 时间:2024/05/17 23:11
Struts2中Action接收参数的方法主要有以下三种:
1.使用Action的属性接收参数:
    a.定义:在Action类中定义属性,创建get和set方法;
    b.接收:通过属性接收参数,如:uame;
    c.发送:使用属性名传递参数,如:user!add?uname=newname;
2.使用DomainModel接收参数:
    a.定义:定义Model类,在Action中定义Model类的对象(不需要new),创建该对象的get和set方法;
    b.接收:通过对象的属性接收参数,如:user.getUname();
    c.发送:使用对象的属性传递参数,如:user!add?user.uname=MGC;
3.使用ModelDriven接收参数:
    a.定义:Action实现ModelDriven泛型接口,定义Model类的对象(必须new),通过getModel方法返回该对象;
    b.接收:通过对象的属性接收参数,如:user.getUname();

    c.发送:直接使用属性名传递参数,如:user!add?uname=MGC


实体类:

import java.io.Serializable;public class UserInfo implements Serializable {private static final long serialVersionUID = 1L;private String name;private int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}


Struts2 Action获取表单传值

1.通过属性驱动式

JSP:

<%@ 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>Insert title here</title></head><body>    <center>        <form action="actionSupport" method="post">        姓名:<input type="text" name="info.name"><br>        年龄:<input type="text" name="info.age"><br>        <input type="submit" value="确定">        </form>    </center></body></html>

Action:直接通过set(),get()获取
package action;import com.opensymphony.xwork2.ActionSupport;import entity.UserInfo;public class UserAction2 extends ActionSupport {private UserInfo info;public UserInfo getInfo() {return info;}public void setInfo(UserInfo info) {this.info = info;}private static final long serialVersionUID = 1L;/** * @method:ActionSupport * @return * @throws Exception */public String actionSupport(){System.out.println("姓名:"+info.getName()+"--"+"年龄:"+info.getAge());return "success";}}

2.模型驱动方式,必须要实现ModelDriven<T>接口。如果要传入多个model这种方式不方便


JSP:

<%@ 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>Insert title here</title></head><body><center><form action="modelDriven" method="post">姓名:<input type="text" name="name"><br>年龄:<input type="text" name="age"><br><input type="submit" value="确定"></form></center></body></html>

Action:必须实现getModel() 方法

package action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import entity.UserInfo;public class UserAction extends ActionSupport implements ModelDriven<UserInfo> {private static final long serialVersionUID = 1L;private UserInfo info;/** * @method:ModelDriven * @return * @throws Exception */public String modelDriven() throws Exception {System.out.println("姓名:"+info.getName()+"--"+"年龄:"+info.getAge());return "success";}@Overridepublic UserInfo getModel() {if (info==null){info = new UserInfo();}return info;}} 
3.第三种方式可以完全不实现ModelDriven<T>,也可使用多个model对象的属性。

JSP:

    <form action="sys/login.action" method="post">       <input type="text" name="user.username">       <input type="text" name="teacher.level">       <input type="submit" value="submit">      </form>  

Action: 必须提供set方法


    public class sysAction extends ActionSupport{       private User user;       private Teacher teacher;             public String login() throws Exception {        System.out.println(user.getUsername());        System.out.println(teacher.getLevel());        return SUCCESS;       }             public void setUser(User user) {        this.user = user;       }       public void setTeacher(Teacher teacher) {        this.teacher = teacher;       }      }



阅读全文
0 0
原创粉丝点击