package stuinfo.action;
import java.util.List;
import javax.swing.JOptionPane;
import com.opensymphony.xwork2.ActionSupport;
import stuinfo.model.*;
import stuinfo.dao.StudentDao;
import stuinfo.model.Student;
/**
* 前端传给 struts.xml,然后通过struts.xml找到对应name中的类的getter and setter方法
* 通过getter and setter获取到了前端的参数,然后调用方法将数据传给dao进行保存到数据库当中(形参穿到实参)
* @author Administrator
*
*/
public class AddAction extends ActionSupport{
private static final long serialVersionUID=1;
private String id;
private String stuname;
private String sex;
private float Weight;
private String studynum;
private int age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public float getWeight() {
return Weight;
}
public void setWeight(float weight) {
Weight = weight;
}
public String getStudynum() {
return studynum;
}
public void setStudynum(String studynum) {
this.studynum = studynum;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
/**
* 判断用户输入信息正确性
* @throws Exception
*/
public void validate(Student stu) throws Exception{
if(stu.getStuname()==""||stu.getStuname()==null){
System.out.println(stu.getStuname()+"学生姓名不能为空");
addFieldError("name","姓名不允许为空!");
}
if(stu.getWeight()<=0||stu.getWeight()>=500){
System.out.println("该名同学是外星人,请核对名单重新输入");
addFieldError("weight","该名同学是外星人,请核对名单重新输入!");
}
if(stu.getAge()<=0||stu.getAge()>=200){
addFieldError("age","您现在有危险!地球生物不存在这种年龄,速跑");
}
}
/**
* 将学生信息设置为用户输入的信息 ,下面的这个写法是错误的,不能将getter and setter方法放到struts.xml中name的
* 另外的类当中,然后封装该方法到struts的name=""的类中这样是错误的不会调用到getter and setter,因为前段跟后台的交互
* 就只做到了struts.xml跟sturtus.xml 中name=""的类,其他的跟前段无任何数据的交流 。只能放在name=""后面的类中,
* 然后在该类中添加method方法
* @return
*/
/*public Student info(){
Student info = new Student();
info.setId(stu.getId());
info.setStuname(stu.getStuname());
info.setAge(stu.getAge());
info.setWeight(stu.getWeight());
info.setStudynum(stu.getStudynum());
info.setSex(stu.getSex());
stuDao.Student
return info;
}*/
/**info方法是往数据库里面添加数据
* 通过getter and setter获取前端的参数
* 定义一个空的student对象info,将对info进行设置,最后将info传回给到addstu(Student stu) 进行判断
* @return
*/
public Student info(){
Student info = new Student();
info.setStuname(this.getStuname());
info.setSex(this.getSex());
info.setAge(this.getAge());
info.setWeight(this.getWeight());
info.setStudynum(this.getStudynum());
try {
this.validate(info);
} catch (Exception e) {
e.printStackTrace();
}
return info;
}
/**
*
*/
/*public Student info(){
Student stu = new Student();
stu.setStuname(this.getStuname());
stu.setSex(this.getSex());
stu.setAge(this.getAge());
stu.setWeight(this.getWeight());
stu.setStudynum(this.getStudynum());
if(stu.getStuname()==""||stu.getStuname()==null){
System.out.println(stu.getStuname()+"学生姓名不能为空");
addFieldError("name","姓名不允许为空!");
alert("name","姓名不允许为空!");
return false;
}
else if(stu.getWeight()<=0||stu.getWeight()>=500){
System.out.println("该名同学是外星人,请核对名单重新输入");
addFieldError("weight","该名同学是外星人,请核对名单重新输入!");
alert("weight","该名同学是外星人,请核对名单重新输入!");
return false;
}
else if(stu.getAge()<=0||stu.getAge()>=200){
addFieldError("age","您现在有危险!地球生物不存在这种年龄,速跑");.
alert("age","您现在有危险!地球生物不存在这种年龄,速跑");
return false
} else{
return true;
}
return stu;
}另一种验证info学生对象方法*/
/**
* 保存数据,判断是否已经将数据保存
* @return
*/
public String add(){
StudentDao dao = new StudentDao();
boolean save = dao.student(info());//由于数据保存Dao中使用的是boolean型,所以这里也使用boolean型
//这里的这个info()是将上面的student封装的student对象传递到dao中,进行保存,跟dao中定义的info1无关
if(save){//save是boolean型,默认是true,即等价于if(save=true)
return SUCCESS;
}else{
return ERROR;
}
}
/**
* 打印输出信息
* @param mess
*/
public void message(String mess){
int type=JOptionPane.YES_NO_OPTION;
String title = "提示信息";
JOptionPane.showMessageDialog(null, mess, title, type);
}
}