栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Struts之CURD

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Struts之CURD

1、新建Maven项目(详细借鉴上一篇) 2、导入pom.xml依赖(jar依赖)

web.xml改成3.1

导入struts相关的配置文件

jdk1.5-->1.8-->同时pom.xml需要添加plugins>plugin

web.2.3-->3.1

3、导入pom.xml依赖 4、baseAction

 继承 ActionSupport

实现 ServletRequestAware,ServletResponseAware,ModelDriven

①、对于常用的结果进行统一管理,编码错误问题 ②、关于tomcat集成常用对象进行统一管理,如:request、response、session ③、封装了携带到页面的常量
package com.zking.util;

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;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public abstract class  baseAction extends ActionSupport implements ServletRequestAware,ServletResponseAware,ModelDriven{
	protected HttpServletResponse resp;
	protected HttpServletRequest req;
	protected HttpSession session;
	
//1、编码习惯问题,容易出现大小写配置错误
	protected static final String LIST="list";
	protected static final String TOLIST="toList";
	protected static final String TOEDIT="toEdit";
//	2、每个子控制器都要实现对应接口,拿到request、response对象
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.resp=arg0;
		
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.req=arg0;
		this.session=arg0.getSession();
	}
	
//	3、向前端页面反馈的数据变量不统一
	protected Object result;
	protected String msg;
	protected int code;
	public Object getResult() {
		return result;
	}
	public void setResult(Object result) {
		this.result = result;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	
	
	
}
5、ClzAction
package com.dhm.web;

import com.dhm.dao.ClzDao;
import com.dhm.entity.Clz;
import com.zking.util.baseAction;
import com.zking.util.PageBean;

public class ClzAction extends baseAction{
       private Clz clz=new Clz();
       private ClzDao clzDao=new ClzDao();
       
      @Override
    public Clz getModel() {
    	// TODO Auto-generated method stub
    	return clz;
    }
       
       public String list() throws Exception{
    	   PageBean pageBean=new PageBean();
    	   pageBean.setRequest(req);
    	   this.result=this.clzDao.list(clz, pageBean);
    	   this.req.setAttribute("result", result);
    	   this.req.setAttribute("pageBean", pageBean);
    	   return LIST;
       }
       
       public String toEdit() throws Exception{
    	   int cid = clz.getCid();
   		if(cid!=0) {
   			this.result=this.clzDao.list(clz, null).get(0);
   			this.req.setAttribute("result", result);
   		}
    	   return TOEDIT;
       }
       
       
       public String add() throws Exception{
    	   this.clzDao.add(clz);
    	   return TOLIST;
       }
       
       
       public String edit() throws Exception{
    	   this.clzDao.edit(clz);
    	   return TOLIST;
       }
	
       
       public String del() throws Exception{
    	   this.clzDao.del(clz);
    	   return TOLIST;
       }
       
}
5、ClzDao
package com.dhm.dao;

import java.util.List;

import com.dhm.entity.Clz;
import com.zking.util.baseDao;
import com.zking.util.PageBean;

public class ClzDao extends baseDao{

	public List list(Clz clz, PageBean pageBean) throws Exception {
		String sql="select * from t_struts_class where 1=1";
		int cid = clz.getCid();
		if(cid!=0) {
			sql+=" and cid = "+cid;
		}
		
		return super.executeQuery(sql, Clz.class, pageBean);
	}
	
	
		public void add( Clz t) throws Exception {
			String sql="insert into t_struts_class values(?,?,?,?)";
			super.executeUpdate(sql, t, new String[] {"cid","cname","cteacher","pic"});
		}
	
	
		public void edit( Clz t) throws Exception {
			String sql="update t_struts_class set cname=?,cteacher=?,pic=? where cid=?";
			super.executeUpdate(sql, t, new String[] {"cname","cteacher","pic","cid"});
		}
		
		
		public void del( Clz t) throws Exception {
			String sql="delete from t_struts_class where cid=?";
			super.executeUpdate(sql, t, new String[] {"cid"});
		}
	
}
7、struts-sy.xml配置

  type属性有四个选项:
             1、默认forward:标签体对应的转发页面;
             2、action:标签体对应的转发的Action后台方法;
             3、redirect:标签体对应的重定向方法:
             4、redirectAction:标签体对应的重定向;


    
           
      /clz_list
      /clzList.jsp
      /clzEdit.jsp
      
    

8、界面 clzList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>	
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>	







书籍列表

.page-item input {
	padding: 0;
	width: 40px;
	height: 100%;
	text-align: center;
	margin: 0 6px;
}


.page-item input, .page-item b {
	line-height: 38px;
	float: left;
	font-weight: 400;
}

.page-item.go-input {
	margin: 0 10px;
}



	
新增
ID 班级名 教员 图片 操作
${b.cid } ${b.cname } ${b.cteacher } ${b.pic } 修改 删除
clzEdit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" prefix="z"%>	
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>	







书籍新增/修改


	
ID:
班级名称:
教员:
图片:
9、界面效果

修改界面

 

---------------没有了------------------------- 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/324751.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号