Stryts–crud值(增删改查)
思路:
1、导入相关的pom依赖(struts、自定义标签库的依赖)
2、分页的tag类导入、z.tld、完成web.xml的配置
3、dao层去访问数据
4、web层去调用dao层给前台返回数据
5、在struts_sy.xml进行配置
6、写jsp
1、定义baseAction,存放结果码常量,请求、响应、上下文、公用的传值
commons-fileupload commons-fileupload 1.3.1 <dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- 5.4、tomcat-jsp-api -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>8.0.47</version>
</dependency>
chain:Action链式处理的结果类型,也就是将结果转发到这个action中。
redirect:实际上dispatcher和redirect的区别就是在于转发和重定向的区别。
redirectAction:用于直接redirectaction。
2、Struts标签的使用
s:iterator
S:action
S:url
S:form
s:textfield
S:select
S:radio
S:param
s:textarea
1、不直接跳页面,跳子控制器,因为路径问题和*。action配置
2、修改页面弹栈的问题,load出的结果作为跟,属性可以直接取值
3、页面样式问题 theme
<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>
</td>
</tr>
</s:iterator>
</table>
配置web.xml
<filter>
<filter-name>encodingFiter</filter-name>
<filter-class>com.lrc.c.util.EncodingFiter</filter-class>
</filter>
<filter-mapping>
<filter-name>encodingFiter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
l.tld
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>lrc 1.1 core library</description>
<display-name>lrc</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/lrc</uri>
<tag>
<!-- 标签库中的标签名 -->
<name>page</name>
<!-- 标签对应的后台助手类 -->
<tag-class>com.page.lrc.tag.PageTag</tag-class>
<!-- 标签类别 -->
<body-content>JSP</body-content>
<attribute>
<!-- 自定义标签携带的属性 -->
<name>pageBean</name>
<!-- 属性值是否必填 -->
<required>true</required>
<!-- 是否支持表达式(EL/ognl) -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
Clazz
package com.lrc.c.entity;
import java.lang.reflect.Field;
public class Clazz {
private int cid;
private String cname;
private String cteacher;
private String pic="无";
public int getCid() {
return cid;
}
public void setCid(int 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 String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
@Override
public String toString() {
return "Clazz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + ", pic=" + pic + "]";
}
public Clazz(int cid, String cname, String cteacher, String pic) {
super();
this.cid = cid;
this.cname = cname;
this.cteacher = cteacher;
this.pic = pic;
}
public Clazz() {
super();
}
public <T> T newInstance() {
// TODO Auto-generated method stub
return null;
}
public Field[] getDeclaredFields() {
// TODO Auto-generated method stub
return null;
}
}
BaseAction
package com.lrc.c.util;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
/**
* 每一个开发的子控制器要用的属性都定义在通用的action中。
* @author Administrator
*
*/
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;
}
@Override
public void setServletResponse(HttpServletResponse arg0) {
this.response = arg0;
}
@Override
public void setServletRequest(HttpServletRequest arg0) {
this.request = arg0;
this.session = arg0.getSession();
this.application = arg0.getServletContext();
}
}
ClzDao
package com.lrc.c.dao;
import java.sql.SQLException;
import java.util.List;
import com.lrc.c.entity.Clazz;
import com.lrc.c.util.BaseDao;
import com.lrc.c.util.PageBean;
import com.lrc.c.util.StringUtils;
/**
* 操作数据库
* @author Administrator
*
*/
public class ClzDao extends BaseDao<Clazz> {
/**
* 班级列表查询(带分页)
* @param clz
* @param pageBean
* @return
* @throws InstantiationException
* @throws IllegalAccessException
* @throws SQLException
*/
public List<Clazz> list(Clazz clz, PageBean pageBean) throws Exception{
String sql = "select * from t_struts_class where true ";
String cname = clz.getCname();
int cid = clz.getCid();
if(StringUtils.isNotBlank(cname)) {
sql += " and cname like '%"+cname+"%'";
}
if(cid != 0) {
sql += " and cid = "+cid;
}
return super.execute(sql, Clazz.class, pageBean);
}
/**
* 班级信息的增加
* @param clz
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int add(Clazz clz) throws Exception {
String sql = "insert into t_struts_class values(?,?,?,?)";
return super.executeUpdate(sql, new String[] {"cid","cname","cteacher","pic"}, clz);
}
/**
* 班级信息的删除
* @param clz
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int del(Clazz clz) throws Exception {
String sql = "delete from t_struts_class where cid = ?";
return super.executeUpdate(sql, new String[] {"cid"}, clz);
}
/**
* 班级信息的修改
* @param clz
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
*/
public int edit(Clazz clz) throws Exception {
String sql = "update t_struts_class set cname=?,cteacher=?,pic=? where cid = ?";
return super.executeUpdate(sql, new String[] {"cname","cteacher","pic","cid"}, clz);
}
}
ClazzAction
package com.lrc.c.web;
import java.sql.SQLException;
import com.opensymphony.xwork2.ModelDriven;
import com.lrc.c.dao.ClzDao;
import com.lrc.c.entity.Clazz;
import com.lrc.c.util.BaseAction;
import com.lrc.c.util.PageBean;
public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
private ClzDao clzDao = new ClzDao();
private Clazz clz = new Clazz();
/**
* 查询班级列表的方法
* @return
*/
public String list() {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
try {
this.result = clzDao.list(clz, pageBean);
request.setAttribute("pageBean", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
/**
* 跳转编辑页面(新增编辑页面是同一张)
* @return
*/
public String preSave() {
// 什么时候跳转新增页面?什么时候跳转编辑页面?
// 区别在于跳编辑页面的时候需要传递cid到后台进行查询,并且将查询到的数据进行回显?
int cid = clz.getCid();
if(cid != 0) {
try {
this.result = clzDao.list(clz, null).get(0);
} catch (Exception e) {
e.printStackTrace();
}
}
return "preSave";
}
/**
* 增加班级信息
* @return
*/
public String add() {
try {
this.code = this.clzDao.add(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* 修改班级信息
* @return
*/
public String edit() {
try {
this.code = this.clzDao.edit(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* 删除班级信息
* @return
*/
public String del() {
try {
this.code = this.clzDao.del(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
@Override
public Clazz getModel() {
return clz;
}
}
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="/user_*" class="com.lrc.a.UserAction" method="{1}">
<result name="rs">/text.jsp</result>
</action>
<action name="demo_*" class="com.lrc.a.DemoAction" method="{1}">
<result name="rs">/text2.jsp</result>
</action>
<action name="tomcat_*" class="com.lrc.a.TomcatAction" method="{1}">
<result name="rs">/text2.jsp</result>
</action>
<action name="ognl_*" class="com.lrc.b.Demo7" method="{1}">
<result name="rs"></result>
</action>
<action name="lrc_*" class="com.lrc.c.web.ClazzAction" method="{1}">
<result name="list">/claList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="redirectAction">/clz_list</result>
</action>
</package>
</struts>
ClzEdit.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>班级的编辑页面</title>
</head>
<body>
<form action="${pageContext.request.contextPath}${result.cname == null ? '/sy/clz_add.action' : '/sy/clz_edit.action'}" method="post">
cid:<input type="text" value="${result.cid }" name="cid"><br>
cname:<input type="text" value="${result.cname }" name="cname"><br>
cteacher:<input type="text" value="${result.cteacher }" name="cteacher"><br>
<input type="hidden" name="pic" value="${result.pic}">
<input type="submit" value="ok">
</form>
</body>
</html>
claList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/lrc" 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">
<title>书籍主页</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/sy/clz_list.action"
method="post">
<!-- <input type='hidden' name='rows' value="20"> -->
<!-- <input type='hidden' name='pagination' value="false"> -->
书名:<input type="text" name="bname">
<input type="submit"
value="确定">
</form>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action">新增</a>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>班级名称</td>
<td>教员</td>
<td>图片</td>
<td>操作</td>
</tr>
<c:forEach items="${result }" var="c">
<tr>
<td>${c.cid }</td>
<td>${c.cname }</td>
<td>${c.cteacher }</td>
<td>${c.pic }</td>
<td>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${c.cid }">修改</a>
<a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${c.cid }">删除</a>
</td>
</tr>
</c:forEach>
</table>
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>
特别注意
chain:Action链式处理的结果类型,也就是将结果转发到这个action中。
redirect:实际上dispatcher和redirect的区别就是在于转发和重定向的区别。
redirectAction:是用于直接redirectaction。
不能直接进入jsp界面,需要进入中央控制器