本文概览:搭建一个spring mvc工程,支持jsp模板和json。
1 配置文件
项目概览如下
1.1 pom.xml
1、配置packaging
1 |
<packaging>war</packaging> |
2、配置WEB依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<!-- web begin --> <!--配置servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- JSON,为了使用@ResponseBody --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-all</artifactId> <version>1.9.5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>3.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> </dependency> <!-- web end --> |
3、配置IOC和AOP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
<!--配置IOC beg--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <artifactId>spring-test</artifactId> <groupId>org.springframework</groupId> <version>${org.springframework.version}</version> <scope>test</scope> </dependency> <!--配置 Spring-IOC end--> <!--配置 Spring-transaction start--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${org.springframework.version}</version> </dependency> <!--配置 Spring-transaction end--> <!--配置 Spring-AOP start--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${org.springframework.version}</version> </dependency> <!--配置 Spring-AOP end --> |
1.2 web.xml
配置如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <!--自动加载spring 容器 beg--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>tg <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--自动加载spring 容器 end--> <!-- 定义mvc的分发请求器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.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> <!--定义mvc的分发请求器 end--> </web-app> |
1.3 springmvc-servlet.xml
只需要配置视图解析器,就可以了,不需要配置视图。因为每一种视图解析都对应一个视图解析。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 定义jsp视图 beg--> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/template/"/> <property name="suffix" value=".jsp"></property> </bean> <!-- 定义jsp视图 beg--> </beans> |
1.4 applicationContext
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 加载properties文件 --> <context:property-placeholder location="classpath:config.properties"/> <context:component-scan base-package="web" /> <mvc:annotation-driven/> <!-- 使用@Aspect定义切面 beg --> <aop:aspectj-autoproxy/> <!-- 使用@Aspect定义切面 beg --> <!-- 使用注解@Transactional beg --> <tx:annotation-driven/> <!-- 使用注解@Transactional end --> <!-- 加载dao层相关文件 beg --> <!-- 加载dao层相关文件 end --> </beans> |
如下两个配置必须要在同一个配置文件中
1 2 |
<mvc:annotation-driven /> <context:component-scan base-package="com.companyname.workgroup.apply.module.contorller"/> |
2 代码
一个controller的代码如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@Controller public class HelloController { /** * 测试一个velocity * * @return */ @RequestMapping(value = "/helloJsp") public String testVelocity() { return "helloJsp"; } /** * 测试一个json * * @return */ @RequestMapping(value = "/helloJson") @ResponseBody public DemoVo testJson() { DemoVo demo = new DemoVo(); demo.setName("hi"); demo.setStore("23823999"); return demo; } } |
3 IDEA配置tomcat的问题
1、建立Artifacts。 通过“ File -> Project Structure ” ,如下图
自定义name如下:
2、新建tomcat配置
(1)指定tomcat安装目录
(2)指定Artifate
4、启动之后,输入 http://localhost:8080/helloJson ,返回如下结果
1 2 3 4 |
{ "name": "hi", "store": "23823999" } |
(全文完)