学习spring3.0.x(以下简称spring 3)已经一段日子了,新特性也接触不少,比较感兴趣还是spring mvc这一块3.0的mvc变化太大了,跟2.5基本上是两个样子,至于详细的区别可以参考以下文章
spring3 mvc变化比较大,但是还是有些2.5的影子的,首先来个2.5也可以使用的注解版本的mvc 入门例子.例子采用maven2管理,所以必须要安装m2eclipse插件或者使用maven2进行管理.
本文基于eclipse3.5 Galileo-sr2Javaee 版本跟m2eclipse插件进行管理.
m2eclipse在线安装地址如下:
http://m2eclipse.sonatype.org/sites/m2e
如何安装请参考
使用eclipse创建一个maven project,选择webapp Javaee5.0的模板项目或者simpleproject,对于如何创建可以参考
[使用心得]maven2之m2eclipse使用手册之六使用Maven2插件创建一个简单的SSH2项目之jetty篇(一)
对于pom.xml如下:
<build> <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.24</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>utf-8</encoding> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.0.3.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.0.3.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <type>jar</type> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <type>jar</type> <scope>provided</scope> </dependency> <!-- Logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${org.slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>${org.slf4j.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${org.slf4j.version}</version> <scope>runtime</scope> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.15</version> <exclusions> <exclusion> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> </exclusion> <exclusion> <groupId>javax.jms</groupId> <artifactId>jms</artifactId> </exclusion> <exclusion> <groupId>com.sun.jdmk</groupId> <artifactId>jmxtools</artifactId> </exclusion> <exclusion> <groupId>com.sun.jmx</groupId> <artifactId>jmxri</artifactId> </exclusion> </exclusions> <scope>runtime</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.8.1</version> <type>jar</type> <scope>test</scope> </dependency> </dependencies> <properties> <org.slf4j.version>1.5.10</org.slf4j.version> </properties>
spring 的mvc本身就是一个servlet,所以在web.xml加入以下内容
<servlet> <servlet-name>StudySpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <description>springmvc.xml</description> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/*.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>StudySpringMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
其中contextConfigLocation是读取spring的配置文件进行解释,
springmvc.xml如下:
<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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 对spring org.lxh包下所有注解扫描 --> <context:component-scan base-package="org.lxh"></context:component-scan> <!-- 支持spring mvc新的注解类型 详细spring3.0手册 15.12.1 mvc:annotation-driven--> <mvc:annotation-driven /> <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀,在requestmapping输入的地址后自动调用该类进行视图解析--> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"></property> </bean> </beans>
UserController.Java类:
@Controller
public class UserController {
private Logger logger = org.slf4j.LoggerFactory
.getLogger(UserController.class);
@RequestMapping(value="/welcome.do",method=RequestMethod.GET)
public void welcome() {
logger.info("Welcome!");
System.out.println("helloworld");
}
}
@Controller:将一个类成为 Spring 容器的 Bean
@RequestMapping:指定该方法处理的 URL 请求,当在浏览器中输入该url地址的时候则会执行会执行指定的方法.
-----value:要运行的url地址
-----method:该方法在什么才执行.默认GET
运行jetty插件命令 jetty:run
看到如下图:
看到该信息后则证明spring的mvc已经正常执行了
输入以下地址:http://localhost:8080/StudySpringMVC/welcome.do
看到如下图的内容:
注意:调用该方法的时候会自动搜索bean id="viewResolver"的设置,然后查找该墓里下的jsp名字构成是所调用的方法名+.jsp,如果没有的话会出现后台NOT_FOUND的提示信息.