一、环境准备
- 开发工具: Myeclipse
- 运行环境:Tomcat 6.0
- 数据库支持:mysql
- 持久层:MyBatis
二、开发框架以及技术
- 前端:js+jQuery+ajax+jsp
- 后端:spring+springMVC+myBatis
- 数据库:mysql
- 接口:webService+axis1.4
- 部署环境:Tomcat 6.0
三、配置web.xml
<?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">
<!-- 配置初始打开的页面 -->
<!-- <welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> -->
<!--配置log4j日志功能,param-value记录日志的相关属性-->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<!-- 设定刷新日志配置文件的时间间隔,这里设置为60s -->
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<!-- Spring 容器加载 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--容器配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springConf/*.xml</param-value>
</context-param>
<!-- SpringMVC的前端控制器 -->
<servlet>
<servlet-name>MyDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 加载配置文件路径 ,此处不写时,默认DispatcherServlet会加载WEB-INF/[DispatcherServlet的Servlet名字]-servlet.xml配置文件,例如这里会默认加载WEB-INF/ MyDispatcher-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springMVC/*.xml</param-value>
</init-param>
<!-- 何时启动 大于0的值表示容器启动时初始化此servlet,正值越小优先级越高 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Spring MVC配置文件结束 -->
<!-- SpringMVC拦截设置,配置哪些url要交给springMVC来处理 -->
<servlet-mapping>
<servlet-name>MyDispatcher</servlet-name>
<!-- 由SpringMVC拦截所有请求 -->
<url-pattern>*.html</url-pattern>
<url-pattern>*.ajax</url-pattern>
</servlet-mapping>
<!-- SpringMVC拦截设置结束 -->
<!--解决中文乱码问题 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<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>
自此,请求交给了springMVC进行处理,接下来配置spring的配置文件,上面配置的classpath:springMVC/*.xml
四、springMVC-servlet.xml配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-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/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">
<!-- 把标记了@Controller注解的类转换为bean -->
<context:component-scan base-package="com.oneScreen.controller" />
<!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/views/" p:suffix=".jsp" />
</beans>
prefix和suffix表示查找视图页面的前缀和后缀,比如传进来的名称是hello,则该jsp视图应该在“/views/hello.jsp”目录。
也可以使用property节点进行配置,例如:
<property name="prefix" value="/views/"/>
<property name="suffix" value=".jsp"/>
五、开发页面控制器
public class HelloWorldController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
//1、收集参数、验证参数
//2、绑定参数到命令对象
//3、将命令对象传入业务对象进行业务处理
//4、选择下一个页面
ModelAndView mv = new ModelAndView();
//添加模型数据 可以是任意的POJO对象
mv.addObject("message", "Hello World!");
//设置逻辑视图名,视图解析器会根据该名字解析到具体的视图页面
mv.setViewName("hello");
return mv;
}
}
我们需要将其添加到Spring配置文件(springMVC/*.xml),让其接受Spring IoC容器管理:
<!-- 处理器 -->
<bean name="/hello" class="xxx.xxx.HelloWorldController"/>
name=”/hello”:前边配置的BeanNameUrlHandlerMapping,表示如过请求的URL为 “上下文/hello”,则将会交给该Bean进行处理。
开发视图页面
<%@ 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>Hello World</title>
</head>
<body>
${message}
</body>
</html>
${message}:表示显示由HelloWorldController处理器传过来的模型数据。
启动服务器运行测试
通过请求:http://localhost:8080/springmvc-chapter2/hello,如果页面输出“Hello World! ”就表明我们成功了!
在spring2.5之前,必须要通过实现Controller接口来定义我们的处理器类,spring2.5之后引入了注解式处理器支持,通过@Controller 和 @RequestMapping注解定义处理器类。例如:
@Controller
public class LoginController {
@Autowired
private UserDaoImpl userDao;
@RequestMapping(value="/loginController/testJson.ajax",method = RequestMethod.POST)
public void singleUserInfo(HttpServletRequest request,HttpServletResponse response, int id,int page){
System.out.println(page);
User user = userDao.findUserById(id);
String ujb = JSONObject.fromObject(user).toString();
PrintWriterUtil.sendDate(response, ujb);
}
}
- @Controller:用于标识是处理器类;
- @RequestMapping:请求到处理器功能方法的映射规则;
- @RequestParam:请求参数到处理器功能处理方法的方法参数上的绑定;
- @ModelAttribute:请求参数到命令对象的绑定;
- @SessionAttributes:用于声明session级别存储的属性,放置在处理器类上,通常列出模型属性(如@ModelAttribute)对应的名称,则这些属性会透明的保存到session中;
- @InitBinder:自定义数据绑定注册支持,用于将请求参数转换到命令对象属性的对应类型;
Spring3.0引入RESTful架构风格支持(通过@PathVariable注解和一些其他特性支持),且又引入了更多的注解支持:
- @CookieValue:cookie数据到处理器功能处理方法的方法参数上的绑定;
- @RequestHeader:请求头(header)数据到处理器功能处理方法的方法参数上的绑定;
- @RequestBody:请求的body体的绑定(通过HttpMessageConverter进行类型转换);
- @ResponseBody:处理器功能处理方法的返回值作为响应体(通过HttpMessageConverter进行类型转换);
- @ResponseStatus:定义处理器功能处理方法/异常处理器返回的状态码和原因;
- @ExceptionHandler:注解式声明异常处理器;
- @PathVariable:请求URI中的模板变量部分到处理器功能处理方法的方法参数上的绑定,从而支持RESTful架构风格的URI;
还有比如:
- JSR-303验证框架的无缝支持(通过@Valid注解定义验证元数据);
- 使用Spring 3开始的ConversionService进行类型转换(PropertyEditor依然有效),支持使用@NumberFormat 和 @DateTimeFormat来进行数字和日期的格式化;
- HttpMessageConverter(Http输入/输出转换器,比如JSON、XML等的数据输出转换器);
- ContentNegotiatingViewResolver,内容协商视图解析器,它还是视图解析器,只是它支持根据请求信息将同一模型数据以不同的视图方式展示(如json、xml、html等),RESTful架构风格中很重要的概念(同一资源,多种表现形式);