6. 整合 SpringMVC
6.1.导入 spring-mvc 包
注意:spring-webmvc这个jar包的版本必须跟spring的版本一致
6.2.配置 web.xml
- 启动 spring,加载 applicationContext.xml
通过spring-web这个jar包里的contextConfigLocation这个类来启动spring,但这个类默认加载的是WEB-INF下的applicationContext文件,但这个文件我们其实放在了类路径下面,所以要修改路径
- 启动 springMVC,加载 spring-mvc.xml
通过spring-webmvc这个jar包里的DispatcherServlet这个类来启动
完整的web.xml:(有些文件后面会补)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>01.mybatis</display-name>
<!-- 启动SpringMVC -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 参数:读取spring-mvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 启动spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 修改路径 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<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>
</web-app>
6.3.配置 spring-mvc.xml
参照模板写
放在config目录下
在WEB-INF下建一个jsp目录
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:contenxt="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 扫描Controller所在的包 -->
<contenxt:component-scan base-package="cn.sm1234.controller"/>
<!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器:简化在Controller类编写的视图路径 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
6.4.编写 Controller
在src下新建包:com.controller
在包里新建class:CustomerController.java
@RequestMapping("/customer")
表示制定访问路径,这个表示访问的是customer
CustomerController.java:
package cn.sm1234.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/customer")
public class CustomerController {
@RequestMapping("/test")
public String test(){
return "test";
}
}
这样子运行起来的访问路径就是/customer+/test
6.5.编写页面
在jsp目录下新建:test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'test.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
测试SpringMVC是否可用
</body>
</html>
部署到Tomcat之后,打开浏览器,输入:
http://localhost:8080/06.mybatis-spring-springmvc/customer/test.action
回车,显示:
7. SSM 整合-客户添加
7.1.在 CustomerController 里面添加方法
添加了两个方法:跳转到添加页面,和保存方法
package cn.sm1234.controller;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.sm1234.domain.Customer;
import cn.sm1234.service.CustomerService;
@Controller
@RequestMapping("/customer")
public class CustomerController {
//注入业务对象
@Resource
private CustomerService customerService;
/*@RequestMapping("/test")
public String test(){
return "test";
}*/
/**
* 跳转到input.jsp
*/
@RequestMapping("/input")
public String input(){
return "input";
}
/**
*保存客户
*/
@RequestMapping("/save")
public String save(Customer customer){
System.out.println("======"+customer);
customerService.saveCustomer(customer);
return "succ";
}
}
7.2.编写 input.jsp 录入客户页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>客户录入页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="${pageContext.request.contextPath}/customer/save.action" method="post">
客户姓名:<input type="text" name="name"/><br/>
客户性别:
<input type="radio" name="gender" value="男"/>男
<input type="radio" name="gender" value="女"/>女
<br/>
客户手机:<input type="text" name="telephone"/><br/>
客户住址:<input type="text" name="address"/><br/>
<input type="submit" value="保存">
</form>
</body>
</html>
还有保存成功的提示页面,succ.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'succ.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
保存成功啦!
</body>
</html>
部署到Tomcat之后,打开浏览器,输入:
http://localhost:8080/07.mybatis-spring-springmvc/customer/input.action
回车,显示:
在输入框输入中文,提交之后发现页面传参到 Controller,中文数据乱码,这时可以在 web.xml 加多编码过滤器:
<!-- 配置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>
再次提交数据:
跳转到保存成功页面:
查看数据库,已经保存:
最后附上用到的相关jar包:
链接:https://pan.baidu.com/s/1b-HTwVCPnPxeH-gheT_uFw
提取码:qyd2