接下来实现一些多个模块都要用到的工具类。
1. 浏览器要经常处理用户的请求,以及响应,所以编码过滤器是有必要实现的。
java代码
public class EncodingFilter implements Filter { private String encoding="UTF-8"; @Override public void init(FilterConfig filterConfig) throws ServletException { if (filterConfig.getInitParameter("ENCODING")!=null){ encoding=filterConfig.getInitParameter("Encoding"); } } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { servletRequest.setCharacterEncoding(encoding); servletResponse.setCharacterEncoding(encoding); filterChain.doFilter(servletRequest,servletResponse); } @Override public void destroy() { encoding=null; } }
在web.xml中的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<!--编码过滤器配置--> <filter> <filter-name>Encoding</filter-name> <filter-class>per.lc.sms.global.EncodingFilter</filter-class> <init-param> <param-name>ENCODING</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.核心控制器:
规范用户的请求,例如:department/add.do,这个请求经过核心控制器处理后,会去IOC容器中去找名为departmentController的<bean>,我们知道spring中的bean是一个个的java类,然后执行类中名为add的方法,方法中会用到业务层的对象,业务层用到持久层的对象。代码在后续体现。这样做让代码逻辑清晰很多。
public class DispatcherServlet extends GenericServlet { private ApplicationContext context=null; @Override public void init() throws ServletException { super.init(); context=new ClassPathXmlApplicationContext("spring.xml"); } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { HttpServletRequest request=(HttpServletRequest)servletRequest; HttpServletResponse response=(HttpServletResponse)servletResponse; String path=request.getServletPath().substring(1); String beanName=null; String methodName=null; int index=path.indexOf(‘/‘); if(index!=-1){ beanName=path.substring(0,index)+"controller"; methodName=path.substring(index+1,path.indexOf(".do")); }else{ beanName="selfController"; methodName=path.substring(0,path.indexOf(".do")); } Object obj=context.getBean(beanName); try { Method method=obj.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);//参数代表目标方法里的参数的类型。复习Java反射的知识 method.invoke(obj,request,response);//第一个参数代表目标对象(就是执行它里面的方法) } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
在web.xml中的配置
<servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>per.lc.sms.global.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
Personnel management system --第二天
原文:https://www.cnblogs.com/liu-chen/p/11643616.html