现在三大框架的兴起ssh spring springmvc 基于注解式的编程简单方便了开发,但是让我感觉摸不着头绪,框架固然是好,提高了开发效率,
对企业有很大的帮助,框架封装的一些底层的东西让我不知道为什么要这么做,只知道该这么做,编程的路线肯定是简单快捷,趋势就是随便点点拖拖,就是一个项目,以后就没有什么代码编程了,我就是好奇为什么要这样,底层的东西到底是什么,闲来无事搞个servlet 玩玩,看看原始的servle怎么干活的,
我习惯先走个流程, 前台页面执行操作后台来处理
一个添加操作,输入点击添加 进入servlet : ../servlet/addTeacherType.html (创建servletshi在web.xml中配置)
1 页面userType_add.jsp
<%@ page language="java" contentType="text/html;charSet=UTF-8" pageEncoding="UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>添加教师类型</title> <link rel="stylesheet" href="../style/myschool.css"/> <style type="text/css"> .td1{ text-align: right; } .td2{ text-align: left; } </style> <script type="text/javascript" src="../script/myschool_validate.js"></script> <script type="text/javascript" src="../script/jquery-1.9.1.js"></script> <script type="text/javascript"> function goBack() { window.self.location="userType_maint.jsp"; } function addUserType() { var typeName=jQuery("#typeName").val(); if(typeName==""){ alert("教师类型不能为空!"); }else{ //参数typeName:通过post传typeName值 在servlet中req.getParameter("typeName")获取; jQuery.post("../servlet/addTeacherType.html",{typeName:typeName},function(data){ alert(JSON.stringify(data)); var www=jQuery(data).find("root").text(); alert(www); var r=jQuery(data).find("resultss").text(); if(r==1){ alert("添加成功!"); }else if(r==2){ alert("该类型名称已存在!"); }else{ alert("添加出现异常!"); } },'xml'); } } </script> </head> <body class="body1"> <form action="" name="userForm" target="_self" id="userForm" > <div align="center"> <table width="95%" border="0" cellspacing="2" cellpadding="2"> <tr> <td> </td> </tr> </table> <table width="95%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="522" class="p1" height="25"> <img src="../images/mark_arrow_03.gif" width="14" height="14"/> <b>系统管理>>教师维护>>添加</b> </td> </tr> </table> <hr width="97%" align="center" size="0"/> <table width="95%" border="0" cellpadding="0" cellspacing="0"> <tr> <td class="td1" width="22%" height="29"> <font color="#FF0000">*</font>教师类型名称: </td> <td class="td2" width="78%"> <input id="typeName" type="text" class="text1" size="10" maxlength="10"/> </td> </tr> </table> <hr width="97%" align="center" size="0"/> <div align="center"> <input name="btnAdd" class="button1" type="button" id="btnAdd" value="添加" onclick="addUserType()"/> <input name="btnBack" class="button1" type="button" id="btnBack" value="返回" onclick="goBack()" /> </div> </div> </form> </body> </html>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name></display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet><!-- 添加教师类型 ,参数:(typeName 类型名称) --> <servlet-name>addTeacherType</servlet-name> <servlet-class>com.myschool.servlet.AddTeacherTypeServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>addTeacherType</servlet-name> <url-pattern>/servlet/addTeacherType.html</url-pattern> </servlet-mapping> </web-app>
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.myschool.service.TeacherTypeManager; public class AddTeacherTypeServlet extends HttpServlet{ //1.页面/myschool/WebRoot/sysmgr/userType_add.jsp 访问servlet (/servlet/addTeacherType.html) //通过web.xml配置的/servlet/addTeacherType.html // 2.定位到servlet 这个类执行dopost(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp);//注意这个地方可以开个口 来切dopost() } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String typeName=req.getParameter("typeName");//获取页面的值标签 name = "typeName" int r=TeacherTypeManager.getInstance().addTeacherType(typeName); //1 .addTeacher.jsp代码 看servle请求http://localhost:8080/项目/servlet/addTeacherType.html?typeName=化学 //2. 进入xml 如下 /** * <?xml version="1.0" encoding="UTF-8"?> * <%@ page language="java" contentType="text/xml;charSet=UTF-8" * pageEncoding="UTF-8"%> * <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> * <root> * <result><c:out value="${result}"></c:out></result><!-- 1:成功 0:出现异常 2:该教师编号已经存在 --> * </root> */ //3. 在页面 var r=jQuery(data).find("result").text();获取xml中result的值 req.setAttribute("result", r);//${result} req.getRequestDispatcher("../view/sysmgr/addTeacherType.jsp").forward(req, resp);//发送到xml } }
hibernate.cfg.xml ,和xxxxx.hbm.xml 就不贴了
service层
import java.util.List; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import com.myschool.entity.TeacherType; import com.myschool.util.HibernateUtil; import com.myschool.util.PageModel; public class TeacherTypeManager { private static TeacherTypeManager instance; public static TeacherTypeManager getInstance(){ if(instance==null){ instance=new TeacherTypeManager(); } return instance; } /** * 根据类型名称查找类型对象 * @param typeName * @return */ public TeacherType getTeacherType(String typeName){ Session session=HibernateUtil.getSessionFactory().getCurrentSession(); try{ session.beginTransaction(); List list=session.createQuery("from TeacherType where name=?").setString(0,typeName).list(); if(list.size()>0){ return (TeacherType)list.get(0); } }catch(Exception e){ e.printStackTrace(); }finally{ //session.close(); session.getSessionFactory().close(); } return null; } /** * 根据类型名称添加教师类型 * @param typeName * @return */ public int addTeacherType(String typeName){ Session session=HibernateUtil.getSessionFactory().getCurrentSession(); try{ session.beginTransaction(); TeacherType tt=this.getTeacherType(typeName); if(tt==null){ tt=new TeacherType(); tt.setName(typeName); //这里保存的时候有待解决 持有的对象不可以保存
session.merge(tt); return 1; }else{ return 2; } }catch(Exception e){ e.printStackTrace(); return 3; }finally{ session.getTransaction().commit(); session.getSessionFactory().close(); }<span style="font-family: Arial, Helvetica, sans-serif;"> </span>
<pre name="code" class="java">}addTeacherType.jsp xml
<?xml version="1.0" encoding="UTF-8"?> <%@ page language="java" contentType="text/xml;charSet=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <root>ddd <resultss><c:out value="${result}"></c:out></resultss><!-- 1:成功;2:已经存在;3:出现异常 --> </root>
用get请求时返回的页面