思路:
1、导入相关的pom依赖(struts、自定义标签库的依赖)
2、分页的tag类导入、z.tld、完成web.xml的配置
3、dao层去访问数据
4、web层去调用dao层给前台返回数据
5、在struts_sy.xml进行配置
6、写jsp
1、导入相关的pom依赖(struts、自定义标签库的依赖):
<!-- 5.3、jstl、standard -->
<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>
- 不直接跳页面,跳子控制器,因为路径问题和*。action配置
- 修改页面弹栈的问题,load出的结果作为跟,属性可以直接取值
- 页面样式问题 theme
Struts标签的使用
s:iterator
S:action
S:url
S:form
s:textfield
S:select
S:radio
S:param
s:textarea
2、分页的tag类导入、z.tld、完成web.xml的配置:
导入之前mvc点这里里面的工具栏,自定义标签:(上图)
3、dao层去访问数据:
Clazz实体类:
package com.xhh.entity;
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();
}
}
ClzDao:
package com.xhh.dao;
import java.sql.SQLException;
import java.util.List;
import com.xhh.entity.Clazz;
import com.xhh.util.BaseDao;
import com.xhh.util.PageBean;
import com.xhh.util.StringUtils;
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 InstantiationException, IllegalAccessException, SQLException{
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.executeQuery(sql, Clazz.class, pageBean);
}
/**
* 班级信息增加
* @param clz
* @return
* @throws IllegalArgumentException
* @throws IllegalAccessException
* @throws SQLException
* @throws SecurityException
* @throws NoSuchFieldException
*/
public int add(Clazz clz) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException {
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
* @throws SecurityException
* @throws NoSuchFieldException
*/
public int del(Clazz clz) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException {
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
* @throws SecurityException
* @throws NoSuchFieldException
*/
public int edit(Clazz clz) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException {
String sql="update t_struts_class set cname=?,cteacher=?,pic=? where cid = ?";
return super.executeUpdate(sql, new String [] {"cname","cteacher","pic","cid"}, clz);
}
}
BaseAction工具类:
package com.xhh.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();
}
}
4、web层去调用dao层给前台返回数据:
ClazzAction:
package com.xhh.web;
import java.sql.SQLException;
import com.opensymphony.xwork2.ModelDriven;
import com.xhh.dao.ClzDao;
import com.xhh.entity.Clazz;
import com.xhh.util.BaseAction;
import com.xhh.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 (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
/**
* 跳转编辑页面(新增编辑页面是同一张)
* @return
*/
public String preSave() {
int cid = clz.getCid();
if(cid!=0) {
try {
this.result=clzDao.list(clz, null).get(0);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "preSave";
}
/**
* 增加班级信息方法
* @return
* @throws SecurityException
* @throws NoSuchFieldException
*/
public String add(){
try {
this.code=this.clzDao.add(clz);
} catch (IllegalArgumentException | IllegalAccessException | SQLException | NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/**、
* 修改班级信息
* @return
* @throws SecurityException
* @throws NoSuchFieldException
*/
public String edit() {
try {
this.code=this.clzDao.edit(clz);
} catch (IllegalArgumentException | IllegalAccessException | SQLException | NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
/**
* 删除班级信息
* @return
* @throws SecurityException
* @throws NoSuchFieldException
*/
public String del(){
try {
this.code=this.clzDao.del(clz);
} catch (IllegalArgumentException | IllegalAccessException | SQLException | NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
@Override
public Clazz getModel() {
// TODO Auto-generated method stub
return clz;
}
}
5、在struts_sy.xml进行配置:
配置struts-sy文件:
<?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>
<!--
相对mvc的差异性
package:用来将一类子控制器进行分类
http://localhost:8080/stauts/sy/user_add.action
中/sy对应的namespace="/sy"
extends包的继承
*的含义:
代表任意方法,只要前台浏览器匹配/user_*这一个是,那么user_add中,代表了adds
-->
<package name="sy" extends="base" namespace="/sy">
<action name="/user_*" class="com.xhh.web.UserAction" method="{1}">
<result name="success">/test.jsp</result>
</action>
<action name="demo_*" class="com.xhh.web.DemoAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="tomcat_*" class="com.xhh.web.TomacatAction" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="ognl_*" class="com.xhh.ognl.Demo7" method="{1}">
<result name="rs">/rs.jsp</result>
</action>
<action name="clz_*" class="com.xhh.web.ClazzAction" method="{1}">
<result name="list">/clzList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="redirectAction">clz_list</result>
</action>
</package>
</struts>
6、写jsp
clzList.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="/xhh" prefix="x" %>
<!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="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>
<x:page pageBean="${pageBean}"></x:page>
</body>
</html>
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>Insert title here</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" name="cid" value="${result.cid }"><br>
cname:<input type="text" name="cname" value="${result.cname }"><br>
cteacher:<input type="text" name="cteacher" value="${result.cteacher }"><br>
<input type="hidden" name="pic" value="${result.pic }"><br>
<input type="submit" value="ok">
</form>
</body>
</html>
7.测试: