Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(四)Spring 3.1.0 整合
来源:互联网 发布:java入门视频百度网盘 编辑:程序博客网 时间:2024/04/30 10:08
1 Spring下载与安装
在第一篇《Spring 3.x+Spring MVC 3.x+MyBatis 3.x 整合(一)Spring MVC 环境搭建》中已经加入了spring的所有jar包,这里不再说明。
2 修改Web配置文件
为web.xml添加如下配置:
<!-- Spring 配置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value><!-- 可以把配置文件放在WEB-INF下,然后使用如下配置<param-value>/WEB-INF/applicationContext.xml</param-value> --></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
完整的web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><display-name></display-name><context-param><param-name>webAppRootKey</param-name><param-value>whowii.website2</param-value></context-param><!-- log4j 配置 --><context-param><param-name>log4jConfigLocation</param-name><param-value>WEB-INF/log4j.properties</param-value></context-param><context-param><param-name>log4jRefreshInterval</param-name><param-value>60000</param-value></context-param><listener><listener-class>org.springframework.web.util.Log4jConfigListener</listener-class></listener><!-- Spring 配置 --><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value><!-- 可以把配置文件放在WEB-INF下,然后使用如下配置<param-value>/WEB-INF/applicationContext.xml</param-value> --></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- spring mvc 配置--><servlet><!-- 该名称将影响 xxx-servlet.xml 配置文件名称 --><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><!-- load-on-startup:表示启动容器时初始化该Servlet --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><!-- url-pattern:表示哪些请求交给Spring Web MVC处理, "/"是用来定义默认servlet映射的。也可以如"*.html"表示拦截所有以html为扩展名的请求。 --><url-pattern>*.do</url-pattern></servlet-mapping><!-- 默认首页配置 --><welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list></web-app>
3 增加spring配置文件
在src目录下创建applicationContext.xml配置文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 自动扫描业务包 --> <context:component-scan base-package="com.whowii.core.service.impl" /></beans>
4 修改java类
本测试代码最终达到的目标是,为Action注入Service实例。
A.新增服务接口:com.whowii.core.service.UserService,内容如下:
package com.whowii.core.service;public interface UserService {public String getMsg();}
package com.whowii.core.service.impl;import org.springframework.stereotype.Service;import com.whowii.core.service.UserService;@Service("userService")public class UserServiceImpl implements UserService {public String getMsg() {return "UserServiceImpl";}}注意Service注解。
C.修改EnterController类,增加类型为UserService的属性userService,最终的代码如下:
package com.whowii.core.action;import javax.annotation.Resource;import org.apache.log4j.Logger;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import com.whowii.core.service.UserService;@Controller@RequestMapping(value = "/manage/enter")public class EnterController {private Logger logger = Logger.getLogger(getClass());private UserService userService;@Resource(name = "userService")public void setUserService(UserService userService) {this.userService = userService;}// 访问URL:website_java2/enter/index.do@RequestMapping(value = "/index", method = RequestMethod.GET)public String index(String id, Model model) {logger.info("访问了index方法,id=" + id);model.addAttribute("title", "系统登录");model.addAttribute("date", this.userService.getMsg());return "manage/enter/index";}}这里通过Resource注解,注入UserService实例。
4 测试访问
启动tomcat,打开浏览器访问:http://localhost:8180/website_java2/manage/enter/index.do
打开的页面应该是这样的:
2 1
- Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(四)Spring 3.1.0 整合
- Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(一)Spring MVC 3.1.0 环境搭建
- spring 4.x + mybatis 3.x 整合
- Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(三)Spring MVC 参数传递
- Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(二)整合log4j日志工具
- Spring 4.x+Spring MVC 4.x+MyBatis 4.x 整合(五)MyBatis 3.2.5 整合
- mybatis 3.2.7 与 spring mvc 3.x、logback整合
- Spring 4.X整合Quartz2.X配置
- spring 3.x整合ehcache 2.x
- Mybatis入门实例(四)——MyBatis与Spring 3.X的整合
- Spring与Struts1.x整合
- Dwr3.x spring整合配置
- struts 1.x spring 整合
- spring与struts1.x整合
- struts1.x和spring整合
- spring boot spring cache ehcache3.x整合
- Spring MVC 4.x 知识点
- Quartz2.x学习笔记(三):Quartz2.x整合spring
- day28_struts2(全天)のPPT学习——05 Action接受请求参数.ppt
- 设计模式之迭代器模式
- IoCompleteRequest的实现逻辑
- day28_struts2(全天)のPPT学习——06 使用Struts2的输入校验
- hadoop-2.X启动后用JPS命令查看,resourcemanger无法启动:地址已被使用
- Spring 4.x+Spring MVC 4.x+MyBatis 3.x 整合(四)Spring 3.1.0 整合
- HDU5183Negative and Positive (NP)(哈希表)
- POJ2965 The Pilots Brothers' refrigerator(枚举+思维题)
- 地址宏定义
- 通过jni实现c++和java互调
- LightOJ 1422 - Halloween Costumes(区间DP)
- The container 'Android Dependencies' references non existing library
- 莀莂蚈肃腿莄螄蝿聿莇袀羈
- 羆膁葿蚁羅芄芁薇肄羃蒇蒃