向数据库插入数据第二种方法
<html> <body> <h1>学生添加</h1> <s:form action="student/student_saveStudent" method="post" theme="simple"><br> 姓名:<s:textfield name="student.name" label="姓名" required="true"></s:textfield><br> 密码:<s:password label="密码" name="student.password" required="true"></s:password><br> 年龄:<s:textfield name="student.age" label="年龄" required="true"></s:textfield><br> 性别:<s:radio label="性别" name="student.sex" list="#{‘1‘:‘男‘,‘2‘:‘女‘}"></s:radio><br> 出生年月:<s:textfield name="student.dte" label="出生年月" onfocus="WdatePicker({dateFmt : ‘yyyy-MM-dd HH:mm:ss‘})" class="Wdate"></s:textfield><br> 爱好:<s:checkboxlist label="爱好" name="student.hobby" list="#{‘1‘:‘basketball‘,‘2‘:‘football‘,‘3‘:‘music‘,‘4‘:‘art‘}"></s:checkboxlist><br> 省份:<s:select id="addr1" label="省份 " name="province" list="{‘河北省‘,‘山东省‘}"></s:select> 城市:<select id="addr2" name="city"></select><br> 地址:<s:textfield label="location" name="student.address" id="addr3"></s:textfield> <s:submit value="提交"></s:submit> </s:form> </body> </html>
将前台的name都设为student.**
package com.action; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import org.apache.struts2.ServletActionContext; import com.dao.StudentDao; import com.dao.imp.StudentDaoImp; import com.entity.Sheng; import com.entity.Student; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class StudentAction extends ActionSupport{ private StudentDao dao=new StudentDaoImp(); public StudentDao getDao() { return dao; } public void setDao(StudentDao dao) { this.dao = dao; } //new一个dao private Student student; //声明一个student对象 public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public String saveStudent(){ dao.saveStudent(student); return "saveStudent"; } }
在后台声明一个student对象 加上get set方法 就会自动得到前台对象的值
这时候再调用dao.saveStudent(student);将其保存到数据库中即可
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。