Struts 整合 SpringMVC

Struts 整合 SpringMVC 过程:这篇文章是我在整合过程中所做的记录和笔记

web.xml :筛选器机制过滤

  • 原机制是拦截了所有 url ,即 <url-pattern>/*</url-pattern>

  • 新机制为了将 structs2 的 url 与 SpringMVC 的 url 区分开来,则修改了拦截属性

<!-- 原代码 --><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern><dispatcher>REQUEST</dispatcher>  <dispatcher>FORWARD</dispatcher> </filter-mapping><!-- 新代码 --><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.action</url-pattern><dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping><filter-mapping><filter-name>struts2</filter-name><url-pattern>*.jsp</url-pattern><dispatcher>REQUEST</dispatcher>  <dispatcher>FORWARD</dispatcher> </filter-mapping>

web.xml struts 整合 SpringMVC

<!-- SpringMVC 配置开始 --><!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContextinstead of the default XmlWebApplicationContext --><context-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></context-param><!-- Configuration locations must consist of one or more comma- or space-delimitedfully-qualified @Configuration classes. Fully-qualified packages may also bespecified for component-scanning --><context-param><param-name>contextConfigLocation</param-name><param-value>spring.config.AppConfig</param-value></context-param><!-- Bootstrap the root application context as usual using ContextLoaderListener --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- Declare a Spring MVC DispatcherServlet as usual --><servlet><servlet-name>dispatcher</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContextinstead of the default XmlWebApplicationContext --><init-param><param-name>contextClass</param-name><param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value></init-param><!-- Again, config locations must consist of one or more comma- or space-delimitedand fully-qualified @Configuration classes --><init-param><param-name>contextConfigLocation</param-name><param-value>spring.config.MvcConfig</param-value></init-param></servlet><!-- map all requests for /km/* to the dispatcher servlet --><servlet-mapping><servlet-name>dispatcher</servlet-name><url-pattern>/km/*</url-pattern></servlet-mapping><!-- SpringMVC 配置结束 -->
  • 基于web.xml配置文件的配置属性,需要配置两个Config类:【两个配置的区别】

    • AppConfig.java

    @Configuration
    @Import({KmAppConfig.class})
    public class AppConfig {}
    • MvcConfig.java

    @Configuration
    @Import({KmMvcConfig.class})
    public class MvcConfig {}
  • 基于Config类,配置具体的应用Config

    • KmAppConfig.java

    @Configuration
    @ComponentScan(basePackages = "com.teemlink.km.") //扫描包体
    public class KmAppConfig implements TransactionManagementConfigurer,ApplicationContextAware {
    private static ApplicationContext applicationContext;@Autowired
    private DataSource dataSource;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {KmAppConfig.applicationContext = applicationContext;
    }
    public static ApplicationContext getApplicationContext() {return applicationContext;
    }@Bean
    public DataSource dataSource() {//如何读取配置资源的数据?String url = "jdbc:jtds:sqlserver://192.168.80.47:1433/nj_km_dev";String username = "sa";String password = "teemlink";DriverManagerDataSource ds = new DriverManagerDataSource(url, username, password);ds.setDriverClassName("net.sourceforge.jtds.jdbc.Driver");return ds;
    }@Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){return new JdbcTemplate(dataSource);}
    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {return new DataSourceTransactionManager(dataSource);
    }
    }
    • KmMvcConfig.java

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.teemlink.km.**.controller")
    public class KmMvcConfig extends WebMvcConfigurerAdapter {@Overridepublic void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {configurer.enable();}
    }

基于SpringMVC 的 Controller - Service - Dao 框架

  • AbstractBaseController

    /*** 抽象的RESTful控制器基类* @author Happy**/
    @RestController
    public abstract class AbstractBaseController {@Autowired  protected HttpServletRequest request;@Autowiredprotected HttpSession session;protected Resource success(String errmsg, Object data) {return new Resource(0, errmsg, data, null);}protected Resource error(int errcode, String errmsg, Collection<Object> errors) {return new Resource(errcode, errmsg, null, errors);}private Resource resourceValue;public Resource getResourceValue() {return resourceValue;}public void setResourceValue(Resource resourceValue) {this.resourceValue = resourceValue;}/*** 资源未找到的异常,返回404的状态,且返回错误信息。* @param e* @return*/@ExceptionHandler(RuntimeException.class)@ResponseStatus(HttpStatus.NOT_FOUND)public Resource resourceNotFound(RuntimeException e) {return error(404, "Not Found", null);}/*** 运行时异常,服务器错误,返回500状态,返回服务器错误信息。* @param e* @return*/@ExceptionHandler(RuntimeException.class)@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)public Resource error(RuntimeException e) {return error(500, "Server Error", null);}/*** Restful 接口返回的资源对象* */@JsonInclude(Include.NON_EMPTY)public class Resource implements Serializable {private static final long serialVersionUID = 2315158311944949185L;private int errcode;private String errmsg;private Object data;private Collection<Object> errors;public Resource() {}public Resource(int errcode, String errmsg, Object data, Collection<Object> errors) {this.errcode = errcode;this.errmsg = errmsg;this.data = data;this.errors = errors;}public int getErrcode() {return errcode;}public void setErrcode(int errcode) {this.errcode = errcode;}public String getErrmsg() {return errmsg;}public void setErrmsg(String errmsg) {this.errmsg = errmsg;}public Object getData() {return data;}public void setData(Object data) {this.data = data;}public Collection<Object> getErrors() {return errors;}public void setErrors(Collection<Object> errors) {this.errors = errors;}}
    }
  • IService

    /**** @param <E>*/
    public interface IService<E> {/*** 创建实例* @param entity* @return* @throws Exception*/public IEntity create(IEntity entity) throws Exception;/*** 更新实例* @param entity* @return* @throws Exception*/public IEntity update(IEntity entity) throws Exception;/*** 根据主键获取实例* @param pk* @return* @throws Exception*/public IEntity find(String pk) throws Exception;/*** 删除实例* @param pk* @throws Exception*/public void delete(String pk) throws Exception;/*** 批量删除实例* @param pk* @throws Exception*/public void delete(String[] pk) throws Exception;}
  • AbstractBaseService

    /*** 抽象的业务基类**/
    public abstract class AbstractBaseService {/*** @return the dao*/public abstract IDAO getDao();  @Transactionalpublic IEntity create(IEntity entity) throws Exception {if(StringUtils.isBlank(entity.getId())){entity.setId(UUID.randomUUID().toString());}return getDao().create(entity);}@Transactionalpublic IEntity update(IEntity entity) throws Exception {return getDao().update(entity);}public IEntity find(String pk) throws Exception {return getDao().find(pk);}@Transactionalpublic void delete(String pk) throws Exception {getDao().remove(pk);}@Transactionalpublic void delete(String[] pks) throws Exception {for (int i = 0; i < pks.length; i++) {getDao().remove(pks[i]);}}
    }
  • IDAO

    /****/
    public interface IDAO {public IEntity create(IEntity entity) throws Exception;public void remove(String pk) throws Exception;public IEntity update(IEntity entity) throws Exception;public IEntity find(String id) throws Exception;}
  • AbstractJdbcBaseDAO

    /*** 基于JDBC方式的DAO抽象实现,依赖Spring的JdbcTemplate和事务管理支持**/
    public abstract class AbstractJdbcBaseDAO {@Autowiredpublic JdbcTemplate jdbcTemplate;protected String tableName;public JdbcTemplate getJdbcTemplate() {return jdbcTemplate;}public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;}/*** 构建分页sql* @param sql* @param page* @param lines* @param orderbyFile* @param orderbyMode* @return* @throws SQLException*/protected abstract String buildLimitString(String sql, int page, int lines,String orderbyFile, String orderbyMode) throws SQLException ;/*** 获取数据库Schema* @return*/
    //    protected abstract String getSchema();/*** 获取表名* @return*/protected String getTableName(){return this.tableName;}/*** 获取完整表名* @return*/public String getFullTableName() {return getTableName().toUpperCase();}}

测试框架

  • 基本测试框架

     /*** 单元测试基类,基于Spring提供bean组件的自动扫描装配和事务支持**/
    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = KmAppConfig.class)
    @Transactional
    public class BaseJunit4SpringRunnerTest {}  
  • 具体实现:(以Disk为例)

    public class DiskServiceTest extends BaseJunit4SpringRunnerTest {@AutowiredDiskService service;@Beforepublic void setUp() throws Exception {}@Afterpublic void tearDown() throws Exception {}@Testpublic void testFind() throws Exception{Disk disk = (Disk) service.find("1");Assert.assertNotNull(disk);}@Test@Commitpublic void testCreate() throws Exception{Disk disk = new Disk();disk.setName("abc");disk.setType(1);disk.setOrderNo(0);disk.setOwnerId("123123");service.create(disk);Assert.assertNotNull(disk.getId());}
    }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/281314.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue保持用户登录及权限控制

vue-router-power-demo 核心内容有两点&#xff1a; 一是保持用户登录状态&#xff0c;二是根据登录用户的角色动态挂在路由 使用vuex保持用户登录 点击登录按钮&#xff0c;使用vuex的actions分发登录操作&#xff0c;发送用户名和密码到后台获取登录token&#xff0c; 并存…

java B2B2C Springcloud多租户电子商城系统-Spring Cloud Sleuth

在微服务框架中&#xff0c;一个由客户端发起的请求在后端系统中会经过多个不同的的服务节点调用来协同产生最后的请求结果&#xff0c;每一个前段请求都会形成一条复杂的分布式服务调用链路&#xff0c;链路中的任何一环出现高延时或错误都会引起整个请求最后的失败。 愿意了解…

C#性能测试BenchmarkDotnet

1.简介在我们开发高性能代码时&#xff0c;需要各种针对性能优化进行编码。那么如何才能知道我们所加的代码是否有性能方面的正向优化呢&#xff1f;有了BenchmarkDotNet&#xff0c;做性能对比测试就非常容易了&#xff0c;只需要把你的测试方法加上特性[Benchmark], 想做不同…

Requests获取连接的IP地址

在接口自动化的时候&#xff0c;需要获取到连接的本地IP地址&#xff0c;方法如下 1 import requests 2 3 rsp requests.get("http://www.baidu.com", streamTrue) 4 print rsp.raw._connection.sock.getpeername()[0] 5 print rsp.raw._connection.sock.getsockna…

阿里云APP(V4.3) SSH远程登录功能设置操作指南

阿里云APP V4.3 发布了&#xff0c;这次的升级&#xff0c;不仅在iOS和android平台上支持SSH远程登录ECS功能&#xff0c;也支持密钥登录哦~~~ SSH远程登录&#xff0c;这是一个连阿里巴巴自己的技术人员都开心不已的功能&#xff01; 各位攻城狮们&#xff0c;从更新到V4.3的那…

JS专题之节流函数

本文共 2000 字&#xff0c;读完只需 8 分钟上一篇文章讲了去抖函数&#xff0c;然后这一篇讲同样为了优化性能&#xff0c;降低事件处理频率的节流函数。 一、什么是节流&#xff1f; 节流函数&#xff08;throttle&#xff09;就是让事件处理函数&#xff08;handler&#xf…

vue 2.6 插槽v-slot用法记录

v-slot用法简记用法示例匿名插槽与具名插槽插槽作用域组件使用插槽动态命名总结用法示例 vue2.6统一了插槽的语法v-slot 匿名插槽与具名插槽 在其他组件中使用child组件 <child><template v-slot:slotName>hello world</template> </child>child组…

Latex排版全解(转)

Latex排版全解 http://blog.csdn.net/langb2014/article/details/51354238转载于:https://www.cnblogs.com/yifdu25/p/8338399.html

git-ftp Can't access remote 'ft://...', exiting...问题记录

环境 服务器&#xff1a;西部数码虚拟主机 本地系统&#xff1a;windows 10 (LTSC 2019) 软件&#xff1a; Git Bash&#xff0c;gti-ftp (版本1.6.0) 问题 在使用git ftp init初始化上传代码的时候会出现 $ git ftp init fatal: Cant access remote ftp://dmkt:***dmkt.goto…

【Flutter教程】从零构建电商应用(一)

在这个系列中&#xff0c;我们将学习如何使用google的移动开发框架flutter创建一个电商应用。本文是flutter框架系列教程的第一部分&#xff0c;将学习如何安装Flutter开发环境并创建第一个Flutter应用&#xff0c;并学习Flutter应用开发中的核心概念&#xff0c;例如widget、状…

为OWA自定义快捷键

这篇短文分享一下如何为自己常用的网页添加自定义功能&#xff0c;例如添加快捷键。我这里用一个常用的网站作为范例。它是Outlook Web Access (OWA), 它的地址一般如下。我在写邮件时希望能用一些快捷键来提高工作效率&#xff0c;但系统默认自带的快捷键特别少&#xff0c;而…

数据结构 快速排序

快速排序是对冒泡排序的一种改进&#xff0c;是所有内部排序算法中平均性能最优的排序算法。其基本思想是基于分治法的&#xff1a;在待排序数组L[1...n]中任取一个元素pivot作为基准&#xff0c;从数组的两端开始扫描。设两个指示标志&#xff08;low指向起始位置&#xff0c;…

Finally语句块的运行

一、finally语句块是否一定运行&#xff1f; Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被运行&#xff1f;非常多人都说不是。当然他们的回答是正确的&#xff0c;经过试验。至少下面有两种情况下finally语句是不会被运行的&#xff1a; &#xf…

vue-cli 3.0 跨域请求代理

官方文档中指明&#xff0c;跨域请求可以通过配置vue.config.js中的devServer.proxy选项来进行配置。 这个选项配置的本质实际上就是http-proxy-middleware中间件的用法&#xff0c;和Webpack-dev-server的proxy一样。 vue-cli 3.0中介绍了两种常见的用法&#xff1a; modul…

小米人员架构调整:组建中国区,王川任总裁

12月13日上午&#xff0c;小米内部发布人员调整公开信&#xff0c;信中传达了两个重要内容&#xff1a;将销售与服务部改组为中国区&#xff0c;任命集团高级副总裁王川兼任中国区总裁。 在今年9月份&#xff0c;也就是小米上市前夕&#xff0c;雷军在一封内部信中宣布对公司组…

在 .NET 7上使用 WASM 和 WASI

WebAssembly&#xff08;WASM&#xff09;和WebAssembly System Interface&#xff08;WASI&#xff09;为开发人员开辟了新的世界。.NET 开发人员在 Blazor WebAssembly 发布时熟悉了 WASM。Blazor WebAssembly 在浏览器中基于 WebAssembly 的 .NET 运行时上运行客户端。WASI通…

Java基础 五 方法

方法 1.1 方法概述 在我们的日常生活中&#xff0c;方法可以理解为要做某件事情&#xff0c;而采取的解决办法。 如&#xff1a;小明同学在路边准备坐车来学校学习。这就面临着一件事情&#xff08;坐车到学校这件事情&#xff09;需要解决&#xff0c;解决办法呢&#xf…

django rest framework 过滤 lim分页

一.过滤 1.首先引用diango 自带的过滤配置 2.导入模块 from django_filters.rest_framework import DjangoFilterBackend from django_filters import rest_framework as filters 3.一种简单的过滤: class BookView(ModelViewSet):queryset Book.objects.all()serializer_clas…

MySQL用户及权限管理

MySQL用户及权限管理查看用户及权限查看用户及作用域&#xff08;使用范围&#xff09;查看用户权限创建用户及授权字段参数用户管理使用命令提示符登录MySQL mysql -h localhost -u root -p查看用户及权限 mysql中的用户信息和权限等都存储在一个名为mysql的数据库中。其中主…

附近有什么?8款可以查周边的App

如今科技发达的时代&#xff0c;手机的功能不仅仅只是能通讯聊天&#xff0c;而是逐渐的走进了人们的生活中。因为有了APP&#xff0c;我们的生活才更丰富&#xff0c;并且有很多是我们生活中不可缺少的软件&#xff0c;而这些软件便是根据手机中的GPS定位系统而来的。简单来说…