Struts:crud
1.导jar包
2.实体类(Student,Clazz)
1.Student:
public class Student {
private Integer sid;
private String sname;
private String spin;
private String sex;
private String mark;
private Integer cid;
private String cname;
public Integer getSid() {
return sid;
}
public void setSid(Integer sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
this.spin = PinYinUtil.toPinyin(this.sname);
}
public String getSpin() {
return spin;
}
public void setSpin(String spin) {
this.spin = spin;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public Student(Integer sid, String sname, String spin, String sex, String mark, Integer cid) {
super();
this.sid = sid;
this.sname = sname;
this.spin = spin;
this.sex = sex;
this.mark = mark;
this.cid = cid;
}
public Student() {
super();
}
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", spin=" + spin + ", sex=" + sex + ", mark=" + mark
+ ", cid=" + cid + ", clazzName=" + cname + "]";
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public Student(Integer sid, String sname, String spin, String sex, String mark, Integer cid, String cname) {
super();
this.sid = sid;
this.sname = sname;
this.spin = spin;
this.sex = sex;
this.mark = mark;
this.cid = cid;
this.cname = cname;
}
}
2.clazz:
public class Clazz {
private Integer cid;
private String cname;
private String cteacher;
public Integer getCid() {
return cid;
}
public void setCid(Integer cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getCteacher() {
return cteacher;
}
public void setCteacher(String cteacher) {
this.cteacher = cteacher;
}
public Clazz(Integer cid, String cname, String cteacher) {
super();
this.cid = cid;
this.cname = cname;
this.cteacher = cteacher;
}
public Clazz() {
super();
}
public String toString() {
return "Clazz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + "]";
}
}
3.dao方法:(clazzDao,studentDao)
1.ClazzDao:
public class ClazzDAO extends EntityBaseDao<Clazz> {
public ClazzDAO() {
super();
}
public List<Clazz> list() throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select * from t_struts_class where 1=1";
return super.executeQuery(sql, new PageBean(), Clazz.class);
}
public static void main(String[] args) {
try {
System.out.println(new ClazzDAO().list());
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.studentDao:
public class StudentDAO extends EntityBaseDao<Student> {
/**
* 增加
* @param student
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws SQLException
*/
public void add(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
String sql = "insert into t_struts_student(sid,sname,spin,sex,mark,cid) values(?,?,?,?,?,?)";
super.executeUpdate(sql, new String[] {"sid","sname","spin","sex","mark","cid"}, student);
}
/**
* 修改
* @param student
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws SQLException
*/
public void edit(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
String sql = "update t_struts_student set sname=?,spin=?,sex=?,mark=?,cid=? where sid=?";
super.executeUpdate(sql, new String[] {"sname","spin","sex","mark","cid","sid"}, student);
}
/**
* 删除
* @param student
* @throws InstantiationException
* @throws IllegalAccessException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws SQLException
*/
public void del(Student student) throws InstantiationException, IllegalAccessException, NoSuchFieldException, SecurityException, SQLException {
String sql = "delete from t_struts_student where sid=?";
super.executeUpdate(sql, new String[] {"sid"}, student);
}
/**
* 单个查
* @param student
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public Student load(Student student) throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select s.*, c.cname from t_struts_student s inner join t_struts_class c on c.cid = s.cid where s.sid="+student.getSid();
return super.executeQuery(sql, new PageBean(), Student.class).get(0);
}
/**
* 查所有
* @param student
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Student> list(Student student, PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException {
String sql = "select s.*, c.cname from t_struts_student s inner join t_struts_class c on c.cid = s.cid where true ";
if (StringUtils.isNotBlank(student.getSname())) {
sql += " and (s.sname like '%" + student.getSname().trim()
+ "%' or s.spin like '%" + student.getSname().trim() + "%')";
}
if (null != student.getCid() && !new Integer(-1).equals(student.getCid())) {
sql += " and s.cid = " + student.getCid();
}
return super.executeQuery(sql, pageBean, Student.class);
}
}
3.新建action(BaseAction)
1.每一个开发的子控制器要用的属性都定义在通用的action中,方便调用,减少代码量。
public class BaseAction implements ServletRequestAware, ServletResponseAware{
/**
* 为了传值使用
*/
protected HttpServletResponse response;
protected HttpServletRequest request;
protected HttpSession session;
protected ServletContext application;
/**
* 为了配置跳转页面所用
*/
protected final static String SUCCESS = "success";
protected final static String FAIL = "fail";
protected final static String LIST = "list";
protected final static String ADD = "add";
protected final static String EDIT = "edit";
protected final static String DETAIL = "detail";
/**
* 具体传值字段 后端向jsp页面传值所用字段
*/
protected Object result;
protected Object msg;
protected int code;
public Object getResult() {
return result;
}
public Object getMsg() {
return msg;
}
public int getCode() {
return code;
}
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
this.session = arg0.getSession();
this.application = arg0.getServletContext();
}
}
4.action方法(clazzAction,studentAction):
1.StudentAction:
public class StudentAction extends BaseAction implements ModelDriven<Student>{
private StudentDAO studentDao=new StudentDAO();
private Student student=new Student();
PageBean pageBean=new PageBean();
/**
* 查所有
* @return
*/
public String list() {
pageBean.setRequest(request);
try {
this.result=studentDao.list(student, pageBean);
request.setAttribute("pageBean", pageBean);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return LIST;
}
/**
* 增加
* @return
*/
public String add() {
try {
studentDao.add(student);
this.msg="增加成功";
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
/**
* 跳到增加页面
* @return
*/
public String toAdd() {
return ADD;
}
/**
* 修改
* @return
*/
public String edit() {
try {
studentDao.edit(student);
this.msg="修改成功";
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
/**
* 调单个查方法(数据回显)
* @return
*/
public String toEdit() {
try {
this.result=studentDao.load(student);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EDIT;
}
/**
* 删除
* @return
*/
public String delete() {
try {
studentDao.del(student);
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
public Student getModel() {
// TODO Auto-generated method stub
return student;
}
}
2.ClazzAction:
public class ClazzAction extends BaseAction{
public String execute() {
try {
this.result=new ClazzDAO().list();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
5.jsp页面(listStudentaddStudent,editStudent,head)
页面样式问题 theme
单改一个页面:
写属性theme:
<s:form namespace="/sy" action="studentAction_add" theme="simple"></s:form>
改所有页面:
全局属性: struts_base.xml
<constant name="struts.ui.theme" value="simple"/>
1.listStudent:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/zking" prefix="z" %>
<!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">
<%@ include file="/jsp/common/head.jsp" %>
<title>Insert title here</title>
</head>
<body>
<s:form namespace="/sy" action="studentAction_list">
<s:textfield label="姓名" name="sname"></s:textfield>
<s:select list="#clzlist.result" name="cid" label="班级" listKey="cid" listValue="cname" headerKey="" headerValue="===请选择==="></s:select>
<s:submit value="查询"></s:submit>
</s:form>
<s:url namespace="/sy" action="studentAction_toAdd" var="toAddURL"></s:url>
<s:a href="%{#toAddURL}">新增</s:a>
<table border="1" width="100%">
<tr>
<td>序号</td>
<td>学号</td>
<td>姓名</td>
<td>拼音</td>
<td>性别</td>
<td>标记</td>
<td>班级</td>
<td>操作</td>
</tr>
<s:iterator var="s" value="result">
<tr>
<td>序号</td>
<td><s:property value="#s.sid"/></td>
<td><s:property value="#s.sname"/></td>
<td><s:property value="#s.spin"/></td>
<td><s:property value="#s.sex"/></td>
<td><s:property value="#s.mark"/></td>
<td><s:property value="#s.cname"/></td>
<td>
<s:url namespace="/sy" action="studentAction_toEdit" var="toEditURL">
<s:param name="sid" value="#s.sid"></s:param>
</s:url>
<s:a href="%{#toEditURL}">修改</s:a>
<s:url namespace="/sy" action="studentAction_delete" var="toDeleteURL">
<s:param name="sid" value="#s.sid"></s:param>
</s:url>
<s:a href="%{#toDeleteURL}">删除</s:a>
</td>
</tr>
</s:iterator>
</table>
</body>
</html>
2.addStudent:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/zking" prefix="z" %>
<!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">
<%@ include file="/jsp/common/head.jsp" %>
<title>Insert title here</title>
</head>
<body>
<h1>add</h1>
<s:action name="clzAction" namespace="/sy" var="clzlist"></s:action>
<s:form namespace="/sy" action="studentAction_add">
<%-- <s:hidden name="sid" value="5"/> --%>
<s:textfield label="学号" name="sid" />
<s:textfield label="姓名" name="sname" />
<s:radio label="性别" name="sex" list="{'男','女'}"/>
<s:select label="班级" name="cid" headerKey="" headerValue="===请选择===" list="#clzlist.result" listKey="cid" listValue="cname" cssStyle="width:160px;" />
<s:textarea label="备注" name="mark"></s:textarea>
<s:submit value="确定"/>
</s:form>
</body>
</html>
3.editStudent:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/zking" prefix="z" %>
<!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">
<%@ include file="/jsp/common/head.jsp" %>
</head>
<body>
<h1>update</h1>
<!--初始化数据-->
<s:action name="clzAction" namespace="/sy" var="clzlist"></s:action>
<s:push value="result"> <!--把result压到栈顶,方便取值-->
<s:form namespace="/sy" action="studentAction_edit">
<%-- <s:hidden name="sid" value="5"/> --%>
<s:textfield label="学号" name="sid" />
<s:textfield label="姓名" name="sname" />
<s:radio label="性别" name="sex" list="{'男','女'}"/>
<s:select label="班级" name="cid" headerKey="" headerValue="===请选择===" list="#clzlist.result" listKey="cid" listValue="cname" cssStyle="width:160px;" />
<s:textarea label="备注" name="mark"></s:textarea>
<s:submit value="确定"/>
</s:form>
</s:push>
</body>
</html>
4.head:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html >
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib uri="/zking" prefix="z" %>
6.xml配置
1.struts_sy.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<action name="studentAction_*" class="com.zking.four.web.StudentAction" method="{1}"> <!--动态调用-->
<result name="list">/jsp/listStudent.jsp</result>
<result name="add">/jsp/addStudent.jsp</result>
<result name="edit">/jsp/editStudent.jsp</result>
<result name="success" type="redirect">/sy/studentAction_list.action</result>
</action>
</package>斜体样式
</struts>
2.web.xml:
不直接跳页面,跳子控制器,因为路径问题和。action配置*
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts1</display-name>
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
</web-app>