向数据库插入数据
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; } private int id; private String name; private String password; private String sex; private String address; private String hobby; private int age; private int page=1; private String add1; private String add3; private Sheng Hebei; private Sheng Sd; private Student student=new Student(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getPage() { return page; } public void setPage(int page) { this.page = page; } public String getAdd1() { return add1; } public void setAdd1(String add1) { this.add1 = add1; } public String getAdd3() { return add3; } public void setAdd3(String add3) { this.add3 = add3; } public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } public void setSd(Sheng sd) { Sd = sd; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getHobby() { return hobby; } public void setHobby(String hobby) { this.hobby = hobby; } public Sheng getHebei() { Hebei=new Sheng(); Hebei.setName("河北省"); String c1="沧州"; String c2="保定"; String c3="廊坊"; String c4="衡水"; List<String> citys=new ArrayList<String>(); citys.add(c1); citys.add(c2); citys.add(c3); citys.add(c4); Hebei.setCitys(citys); return Hebei; } public void setHebei(Sheng hebei) { Hebei = hebei; } public Sheng getSd() { Sheng sd=new Sheng(); sd.setName("山东省"); String c1="烟台"; String c2="青岛"; String c3="蓬莱"; String c4="威海"; List<String> citys=new ArrayList<String>(); citys.add(c1); citys.add(c2); citys.add(c3); citys.add(c4); sd.setCitys(citys); Sd=sd; return Sd; } public void studentAddr() { HttpServletResponse response=ServletActionContext.getResponse(); response.setCharacterEncoding("utf-8"); if("河北省".equals(add1)) { this.getHebei(); List<String> c1=Hebei.getCitys(); String str=JSONArray.fromObject(c1).toString(); try { response.getWriter().print(str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else{ getSd(); List<String> city=Sd.getCitys(); String str=JSONArray.fromObject(city).toString(); try { response.getWriter().print(str); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public String saveStudent(){ student.setId(id); student.setAge(age); student.setName(name); student.setHobby(hobby); student.setPassword(password); student.setSex(sex); student.setAddress(address); dao.saveStudent(student); return "saveStudent"; } }
StudentAction.java
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="s" uri="/struts-tags"%> <!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>学生添加</title> <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-2.0.3.min.js"></script> <script type="text/javascript" src="${pageContext.request.contextPath}/js/datepicker/WdatePicker.js"></script> <script type="text/javascript"> $(function(){ var sheng=$("#addr1 option:selected").html(); $.ajax({ type:"post", url:"${pageContext.request.contextPath}/student/student_studentAddr", data:"add1="+sheng, dataType:"json", success:function(city){ for(var i=0;i<city.length;i++){ $("#addr2").append('<option class="r">'+city[i]+'</option>'); } var sh=$("#addr1 option:selected").html(); var shi=$("#addr2 option:selected").html(); var htmlstr=sh+''+shi; alert(htmlstr); $("#addr3").val(htmlstr); } }); $("#addr1").change(function(){ var sheng=$("#addr1 option:selected").html(); alert(sheng); $("option").remove(".r"); /* 移除标签文件 */ $("#addr3").val(''); $.ajax({ type:"post", url:"${pageContext.request.contextPath}/student/student_studentAddr", data:"add1="+sheng, dataType:"json", success:function(city){ for(var i=0;i<city.length;i++){ $("#addr2").append('<option class="r">'+city[i]+'</option>'); } var sh=$("#addr1 option:selected").html(); var shi=$("#addr2 option:selected").html(); var htmlstr=sh+''+shi; $("#addr3").val(htmlstr); } }); }); $("#addr2").change(function(){ $("#addr3").val(''); var sh=$("#addr1 option:selected").html(); var si=$("#addr2 option:selected").html(); var htmlstr=sh+''+si; $("#addr3").val(htmlstr); }); }) </script> </head> <body> <h1>学生添加</h1> <s:form action="student/student_saveStudent" method="post" theme="simple"><br> 姓名:<s:textfield name="name" label="姓名" required="true"></s:textfield><br> 密码:<s:password label="密码" name="password" required="true"></s:password><br> 年龄:<s:textfield name="age" label="年龄" required="true"></s:textfield><br> 性别:<s:radio label="性别" name="sex" list="#{'1':'男','2':'女'}"></s:radio><br> 出生年月:<s:textfield name="dte" label="出生年月" onfocus="WdatePicker({dateFmt : 'yyyy-MM-dd HH:mm:ss'})" class="Wdate"></s:textfield><br> 爱好:<s:checkboxlist label="爱好" name="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="address" id="addr3"></s:textfield> <s:submit value="提交"></s:submit> </s:form> </body> </html>
在java action文件中声明对应的属性 只要属性名与前台的属性名称对应 就可以得到前台的值
再声明一个student对象 然后一个一个将获取到的值set进去