1、导入相关的pom依赖(struts、自定义标签库的依赖)
pom.xml
4.0.0
com.zking
struts
war
0.0.1-SNAPSHOT
struts Maven Webapp
http://maven.apache.org
junit
junit
4.12
test
mysql
mysql-connector-java
5.1.44
javax.servlet
javax.servlet-api
4.0.1
provided
javax.servlet
javax.servlet-api
4.0.1
provided
org.apache.struts
struts2-core
2.5.16
jstl
jstl
1.2
taglibs
standard
1.1.2
org.apache.tomcat
tomcat-jsp-api
8.0.47
struts
org.apache.maven.plugins
maven-compiler-plugin
3.7.0
1.8
1.8
UTF-8
2、分页的tag类导入、z.tld、完成web.xml的配置
pageTag
package com.jt.tag;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.jt.util.PageBean;public classPageTag extends BodyTagSupport {private static final long serialVersionUID = -553969530960334073L;privatePageBean pageBean;publicPageBean getPageBean() {returnpageBean;
}public voidsetPageBean(PageBean pageBean) {this.pageBean =pageBean;
}
@Overridepublic intdoStartTag() throws JspException {
JspWriterout =pageContext.getOut();try{out.print(toHTML());
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnsuper.doStartTag();
}privateString toHTML() {//TODO Auto-generated method stub
StringBuilder sb=newStringBuilder();//拼接下一次发送请求所要提交得隐藏得form表单
sb.append("
");sb.append("");
Map paMap =pageBean.getPaMap();if(paMap!=null&&paMap.size()>0) {
Set> entrySet =paMap.entrySet();for (Entryentry : entrySet) {//上一次请求可能携带页码namepage得参数,但是改参数在前面以及单独赋值//为什么要单独赋值?因为上一次请求时第一页得数据,下一次可能时第二页,以后这前后请求page对应得值不一样,需要单独赋值
if(!"page".equals(entry.getKey())) {//[1,2]
for(String val:entry.getValue()) {
sb.append("");
}
}
}
}
sb.append("
");//拼接分页条sb.append("
sb.append("每页"+pageBean.getRows()+"条,共"+pageBean.getTotal()+"条,第"+pageBean.getPage()+"页,共"+pageBean.getMaxPage()+"页
sb.append("href='javascript:gotoPage(1)'>首页
sb.append("href='javascript:gotoPage("+pageBean.getpreviousPage()+")'>上一页
sb.append("href='javascript:gotoPage("+pageBean.getNextPage()+")'>下一页
sb.append("href='javascript:gotoPage("+pageBean.getMaxPage()+")'>尾页
sb.append("id='skipPage'");
sb.append("style='text-align: center; font-size: 12px; width: 50px;'>
sb.append("href='javascript:skipPage()'>Go
");sb.append("
sb.append("
sb.append("function gotoPage(page) {");
sb.append("document.getElementById('pageBeanForm').page.value = page;");
sb.append("document.getElementById('pageBeanForm').submit();");
sb.append("}");
sb.append("function skipPage() {");
sb.append("var page = document.getElementById('skipPage').value;");
sb.append("if(!page || isNaN(page) || parseInt(page)<1 || parseInt(page)>"+pageBean.getMaxPage()+"){");
sb.append("alert('请输入1~N的数字');");
sb.append("return;");
sb.append("}");
sb.append("gotoPage(page);");
sb.append("}");
sb.append("");returnsb.toString();
}
}
z.tld
page
com.jt.tag.PageTag
JSP
pageBean
true
true
web.xml
Archetype Created Web Application
encodingFiter
com.jt.util.EncodingFiter
encodingFiter
/*
struts2
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
struts2
*.action
3、dao层去访问数据
实体类
Clazz
package com.jt.entity;public classClazz {private intcid;privateString cname;privateString cteacher;private String pic="图片暂定";public intgetCid() {returncid;
}public void setCid(intcid) {this.cid =cid;
}publicString getCname() {returncname;
}public voidsetCname(String cname) {this.cname =cname;
}publicString getCteacher() {returncteacher;
}public voidsetCteacher(String cteacher) {this.cteacher =cteacher;
}publicString getPic() {returnpic;
}public voidsetPic(String pic) {this.pic =pic;
}
@OverridepublicString toString() {return "Clazz [cid=" + cid + ", cname=" + cname + ", cteacher=" + cteacher + ", pic=" + pic + "]";
}
}
Clazzdao
package com.jt.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.jt.entity.Clazz;
import com.jt.util.BaseDao;
import com.jt.util.PageBean;public class ClazzDao extends BaseDao{public Listlist(Clazz clazz,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String sql="select * from t_struts_class where true";
String cname=clazz.getCname();int cid=clazz.getCid();if(cid!=0) {
sql+="and cid ="+cid;
}if(StringUtils.isNoneBlank(cname)) {
sql+="and cname like '%"+cname+"%'";
}return super.executeQuery(sql,Clazz.class, pageBean);
}public intadd(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="insert into t_struts_class values(?,?,?,?)";return super.executeupdate(sql, new String[] {"cid","cname","cteacher","pic"}, clazz);
}public intdel(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="delete from t_struts_class where cid=?";return super.executeupdate(sql, new String[] {"cid"}, clazz);
}public intedit(Clazz clazz) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
String sql="update t_struts_class set cname=?,cteacher=?,pic=?";return super.executeupdate(sql, new String[] {"cname","cteacher","pic","cid"}, clazz);
}
}
4、web层去调用dao层给前台返回数据
ClazzAction.java
package com.jt.web;
import java.sql.SQLException;
import java.util.List;
import com.jt.dao.ClazzDao;
import com.jt.entity.Clazz;
import com.jt.util.BaseAction;
import com.jt.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;public class ClazzAction extends BaseAction implements ModelDriven{private Clazz clz=newClazz();private ClazzDao clzDao=newClazzDao();publicString list(){
PageBean pageBean=newPageBean();
pageBean.setRequest(request);try{
List list = this.clzDao.list(clz, pageBean);
request.setAttribute("clzList",list);
request.setAttribute("pageBean",pageBean);
}catch (InstantiationException | IllegalAccessException |SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}return "list";
}/**
* 跳转编辑页面 (新增修改页面)
* @return*/
publicString preSave() {if(clz.getCid()!=0) {try{this.result= this.clzDao.list(clz, null).get(0);
}catch (InstantiationException | IllegalAccessException |SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}return "preSave";
}publicString add() {try{this.code= this.clzDao.add(clz);
}catch (NoSuchFieldException | SecurityException | IllegalArgumentException |IllegalAccessException|SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}return "toList";
}publicString edit() {try{this.clzDao.edit(clz);
}catch (NoSuchFieldException | SecurityException | IllegalArgumentException |IllegalAccessException|SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}return "toList";
}publicString del() {try{this.clzDao.del(clz);
}catch (NoSuchFieldException | SecurityException | IllegalArgumentException |IllegalAccessException|SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}return "toList";
}
@OverridepublicClazz getModel() {//TODO Auto-generated method stub
returnclz;
}
}
5、在struts_sy.xml进行配置
/p>
"http://struts.apache.org/dtds/struts-2.5.dtd">
/success.jsp
/clzList.jsp
/clzEdit.jsp
/clz_list
6、写jsp
clzList.jsp
Insert title here小说目录
书名:编号 | 班级名称 | 老师 | 班级图片 | |
${b.cid } | ${b.cname } | ${b.cteacher } | ${b.pic } | href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改 href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">删除 href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">图片上传 |
clzEdit.jsp
Insert title herecid:cname:
cteacher: