标签:lan cat mode value es2017 port demo 引用 int
首先创建一个maven项目,然后依次完成如下配置:
在pom.xml加入如下基础配置,利用maven加载我们所需要的jar:
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.imooc</groupId> 5 <artifactId>springmvc</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>springmvc Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 11 <properties> 12 <!-- 声明各个依赖包的版本号,在下面描写依赖的时候则不需要再声明 --> 13 <commons-lang.version>2.6</commons-lang.version> 14 <slf4j.version>1.7.6</slf4j.version> 15 <spring.version>4.1.3.RELEASE</spring.version> 16 </properties> 17 18 <!-- 依赖管理,用来管理jar之间的依赖关系 --> 19 <dependencyManagement> 20 <dependencies> 21 <dependency> 22 <groupId>org.springframework</groupId> 23 <artifactId>spring-framework-bom</artifactId> 24 <version>${spring.version}</version> 25 <type>pom</type> 26 <scope>import</scope> 27 </dependency> 28 </dependencies> 29 </dependencyManagement> 30 31 <dependencies> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-webmvc</artifactId> 35 <!-- 由于上面已经声明版本号,这里就不需要再声明 --> 36 </dependency> 37 38 <dependency> 39 <groupId>commons-lang</groupId> 40 <artifactId>commons-lang</artifactId> 41 <!-- 这里直接引用上面声明的版本号即可 --> 42 <version>${commons-lang.version}</version> 43 </dependency> 44 45 <!-- 有关日志的包 --> 46 <dependency> 47 <groupId>org.slf4j</groupId> 48 <artifactId>slf4j-log4j12</artifactId> 49 <version>${slf4j.version}</version> 50 <exclusions> 51 <exclusion> 52 <artifactId>slf4j-api</artifactId> 53 <groupId>org.slf4j</groupId> 54 </exclusion> 55 </exclusions> 56 </dependency> 57 58 <dependency> 59 <groupId>javax.servlet</groupId> 60 <artifactId>javax.servlet-api</artifactId> 61 <version>3.0.1</version> 62 </dependency> 63 64 </dependencies> 65 66 <build> 67 <!-- 插件 --> 68 <plugins> 69 <plugin> 70 <groupId>org.eclipse.jetty</groupId> 71 <artifactId>jetty-maven-plugin</artifactId> 72 <version>9.2.2.v20140723</version> 73 <!-- <groupId>org.apache.tomcat.maven</groupId> 74 <artifactId>tomcat7-maven-plugin</artifactId> 75 <version>2.2</version> 76 <configuration> 77 <url>http://localhost:8080/webapp/index.jsp</url> 78 </configuration> --> 79 </plugin> 80 </plugins> 81 </build> 82 </project>
在web.xml加入如下基础配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 7 <display-name>Spring MVC Study</display-name> 8 <!-- Spring应用上下文,理解层次化的ApplicationContext --> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <!-- Spring自己的配置文件 --> 12 <param-value>/WEB-INF/configs/spring/applicationContext*.xml</param-value> 13 </context-param> 14 15 <listener> 16 <listener-class> 17 org.springframework.web.context.ContextLoaderListener 18 </listener-class> 19 </listener> 20 21 <!-- 利用下面的servlet和servlet-mapping标签将前端控制器配置到web中, --> 22 <servlet> 23 <servlet-name>mvc-dispatcher</servlet-name> 24 <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class> 25 <!-- DispatcherServlet对应的上下文配置, 默认为/WEB-INF/$servlet-name$-servlet.xml 26 如果没有init-param,则会默认加载上述的默认配置文件--> 27 <init-param> 28 <param-name>contextConfigLocation</param-name> 29 <!-- 在/WEB-INF/相应目录下创建如下的xml文件 --> 30 <param-value>/WEB-INF/configs/spring/mvc-dispatcher-servlet.xml</param-value> 31 </init-param> 32 <load-on-startup>1</load-on-startup> 33 </servlet> 34 <servlet-mapping> 35 <!-- 这里的servlet-name与上面的servlet标签里的servlet-name保持一致 --> 36 <servlet-name>mvc-dispatcher</servlet-name> 37 <!-- mvc-dispatcher拦截所有的请求--> 38 <!-- 这里url-pattern指定了不同url来源的依据 --> 39 <url-pattern>/</url-pattern> 40 </servlet-mapping> 41 </web-app>
创建mvc-dispatcher-servlet.xml,加入如下基础配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 <!-- 激活@Required @Autowired @PostConstruct @PreDestroy @Resource等注解, 14 使得在类中可以利用注解来实例化相应对象 --> 15 <context:annotation-config /> 16 17 <!-- DispatcherServlet上下文,只搜索@Controller标注的类,不搜索其他标注的类 --> 18 <context:component-scan base-package="com.imooc.mvcdemo"> 19 <context:include-filter type="annotation" 20 expression="org.springframework.stereotype.Controller" /> 21 </context:component-scan> 22 23 <!-- 让DispatcherServlet启用基于annotation的HandlerMapping --> 24 <mvc:annotation-driven /> 25 26 27 <!-- 配置ViewResolver,告诉servlet将由哪个ViewResolver获取view,这里使用JstlView --> 28 <bean 29 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 30 <property name="viewClass" 31 value="org.springframework.web.servlet.view.JstlView" /> 32 <!-- 配置其前缀prefix,表示jsp文件在/WEB-INF/jsps/目录下,配置其后缀suffix,表示其文件类型是.jsp --> 33 <property name="prefix" value="/WEB-INF/jsps/" /> 34 <property name="suffix" value=".jsp" /> 35 </bean> 36 37 38 </beans>
创建applicationContext.xml,加入如下基础配置:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/mvc 11 http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 12 13 <context:annotation-config /> 14 15 <!-- 这里不需要管理Controller --> 16 <context:component-scan base-package="com.imooc.mvcdemo"> 17 <context:exclude-filter type="annotation" 18 expression="org.springframework.stereotype.Controller" /> 19 </context:component-scan> 20 </beans>
至此,配置文件加载完毕,加入前端控制器:
1 package com.imooc.mvcdemo.controller; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 6 @Controller 7 @RequestMapping("/hello") 8 public class HelloMvcController { 9 10 @RequestMapping("/mvc") 11 //这里经过@RequestMapping配置之后可以由浏览器访问localhost:8080/hello/mvc 12 public String helloMvc() { 13 //返回到home.jsp 14 return "home"; 15 } 16 }
全部做完之后,运行jetty:run,在浏览器中输入相应url即可得到结果。
图解:
webApplicationContext可以有多个,这也就是我们的context, listener所加载的一个上下文信息,它为我们提供了我们所有应用公共所使用的一些组件和服务,也就是service和dao层等,这些服务应当是被整个应用所共享的,它不应该被局限在某一个DispatcherServlet上下文中。
与特定DispatcherServlet相关的上下文,也可以有多个DispatcherServlet。这些不同的DispatcherServlet可以用来分发不同的请求。
标签:lan cat mode value es2017 port demo 引用 int
原文地址:http://www.cnblogs.com/cing/p/7490646.html