注意:spring-webmvc这个jar包的版本必须跟spring的版本一致
通过spring-web这个jar包里的contextConfigLocation这个类来启动spring,但这个类默认加载的是WEB-INF下的applicationContext文件,但这个文件我们其实放在了类路径下面,所以要修改路径
通过spring-webmvc这个jar包里的DispatcherServlet这个类来启动
完整的web.xml:(有些文件后面会补)
<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.mybatisdisplay-name>
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
web-app>
参照模板写
放在config目录下
在WEB-INF下建一个jsp目录
<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">
<contenxt:component-scan base-package="cn.sm1234.controller"/>
<mvc:annotation-driven>mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
在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
在jsp目录下新建:test.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
<head>
<title>My JSP 'test.jsp' starting pagetitle>
<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">
head>
<body>
测试SpringMVC是否可用
body>
html>
部署到Tomcat之后,打开浏览器,输入:
http://localhost:8080/06.mybatis-spring-springmvc/customer/test.action
回车,显示:
添加了两个方法:跳转到添加页面,和保存方法
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";
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<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">
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"%>
<html>
<head>
<title>My JSP 'succ.jsp' starting pagetitle>
<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">
head>
<body>
保存成功啦!
body>
html>
部署到Tomcat之后,打开浏览器,输入:
http://localhost:8080/07.mybatis-spring-springmvc/customer/input.action
回车,显示:
在输入框输入中文,提交之后发现页面传参到 Controller,中文数据乱码,这时可以在 web.xml 加多编码过滤器:
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
再次提交数据:
跳转到保存成功页面:
查看数据库,已经保存:
最后附上用到的相关jar包:
链接:https://pan.baidu.com/s/1b-HTwVCPnPxeH-gheT_uFw
提取码:qyd2