1.学习路线
今天咱们来学struts2参数获得方式,let`go!
图1.学习路线
图2.类和配置文件
form1.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>
<form action="${pageContext.request.contextPath}/Demo8Action">
用户名:<input type="text" name="name" /><br>
年龄:<input type="text" name="age" /><br>
生日:<input type="text" name="birthday" /><br>
<input type="submit" value="提交" />
</form>
</body>
</html>
2.属性驱动获得参数
新建一个Demo8Action类,继承ActionSupport,再创建一个execute()方法,return SUCCESS。
package cn.aisino.c_param;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
//struts2如何获得参数
//每次请求Action时都会创建新的Action实例对象
public class Demo8Action extends ActionSupport {
//调用空参构造方法
public Demo8Action(){
super();
System.out.println("Demo8Action被创建了!");
}
//准备与参数键名称相同的属性
private String name;
//自动类型转换,只能转换8大基本数据类型以及包装类型
private Integer age;
//支持特定类型字符串转换成Date,例如yyyy-MM-dd
private Date birthday;
public String execute() throws Exception {
System.out.println("name:" + name + ",age:"+age+",生日:"+birthday);
return SUCCESS;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
生成getters()和setters()方法,
在包下的配置文件struts.xml中配置Action,
在主配置文件struts.xml中配置,
启动服务器,在地址栏中访问Demo8Action:
提交,控制台可查看数据成功提交: