在上一篇中,我们详细介绍了SpringMVC的一些基本概念。接下来,我们开始讲解怎么搭建SpringMVC的环境。
常用的搭建SpringMVC的办法有两个。一个是自己配置,一个是借助maven。Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具。
这一篇,我们先讲解自己手动配置的方法。关于使用Maven搭建的方法,在下一篇再做进一步的讲解。
手动配置SpringMVC,又可以细分为两个小方法。这两种方式的区别在于一个使用了注解,即SpringMVC 2.5 版本以后。另一种没有使用注解。本篇中,将分别演示和讲解这两种方式。
第一种: 不使用注解的原始方式。
按照我们一贯的风格,先列举几个可能会遇到的问题和需要注意的点。
a. SpringMVC需要一部分依赖包,请严格按照下面列举和说明的方法,引入所有需要引入的包(下面会有详细说明),不要漏掉。
b. SpringMVC的配置中,有的地方需要引入命名空间,也请严格按照下面的说明操作,不要擅自删除一些以为没用的引用。
c. SpringMVC包的下载,可能已经被屏蔽,国内,你懂得。下面会提供几个可以下载的方法,和已经下载好的SpringMVC包供大家学习使用。如果,你可以翻墙,那没什么问题,自己下就好了,这里提供一个简单的方法(直接贴到地址栏 http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.4.RELEASE/spring-framework-3.2.4.RELEASE-dist.zip 版本号可以改成自己需要的)。如果你不可以翻墙,附一个下载(http://pan.baidu.com/s/1gdEY5pD 密码:sna1)。
下面进入正式操作中。
1.创建一个Web 工程,然后 导入必要的SpringMVC包。以防万一,请导入spring中所有的jar包。除此以外,本文在百度云提供的下载包中还有其他几个重要的依赖包。commons-logging-1.1.1下的所有jar包也导入工程中,standard-1.1.2.jar和jstl-1.1.2.jar也要导入。当然,我们这里可能有一些冗余,因为或许没有用到jstl 。
2.配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SpringMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--配置spring核心servlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--可以自定义servlet.xml配置文件名称和位置。默认路径为/WEB-INF下 。 -->
<!-- 插入点AA -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--url-patter配置为/, 表示所有的动态请求。这种方式,不带后缀,会造成其他静态文件(jss,css)不能访问。如果配置为*.do,则不影响静态文件的访问,访问时候的url也要是相应的.do -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
注意:我们这里的Servlet采用的是默认配置。这种情况下,下面的SpringMVC-servlet.xml必须以【servlet-name】-servlet.xml规则命名。在上面中,特意布置了一个插入点AA, 在该处可以自定义配置servlet的名称和位置。这里的param-value配置的名称要和下面的xxx-servlet.xml名称一致。另外,这部分代码一定要放在<load-on-starup>
标签前,即标志的插入点AA处。
<init-param>
<param-value>/WEB-INF/spring-servlet.xml</param-value>
</init-param>
2.配置SpringMVC-servlet.xml文件。
再次强调该xxx-servlet.xml文件名称及位置要和上面的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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.web.controller"/>
<bean name="/helloworld" class="com.web.controller.HelloWorldController"></bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
注意:<beans>
中的命名空间,一定不可以省略,也最好不要擅自的删除。
3.Controller的编写
controller是控制器。以java的形式存在。
package com.web.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
public class HelloWorldController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception{
System.out.println("--Hello World !--");
return new ModelAndView("/welcome");
}
}
4.welcome.jsp 作为测试页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SpringMVC</title>
</head>
<body>
<h2>不使用注解方式的SpringMVC配置样例。</h2>
</body>
</html>
5.演示效果
在浏览器中输入http://localhost:8080/SpringMVC/helloworld
至此,SpringMVC的不使用注解的搭建已经完成了。
第二种,使用注解的SpringMVC搭建方式
1.创建工程及导入jar包,方法同第一种。
2.配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>example</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3.配置hello-servlet.xml
此处的命名规则和第一种方式中解释的原理是一样的,都要和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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<mvc:annotation-driven/>
<context:component-scan base-package="com.controller"/>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
4.编写Controller
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(value={"/","/hello"})
public String hello(){
System.out.println("hello!");
return "hello";
}
}
这里,我们发现,Controller中使用了注解的方式。在文章的最后,我们将详细的来介绍这些注解,并对比第一种方式和第二种方式。
5.编写测试hello.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<H1>HELLO</H1>
</body>
</html>
6.测试结果
至此,使用注解的SpringMVC搭建已经完成。
接下来,我们将详细介绍一下注解这种方式,并对这两种方式做一个简单的对比。
(1)关于命名空间的问题,http://blog.csdn.net/tonytfjing/article/details/38979677
(2)关于注解
a. @Controller
该注解用来标示该类为一个controller, 负责注册一个bean 到spring 上下文中,bean 的ID 默认为类名称开头字母小写,你也可以自己指定。如:默认情况时
@Controller
public class aa(){
}
又如:自己指定时
@Controller("testController")
public class bb(){
}
这样可以告诉SpringMVC这是一个控制器的角色。不需要我们像第一种方式中那样在xxx-servlet.xml中显式的配置了。
b. @RequestMapping
这个注解是用来定义访问的URL, 可以具体到类,也可以具体到方法。
当为方法定义时,可以配合类来一起使用。
如:
@RequestMapping(value="/test")
public class aa(){
@RequestMapping(value="/a1")
public String mTest1(){
return "This is the first test.";
}
@RequestMapping(value="/a2")
public String mTest2(){
return "This is the second test.";
}
}
这种情况下,访问方法mTest1()的路径就是”/test/a1“。
当只单独配置了方法时,同一个类中的方法访问路径相互独立而无相同的根节点关系。
@RequestMapping的完整参数@RequestMapping(value=”“,method =
{“”,”“},headers={},params={“”,”“})
下面,我们来详细说明一下各种参数的意义。
value : 设置访问地址
method: 设置访问方式,字符数组,包括GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE,常用RequestMethod.GET,RequestMethod.POST 。
headers:一般结合method = RequestMethod.POST使用
params: 访问参数设置,字符数组 例如:userId=id
.有一点需要注意的,如果为类定义了访问地址为.do,.html之类的,则
在方法级的@RequestMapping,不能再定义value值,否则会报错
c.@ResponseBody
这个注解可以直接放在方法上,表示返回类型将会直接作为HTTP响应字节流输出(不被放置在Model,也不被拦截为视图页面名称)。可以用于ajax.
d.@RequestParam
这是一个可选参数,例如:@RequestParam(“id”) 注解,所以
它将和URL所带参数 id进行绑定 。如果入参是基本数据类型(如 int、long、float 等),URL 请求参数中一定要有对应的参数,否则将抛出
org.springframework.web.util.NestedServletException 异常,提示无
法将 null 转换为基本数据类型。
@RequestParam包含3个配置 @RequestParam(required = ,value=”“,
defaultValue = “”)
required :参数是否必须,boolean类型,可选项,默认为true
value: 传递的参数名称,String类型,可选项,如果有值,对应到设置方
法的参数 。
defaultValue:String类型,参数没有传递时为参数默认指定的值。
e. @SessionAttributes
Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到
session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。@SessionAttributes 只能声明在类上,而不能声明在方法上。
以上是我们常用的几个注解的介绍和解释,如果读者还需要更为详细的资料,这里推荐两个。
http://aijuans.iteye.com/blog/2160141
http://haohaoxuexi.iteye.com/blog/1343761
http://justice-jl.iteye.com/blog/1814980
好了,我们的SpringMVC的手动配置讲解就到此为止了,下一篇,我们将介绍一下如何利用maven来搭建SpringMVC的框架环境。