From adaf69896e9d53901ac6d63fc5cf94a334eb22a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=A0=E9=99=90=E8=B6=85=E9=A2=91?= <1029559041@qq.com> Date: Thu, 14 Sep 2017 18:54:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 30 +++------ .../java/org/pqh/config/SpringConfig.java | 15 ++++- src/main/java/org/pqh/config/WebConfig.java | 65 +++++++++---------- .../org/pqh/controller/AdminController.java | 15 +++++ .../org/pqh/controller/BaseController.java | 29 +++++++++ .../org/pqh/controller/ConfigController.java | 14 ++++ .../org/pqh/controller/ParamController.java | 14 +--- .../BaseService.java} | 15 ++--- src/main/webapp/WEB-INF/jsp/index.jsp | 4 +- 9 files changed, 120 insertions(+), 81 deletions(-) create mode 100644 src/main/java/org/pqh/controller/AdminController.java create mode 100644 src/main/java/org/pqh/controller/BaseController.java create mode 100644 src/main/java/org/pqh/controller/ConfigController.java rename src/main/java/org/pqh/{controller/AbstractController.java => service/BaseService.java} (77%) diff --git a/pom.xml b/pom.xml index 6c5db3c..1ade8d3 100644 --- a/pom.xml +++ b/pom.xml @@ -22,33 +22,21 @@ org.springframework spring-web - LATEST + RELEASE org.springframework spring-webmvc - LATEST + RELEASE org.springframework spring-orm - LATEST + RELEASE - - - - - - - - - - - - org.apache.commons commons-dbcp2 @@ -57,7 +45,7 @@ org.hibernate hibernate-core - LATEST + 5.2.10.Final @@ -96,11 +84,11 @@ LATEST - - org.fusesource.jansi - jansi - LATEST - + + + + + commons-io diff --git a/src/main/java/org/pqh/config/SpringConfig.java b/src/main/java/org/pqh/config/SpringConfig.java index 4127739..2d9eedd 100644 --- a/src/main/java/org/pqh/config/SpringConfig.java +++ b/src/main/java/org/pqh/config/SpringConfig.java @@ -1,7 +1,9 @@ package org.pqh.config; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.*; import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.view.InternalResourceViewResolver; /** @@ -12,8 +14,19 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; @ComponentScan(basePackages = "org.pqh") @EnableAspectJAutoProxy @Import(HibernateConfig.class) -public class SpringConfig{ +public class SpringConfig extends WebMvcConfigurationSupport{ + private static ApplicationContext applicationContext; + + public static T getBean(Class c){ + return applicationContext.getBean(c); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + super.setApplicationContext(applicationContext); + SpringConfig.applicationContext=applicationContext; + } @Bean("viewReslover") public InternalResourceViewResolver internalResourceViewResolver(){ diff --git a/src/main/java/org/pqh/config/WebConfig.java b/src/main/java/org/pqh/config/WebConfig.java index 13ffed1..64d649d 100644 --- a/src/main/java/org/pqh/config/WebConfig.java +++ b/src/main/java/org/pqh/config/WebConfig.java @@ -1,51 +1,46 @@ package org.pqh.config; import org.springframework.web.WebApplicationInitializer; -import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.filter.CharacterEncodingFilter; -import org.springframework.web.servlet.DispatcherServlet; -import javax.servlet.*; -import java.util.EnumSet; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; /** * Created by reborn on 2017/7/28. */ -public class WebConfig implements WebApplicationInitializer { - - private String suffix="*.miku"; - private String fileEncoding="UTF-8"; +public class WebConfig implements WebApplicationInitializer { + private String encoding ="UTF-8"; + + + +// @Override +// protected Filter[] getServletFilters() { +// org.springframework.web.SpringServletContainerInitializer +// org.springframework.web.SpringServletContainerInitializer +// return new Filter[]{new CharacterEncodingFilter(encoding)}; +// } +// +// @Override +// protected String[] getServletMappings() { +// return new String[]{"/"}; +// } +// +// @Override +// protected Class[] getRootConfigClasses() { +// return new Class[0]; +// } +// +// @Override +// protected Class[] getServletConfigClasses() { +// return new Class[]{SpringConfig.class}; +// } + + @Override public void onStartup(ServletContext servletContext) throws ServletException { - //基于注解配置的上下文 - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - //注册Spring容器配置类 - context.register(SpringConfig.class); - ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context)); - //配置映射路径 - servletContext.log("DispatcherServlet配置映射路径匹配规则:"+suffix); - servlet.addMapping("/",suffix); - //启动顺序 - servlet.setLoadOnStartup(1); - - - servletContext.addListener(new ContextLoaderListener(context)); - - FilterRegistration.Dynamic characterEncodingFilter=servletContext.addFilter("CharacterEncodingFilter", CharacterEncodingFilter.class); - servletContext.log("CharacterEncodingFilter设置编码:"+fileEncoding); - characterEncodingFilter.setInitParameter("encoding",fileEncoding); - characterEncodingFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*"); - -// FilterRegistration.Dynamic openSessionInViewFilter=servletContext.addFilter("OpenSessionInViewFilter", OpenSessionInViewFilter.class); -// openSessionInViewFilter.setInitParameter("flushMode","AUTO"); -// openSessionInViewFilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST),true,"/*"); - - } - } diff --git a/src/main/java/org/pqh/controller/AdminController.java b/src/main/java/org/pqh/controller/AdminController.java new file mode 100644 index 0000000..18f495a --- /dev/null +++ b/src/main/java/org/pqh/controller/AdminController.java @@ -0,0 +1,15 @@ +package org.pqh.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by reborn on 2017/9/14. + */ +@Controller +public class AdminController { + @RequestMapping({"","index"}) + private String index(){ + return "index"; + } +} diff --git a/src/main/java/org/pqh/controller/BaseController.java b/src/main/java/org/pqh/controller/BaseController.java new file mode 100644 index 0000000..e2fb7a2 --- /dev/null +++ b/src/main/java/org/pqh/controller/BaseController.java @@ -0,0 +1,29 @@ +package org.pqh.controller; + +import org.pqh.model.AbstractModel; +import org.pqh.service.BaseService; +import org.pqh.util.DBAction; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.annotation.Resource; + +/** + * Created by reborn on 2017/9/14. + */ +@Controller +public class BaseController { + + @Resource + BaseService baseService; + + @ResponseBody + @RequestMapping(value = "{action}",produces = "text/html;charset=UTF-8") + public String curd(@PathVariable DBAction action,T model) { + return baseService.curd(action, model); + } + + +} diff --git a/src/main/java/org/pqh/controller/ConfigController.java b/src/main/java/org/pqh/controller/ConfigController.java new file mode 100644 index 0000000..6ac3c13 --- /dev/null +++ b/src/main/java/org/pqh/controller/ConfigController.java @@ -0,0 +1,14 @@ +package org.pqh.controller; + +import org.pqh.model.Config; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by reborn on 2017/9/14. + */ +@Controller +@RequestMapping("/config") +public class ConfigController extends BaseController { + +} diff --git a/src/main/java/org/pqh/controller/ParamController.java b/src/main/java/org/pqh/controller/ParamController.java index c8f3b0c..4c184b8 100644 --- a/src/main/java/org/pqh/controller/ParamController.java +++ b/src/main/java/org/pqh/controller/ParamController.java @@ -1,22 +1,14 @@ package org.pqh.controller; import org.pqh.model.Param; -import org.pqh.util.DBAction; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; /** * Created by reborn on 2017/8/3. */ @Controller -public class ParamController extends AbstractController { - @Override - @ResponseBody - @RequestMapping(value = "/param/{action}",produces = "text/html;charset=UTF-8") - public String curd(@PathVariable DBAction action,@ModelAttribute Param formModel) { - return super.curd(action, formModel); - } +@RequestMapping("/param") +public class ParamController extends BaseController { + } diff --git a/src/main/java/org/pqh/controller/AbstractController.java b/src/main/java/org/pqh/service/BaseService.java similarity index 77% rename from src/main/java/org/pqh/controller/AbstractController.java rename to src/main/java/org/pqh/service/BaseService.java index edb0f04..05de1f1 100644 --- a/src/main/java/org/pqh/controller/AbstractController.java +++ b/src/main/java/org/pqh/service/BaseService.java @@ -1,29 +1,22 @@ -package org.pqh.controller; +package org.pqh.service; import org.pqh.dao.BaseDao; import org.pqh.model.AbstractModel; import org.pqh.util.DBAction; import org.springframework.dao.DataAccessException; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.stereotype.Service; import javax.annotation.Resource; /** * Created by reborn on 2017/7/28. */ -@Controller -public abstract class AbstractController { +@Service +public class BaseService { @Resource private BaseDao baseDao; - @RequestMapping("/") - public String index(){ - return "index"; - } - - public String curd(DBAction action,T formModel){ Class tClass= (Class) formModel.getClass(); try { diff --git a/src/main/webapp/WEB-INF/jsp/index.jsp b/src/main/webapp/WEB-INF/jsp/index.jsp index 7ec7e4e..dd1a45f 100644 --- a/src/main/webapp/WEB-INF/jsp/index.jsp +++ b/src/main/webapp/WEB-INF/jsp/index.jsp @@ -1,10 +1,10 @@ +<%@ page contentType="text/html;charset=UTF-8" language="java" %> - Title -

hello world

+

简单的网络爬虫客户端

\ No newline at end of file