hibernate练习:保存客户及通用编码解决方案GenericEncodingFilter
2018-01-15 22:08
393 查看
准备:
1、创建web项目
2、导包:hibernate包、数据库驱动包、标签库包、BeanUtils包
3、引入静态页面
4、搭建hibernate框架
思路分析:
![](https://img-blog.csdn.net/20180117005038736?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpeGluXzM5NDcyMzQx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![](https://img-blog.csdn.net/20180117013400455?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VpeGluXzM5NDcyMzQx/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
customer/add.jsp
AddCustomerServlet
CustomServiceImpl
CustomerDaoImpl
GenericEncodingFilter
web.xml
1、创建web项目
2、导包:hibernate包、数据库驱动包、标签库包、BeanUtils包
3、引入静态页面
4、搭建hibernate框架
思路分析:
customer/add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <TITLE>添加客户</TITLE> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <LINK href="${pageContext.request.contextPath }/css/Style.css" type=text/css rel=stylesheet> <LINK href="${pageContext.request.contextPath }/css/Manage.css" type=text/css rel=stylesheet> <META content="MSHTML 6.00.2900.3492" name=GENERATOR> </HEAD> <BODY> <!-- 保存客户业务功能 --> <FORM id=form1 name=form1 action="${pageContext.request.contextPath }/addCustomer" method=post> <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0> <TBODY> <TR> <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_019.jpg" border=0></TD> <TD width="100%" background="${pageContext.request.contextPath }/images/new_020.jpg" height=20></TD> <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_021.jpg" border=0></TD> </TR> </TBODY> </TABLE> <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0> <TBODY> <TR> <TD width=15 background=${pageContext.request.contextPath }/images/new_022.jpg><IMG src="${pageContext.request.contextPath }/images/new_022.jpg" border=0></TD> <TD vAlign=top width="100%" bgColor=#ffffff> <TABLE cellSpacing=0 cellPadding=5 width="100%" border=0> <TR> <TD class=manageHead>当前位置:客户管理 > 添加客户</TD> </TR> <TR> <TD height=2></TD> </TR> </TABLE> <TABLE cellSpacing=0 cellPadding=5 border=0> <TR> <td>客户名称:</td> <td> <INPUT class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_name"> </td> <td>客户级别 :</td> <td> <INPUT class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_level"> </td> </TR> <TR> <td>信息来源 :</td> <td> <INPUT class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_source"> </td> <td>联系人:</td> <td> <INPUT class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_linkman"> </td> </TR> <TR> <td>固定电话 :</td> <td> <INPUT class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_phone"> </td> <td>移动电话 :</td> <td> <INPUT class=textbox id=sChannel2 style="WIDTH: 180px" maxLength=50 name="cust_mobile"> </td> </TR> <tr> <td rowspan=2> <INPUT class=button id=sButton2 type=submit value=" 保存 " name=sButton2> </td> </tr> </TABLE> </TD> <TD width=15 background="${pageContext.request.contextPath }/images/new_023.jpg"> <IMG src="${pageContext.request.contextPath }/images/new_023.jpg" border=0></TD> </TR> </TBODY> </TABLE> <TABLE cellSpacing=0 cellPadding=0 width="98%" border=0> <TBODY> <TR> <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_024.jpg" border=0></TD> <TD align=middle width="100%" background="${pageContext.request.contextPath }/images/new_025.jpg" height=15></TD> <TD width=15><IMG src="${pageContext.request.contextPath }/images/new_026.jpg" border=0></TD> </TR> </TBODY> </TABLE> </FORM> </BODY> </HTML>
AddCustomerServlet
package cn.itheima.web; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.beanutils.BeanUtils; import cn.itheima.domain.Customer; import cn.itheima.service.CustomerService; import cn.itheima.service.impl.CustomerServiceImpl; /** * Servlet implementation class AddCustomerServlet */ public class AddCustomerServlet extends HttpServlet { private static final long serialVersionUID = 1L; private CustomerService customerService = new CustomerServiceImpl(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 1.获得参数并封装到Customer对象中 Customer c = new Customer(); try { Map<String, String[]> parameterMap = request.getParameterMap(); BeanUtils.populate(c, parameterMap); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } // 2.调用service层保存客户 customerService.save(c); // 3.重定向到客户列表页面 // response.sendRedirect(request.getContextPath() + "/listCustomer "); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
CustomServiceImpl
package cn.itheima.service.impl; import cn.itheima.dao.CustomerDao; import cn.itheima.dao.impl.CustomerDaoImpl; import cn.itheima.domain.Customer; import cn.itheima.service.CustomerService; public class CustomerServiceImpl implements CustomerService { private CustomerDao customerDao = new CustomerDaoImpl(); public void save(Customer c) { //调用dao保存客户 customerDao.save(c); } }
CustomerDaoImpl
package cn.itheima.dao.impl; import org.hibernate.Session; import org.hibernate.Transaction; import cn.itheima.dao.CustomerDao; import cn.itheima.domain.Customer; import cn.itheima.utils.HibernateUtils; public class CustomerDaoImpl implements CustomerDao { // 保存客户 public void save(Customer c) { //1、获得session Session session = HibernateUtils.openSession(); //2、开启事务 Transaction tx = session.beginTransaction(); //3、执行保存 session.save(c); //4、提交事务 tx.commit(); //5、 关闭session session.close(); } }
GenericEncodingFilter
package cn.itheima.web.filter; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Map; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; /** * 通用编码解决方案 * */ public class GenericEncodingFilter implements Filter { @Override public void destroy() { } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 转型为与协议相关对象 HttpServletRequest httpServletRequest = (HttpServletRequest) request; // 对request包装增强 HttpServletRequest myrequest = new MyRequest(httpServletRequest); chain.doFilter(myrequest, response); } @Override public void init(FilterConfig filterConfig) throws ServletException { } } // 自定义request对象 class MyRequest extends HttpServletRequestWrapper { private HttpServletRequest request; private boolean hasEncode; public MyRequest(HttpServletRequest request) { super(request);// super必须写 this.request = request; } // 对需要增强方法 进行覆盖 @Override public Map getParameterMap() { // 先获得请求方式 String method = request.getMethod(); if (method.equalsIgnoreCase("post")) { // post请求 try { // 处理post乱码 request.setCharacterEncoding("utf-8"); return request.getParameterMap(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } else if (method.equalsIgnoreCase("get")) { // get请求 Map<String, String[]> parameterMap = request.getParameterMap(); if (!hasEncode) { // 确保get手动编码逻辑只运行一次 for (String parameterName : parameterMap.keySet()) { String[] values = parameterMap.get(parameterName); if (values != null) { for (int i = 0; i < values.length; i++) { try { // 处理get乱码 values[i] = new String(values[i].getBytes("ISO-8859-1"), "utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } } hasEncode = true; } return parameterMap; } return super.getParameterMap(); } @Override public String getParameter(String name) { Map<String, String[]> parameterMap = getParameterMap be11 (); String[] values = parameterMap.get(name); if (values == null) { return null; } return values[0]; // 取回参数的第一个值 } @Override public String[] getParameterValues(String name) { Map<String, String[]> parameterMap = getParameterMap(); String[] values = parameterMap.get(name); return values; } }
web.xml
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>Hibernate_crm</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <description></description> <display-name>GenericEncodingFilter</display-name> <filter-name>GenericEncodingFilter</filter-name> <filter-class>cn.itheima.web.filter.GenericEncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>GenericEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <description></description> <display-name>AddCustomerServlet</display-name> <servlet-name>AddCustomerServlet</servlet-name> <servlet-class>cn.itheima.web.AddCustomerServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AddCustomerServlet</servlet-name> <url-pattern>/addCustomer</url-pattern> </servlet-mapping> </web-app>
相关文章推荐
- 015-GenericEncodingFilter模板【解决全局乱码】
- 【Java】全站编码过滤器GenericEncodingFilter代码与配置
- java;jsp;tomcat;mysql;hibernate;j2ee 编码中文乱码全面解决方案
- hibernate练习2:显示所有客户列表
- 解决中文乱码用spring经典编码过滤器org.springframework.web.filter.CharacterEncodingFilter
- Spring在web.xml请求中定义编码(org.springframework.web.filter.CharacterEncodingFilter)
- Spring在web请求中定义编码(org.springframework.web.filter.CharacterEncodingFilter)
- java;jsp;tomcat;mysql;hibernate;编码中文乱码全面解决方案
- Spring在web请求中定义编码(org.springframework.web.filter.CharacterEncodingFilter)
- java;jsp;tomcat;mysql;hibernate;j2ee 编码中文乱码全面解决方案
- Spring的编码过滤器CharacterEncodingFilter
- 【WEB小工具】EncodingFilter—设置全局编码
- 编码过滤器类CharacterEncodingFilter
- 阻止用户关闭网页,提示保存的解决方案IE/FF/OP通用(未经测试)
- 编写通用的字符集编码的过滤器Filter
- 【Spring进阶】Spring重写CharacterEncodingFilter完全解决编码问题
- hibernate 自动导入sql 文件import.sql 国际化编码的问题的解决方案
- java;jsp;tomcat;mysql;hibernate;j2ee 编码中文乱码全面解决方案
- Spring在web请求中定义编码(org.springframework.web.filter.CharacterEncodingFilter)
- Spring在web请求中定义编码(org.springframework.web.filter.CharacterEncodingFilter)