项目实录之集成Spring MVC
2015-07-28 00:00
531 查看
摘要: Spring MVC是Java web开发中的瑰宝,本文给出一个使用Spring MVC的demo,一起领略一下Spring MVC的魅力吧!
阅读本文之前建议先阅读如下文章
项目实录之Maven工程搭建
一、Spring上下文配置文件
1、分别在common、dao、service三个模块下New - Source Folder,命名为src/main/resources,在resources目录下新建文件夹context。
2、新增context-*.xml文件
①在bottae-common/src/main/resources/context目录下新建context-common.xml文件,添加如下内容:
②在bottae-dao/src/main/resources/context目录下新建context-dao.xml文件,添加如下内容:
③在bottae-service/src/main/resources/context目录下新建context-service.xml文件,添加如下内容:
④在bottae-web/src/main/resources/context目录下新建context-web.xml文件,添加如下内容:
3、web配置
在bottae-web的src/main/webapp/WEB-INF目录下新建dispatcher.xml文件,添加如下内容:
修改web.xml文件的配置,修改之后大致内容如下:
二、Spring MVC依赖配置
在父pom.xml中添加jetty插件,便于测试服务。添加如下配置:
添加Spring相关依赖,还有JDK依赖。
现在整个父pom.xml看起来是下面这个样子的:
三、新增视图文件夹
在web层的src/main/webapp/WEB-INF/下新建views文件夹。
四、测试工程
1、在common层新建类wang.wangci.bottae.common.Guid
2、在service层新建接口wang.wangci.bottae.service.UUIDservice
3、在service层新建UUIDService的实现类wang.wangci.bottae.service.impl.UUIDServiceImpl
5、在views目录下新建uuid.jsp
五、访问服务
命令行进入bottae目录执行下面两个命令:
服务起来之后:
访问http://localhost:7777/uuid/page ,如果得到了JSP页面的内容,并且${uuid}被uuid内容给替换了,说明成功了。
访问http://localhost:7777/uuid/get ,如果得到了一串uuid,说明成功了。
本文源码:
360云盘下载 访问密码 bae6
进入我的个人网站查看原文
阅读本文之前建议先阅读如下文章
项目实录之Maven工程搭建
一、Spring上下文配置文件
1、分别在common、dao、service三个模块下New - Source Folder,命名为src/main/resources,在resources目录下新建文件夹context。
2、新增context-*.xml文件
①在bottae-common/src/main/resources/context目录下新建context-common.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> </beans>
②在bottae-dao/src/main/resources/context目录下新建context-dao.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config /> </beans>
③在bottae-service/src/main/resources/context目录下新建context-service.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:p="http://www.springframework.org/schema/p" 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.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="wang.wangci.bottae.service" /> </beans>
④在bottae-web/src/main/resources/context目录下新建context-web.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:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="wang.wangci.bottae.web" /> </beans>
3、web配置
在bottae-web的src/main/webapp/WEB-INF目录下新建dispatcher.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <mvc:annotation-driven /> <!-- 注解探测器 --> <context:component-scan base-package="wang.wangci.bottae"> </context:component-scan> <!-- annotation默认的方法映射适配器 --> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 对静态资源文件的访问 --> <mvc:default-servlet-handler /> <!-- 视图文件解析配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> </beans>
修改web.xml文件的配置,修改之后大致内容如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" metadata-complete="false" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <!-- 加载Spring容器配置 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置Spring容器加载配置文件路径 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:context/context-*.xml</param-value> </context-param> <!-- 配置Spring核心控制器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <async-supported>true</async-supported> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
二、Spring MVC依赖配置
在父pom.xml中添加jetty插件,便于测试服务。添加如下配置:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.11.v20130520</version> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>7777</port> </connector> </connectors> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
添加Spring相关依赖,还有JDK依赖。
现在整个父pom.xml看起来是下面这个样子的:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>wang.wangci</groupId>
<artifactId>bottae-parent</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>bottae-parent</name>
<url>http://maven.apache.org</url>
<modules>
<module>bottae-common</module>
<module>bottae-dao</module>
<module>bottae-service</module>
<module>bottae-web</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>3.2.3.RELEASE</spring.version>
</properties>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.11.v20130520</version> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>7777</port> </connector> </connectors> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
<dependencies>
<!-- spring start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring 依賴jackson -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.7</version>
</dependency>
<!-- spring end -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
三、新增视图文件夹
在web层的src/main/webapp/WEB-INF/下新建views文件夹。
四、测试工程
1、在common层新建类wang.wangci.bottae.common.Guid
package wang.wangci.bottae.common; import java.security.SecureRandom; import java.util.UUID; /** * Project Name:bottae-common * File Name:Guid.java * Package Name:wang.wangci.bottae.common * Author:huangjia * Date:2015年7月28日下午5:35:11 * */ public class Guid { private static SecureRandom random = new SecureRandom(); /** * UUID, 通过Random数字生成, 中间无-分割. */ public static String g() { return g2().replaceAll("-", ""); } /** * UUID, 通过Random数字生成, 中间有-分割. */ public static String g2() { return UUID.randomUUID().toString().toUpperCase(); } /** * 使用SecureRandom随机生成Long. */ public static long randomLong() { return Math.abs(random.nextLong()); } }
2、在service层新建接口wang.wangci.bottae.service.UUIDservice
package wang.wangci.bottae.service; /** * Project Name:bottae-service * File Name:UUIDService.java * Package Name:wang.wangci.bottae.service * Author:huangjia * Date:2015年7月28日下午5:38:36 * */ public interface UUIDService { public String uuid(); }
3、在service层新建UUIDService的实现类wang.wangci.bottae.service.impl.UUIDServiceImpl
package wang.wangci.bottae.service.impl; import org.springframework.stereotype.Component; import wang.wangci.bottae.common.Guid; import wang.wangci.bottae.service.UUIDService; /** * Project Name:bottae-service * File Name:UUIDService.java * Package Name:wang.wangci.bottae.service.impl * Author:huangjia * Date:2015年7月28日下午5:40:37 * */ @Component public class UUIDServiceImpl implements UUIDService{ public String uuid() { return Guid.g(); } } 4、在web层新建wang.wangci.bottae.web.controllers.UUIDController类 package wang.wangci.bottae.web.controllers; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import wang.wangci.bottae.service.UUIDService; /** * Project Name:bottae-web * File Name:UUIDController.java * Package Name:wang.wangci.bottae.web.controllers * Author:huangjia * Date:2015年7月28日下午5:42:44 * */ @Controller @RequestMapping("/uuid") public class UUIDController { @Resource private UUIDService uuidService; @RequestMapping("/get") @ResponseBody public String get(){ return uuidService.uuid(); } @RequestMapping("/page") public String page(ModelMap model){ model.addAttribute("uuid", uuidService.uuid()); return "uuid"; } }
5、在views目录下新建uuid.jsp
<%@ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <title>UUID</title> </head> <body> 恭喜你,成功得到了該頁面。你拿到的UUID是: <b>${uuid}</b> </body> </html>
五、访问服务
命令行进入bottae目录执行下面两个命令:
mvn clean install cd bottae-web mvn jetty:run
服务起来之后:
访问http://localhost:7777/uuid/page ,如果得到了JSP页面的内容,并且${uuid}被uuid内容给替换了,说明成功了。
访问http://localhost:7777/uuid/get ,如果得到了一串uuid,说明成功了。
本文源码:
360云盘下载 访问密码 bae6
进入我的个人网站查看原文
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- 一个jar包里的网站