万能MVC3

万能MVC3

把自己写好的工具类打成jir包:
1、首先把所要打包的文件选中,然后右键找到Export、
在这里插入图片描述
2、找到JAR file选择你要导出的文件路径
在这里插入图片描述
3、最后点击finish,就把文件成功的导出来了
在这里插入图片描述
自定义mvc的最后一次升级
1、导进所需要的框架jar包
2、直接调用泛型de的增删改查方法
一、写好通用BaseDao:

public class BaseDao<T> {//T是泛型对象
	private static final String T = null;
	
	Connection con=null;//扩大作用域
	PreparedStatement ps=null;
	ResultSet rs=null;
	public List<T> executeQuery(String sql,Class clz,PageBean pagebean) throws InstantiationException, IllegalAccessException, SQLException{
			List<T> list;
			try {
				list = new ArrayList<>();
				con = DBAccess.getConnection();
				if (pagebean != null && pagebean.isPagination()) {
					//该分页了
					String countSql=getCountSql(sql);
					ps=con.prepareStatement(countSql);
					rs=ps.executeQuery();//结果集
					if(rs.next()) {
						pagebean.setTotal(rs.getLong(1)+"");
					}	
					String pageSql=getPageSql(sql,pagebean);
					ps=con.prepareStatement(pageSql);
					rs=ps.executeQuery();
					
				} else {
					ps = con.prepareStatement(sql);
					rs = ps.executeQuery();

				}
				while (rs.next()) {
					T t = (T) clz.newInstance();
					Field[] fields = clz.getDeclaredFields();
					for (Field field : fields) {
						field.setAccessible(true);
						field.set(t, rs.getObject(field.getName()));
					}
					list.add(t);
				} 
			} finally {
				DBAccess.close(con);
			}
				return list;
	}
	
	
	private String getPageSql(String sql, PageBean pagebean) {
		return sql + " limit "+pagebean.getStartIndex() +","+pagebean.getRows();
	}
	private String getCountSql(String sql) {
		// TODO Auto-generated method stub
		return "select count(1) from ("+sql+") t";
	}
	
	/**
	 * 通用的增、删、改方法
	 * @param sql  
	 * @param attrs    ? 占位符:代表的数据库的实体类的属性对象值
	 * @param t       泛型:实体类的实例
	 * @return
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public int executeUpdate(String sql,String[] attrs,T t) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		Connection con = DBAccess.getConnection();
		PreparedStatement ps = con.prepareStatement(sql);
		for (int i = 0; i < attrs.length; i++) {
			Field field = t.getClass().getDeclaredField(attrs[i]);
			field.setAccessible(true);
			ps.setObject(i+1, field.get(t));
			
		}
		return ps.executeUpdate();
	}
}


二、写一个自己具体要用的BookDao,继承BaseDao:

public class BookDao extends BaseDao<Book>{

	//增加
	public int add(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		String sql="insert into t_mvc_book values(?,?,?)";
		return super.executeUpdate(sql, new String[] {"bid","bname","price"}, book);
	}
	
	//删除
	public int delete(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		String sql="delete from t_mvc_book where bid=?";
		return super.executeUpdate(sql, new String[] {"bid"}, book);
	}
	
		//带分页查询
	public List<Book> list(Book book,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_mvc_book where true";
		if(util.StringUtils.isNotBlank(book.getBname())) {
			sql+=" and bname like '%"+book.getBname()+"%'";
		}
		return super.executeQuery(sql, Book.class, pageBean);
	}
	
	//修改
	public int update(Book book) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		String sql="update t_mvc_book set bname=?,price=? where bid=?";
		return super.executeUpdate(sql, new String[] {"bname","price","bid"}, book);
		
	}
}


三、在主控器的基础上编写子控制器BookAction:

public class BookAction extends ActionSupport implements ModelDriven<Book>{

	private BookDao bookDao=new BookDao();//private:利于不被其他页面所访问到
	private Book book=new Book();

   /**
    * 查询分页数据
    * 
    * @param req
    * @param resp
    * @return
    */
	public String list(HttpServletRequest req,HttpServletResponse resp) {
		try {
			PageBean pageBean=new PageBean();
			pageBean.setRequest(req);
			List<Book> list = this.bookDao.list(book, pageBean);
			req.setAttribute("bookList", list);//存值
			req.setAttribute("pageBean", pageBean);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * 增加书籍
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	public String add(HttpServletRequest req,HttpServletResponse resp) {
		try {
			int n = this.bookDao.add(book);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * 删除书籍
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	public String delete(HttpServletRequest req,HttpServletResponse resp) {
		try {
			int n = this.bookDao.delete(book);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * 需要进行更改的书籍
	 * 
	 * 修改方法
	 * @param req
	 * @param resp
	 * @return
	 */
	public String load(HttpServletRequest req,HttpServletResponse resp) {
		try {
			List<Book> list=this.bookDao.list(book, null);
			req.setAttribute("book", list.get(0));
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return "toEdit";
	}
	
	/**
	 * 开始修改书籍信息
	 * @param req
	 * @param resp
	 * @return
	 */
	public String update(HttpServletRequest req,HttpServletResponse resp)  {
		try {
			int n = this.bookDao.update(book);
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException 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 "toList";
	}

	@Override
	public Book getModel() {
		// TODO Auto-generated method stub
		return book;
	}
}


四、前台页面的编写 bookList.jsp:


<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主界面</title>
<script type="text/javascript">
		function add(){
			window.location.href="bookEdit.jsp";
		}
		function update(bid){
			window.location.href="${pageContext.request.contextPath}/bookAction.action?methodName=load&&bid="+bid;
		}
		function del(bid){
			window.location.href="${pageContext.request.contextPath}/bookAction.action?methodName=delete&&bid"+bid;
		}
</script>
</head>
<body>

<h2>小说目录</h2> 
	<form action="${pageContext.request.contextPath}/bookAction.action?methodName=list"
		method="post">
		书名:<input type="text" name="bname"> <input type="submit"
			value="确定">
	</form>
	<button onclick="add();">新增书籍</button>
	<table border="1" width="100%">
		<tr>
			<td>编号</td>
			<td>名称</td>
			<td>价格</td>
			<td>操作</td>
		</tr>
		<c:forEach items="${bookList }" var="b">
			<tr>
				<td>${b.bid }</td>
				<td>${b.bname }</td>
				<td>${b.price }</td>
				<td>
					<button onclick="update(${b.bid });">修改</button>&nbsp;&nbsp;&nbsp;
					<button onclick="del(${b.bid });">删除</button>
				</td>
			</tr>
		</c:forEach>
	</table>
</body>



写一个bookEdit.jsp:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>增加/修改界面</title>

<script type="text/javascript">

		function doSubmit(bid){
			var bookForm =document.getElementById("bookForm");
			if(bid){
				//修改
				bookForm.action='${pageContext.request.contextPath}/bookAction.action?methodName=update';
			}else{
				//新增
				bookForm.action='${pageContext.request.contextPath}/bookAction.action?methodName=add';
			}
			bookForm.submit();
		}

</script>
</head>

<body>

	<form id="bookForm" method="post" action="${pageContext.request.contextPath}/bookAction.action?methodName=list">
	      bid:<input name="bid" value="${book.bid }"><br>
	      bname:<input name="bname" value="${book.bname }"><br>
	      price:<input name="price" value="${book.price }"><br>
	      <input type="submit" value="提交" onclick="doSubmit();"><br>
	
	</form>
	
</body>


五、mvc的配置:

	<action path="/bookAction" type="web.BookAction">
		<forward name="list" path="/book.jsp" redirect="false" />
		<forward name="toList" path="bookAction.action?methodName=list" redirect="true" />
		<forward name="toEdit" path="/bookEdit.jsp" redirect="false" />
	</action>


六、web配置

<filter>
  			<filter-name>encodingFiter</filter-name>
            <filter-class>util.EncodingFiter</filter-class>
  </filter>
  <filter-mapping>
            <filter-name>encodingFiter</filter-name>
            <url-pattern>/*</url-pattern>
  </filter-mapping>
  
   <servlet>
   			<servlet-name>dispatherServlet</servlet-name>
            <servlet-class>com.wyy.framework.DispatherServlet </servlet-class>
   </servlet>
   <servlet-mapping>
   		<servlet-name>dispatherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
   </servlet-mapping>


超强版的mvc做的增删查改要注意的是防止它重复传值

还有前面要查看定义的jar里的代码需要下载一个JD-GUI(Java反编译工具),要查看哪个jar的内容就直接把哪个jar托进JD-GUI中就可以显示其内容也就是代码:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值