SSM學習筆記3——整合 SpringMVC、整合SSM
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
相關文章
- ssm框架整合筆記SSM框架筆記
- 八、SpringMVC——ssm整合SpringMVCSSM
- SpringMVC-整合SSMSpringMVCSSM
- SpringMVC(3)- 校驗框架、SSM整合SpringMVC框架SSM
- SSM——Spring整合SpringMVC,MyBatisSSMSpringMVCMyBatis
- SSM(SpringMVC + Spring + Mybatis)整合SSMSpringMVCMyBatis
- SSM整合(Spring、SpringMVC、Mybatis)SSMSpringMVCMyBatis
- ssm整合SSM
- SSM框架整合SSM框架
- 整合SSM框架SSM框架
- SSM整合開發SSM
- SSM框架的整合SSM框架
- ssm整合(maven版本)SSMMaven
- ssm整合詳解SSM
- Maven整合SSM框架(maven+spring+springmvc+mybatis)MavenSSM框架SpringMVCMyBatis
- SSM框架整合流程SSM框架
- SSM框架整合開發SSM框架
- SSM框架快速整合redisSSM框架Redis
- 快速整合搭建SSM框架SSM框架
- SSM整合之CRUD環境搭建整合SSM
- SSM(Spring-MyBatis-SpringMVC)框架整合【完整版】SSMMyBatisSpringMVC框架
- SSM框架——詳細整合教程(Spring+SpringMVC+MyBatis)SSM框架SpringMVCMyBatis
- SSM框架——具體整合教程(Spring+SpringMVC+MyBatis)SSM框架SpringMVCMyBatis
- 【SSM整合】-Maven管理SSM框架的pom.xml配置SSMMaven框架XML
- SSM專案整合——後端SSM後端
- SSM框架整合(配置檔案)SSM框架
- Mybatis20_ssm整合10MyBatisSSM
- SSM 框架快速整合例項--學生查詢SSM框架
- 【Java】SSM框架整合 附原始碼JavaSSM框架原始碼
- Maven整合SSM和Redis,親測MavenSSMRedis
- mybatis逆向工程和SSM框架整合MyBatisSSM框架
- 使用xml檔案配置SSM整合XMLSSM
- SSM三大框架整合詳細教程(Spring+SpringMVC+MyBatis)SSM框架SpringMVCMyBatis
- Mybatis整合Spring(ssm整合待續)-day04MyBatisSpringSSM
- SpringMVC mybatis SSM 整合程式碼生成器 java redis shiro ehcacheSpringMVCMyBatisSSMJavaRedis
- [Android]後端之路--整合SSM(Spring+SpringMVC+MyBatis)框架(2)Android後端SSMSpringMVCMyBatis框架
- 最容易的ssm三大框架整合(spring+springmvc+mybatis)教程SSM框架SpringMVCMyBatis
- SSM三大框架整合詳細教程SSM框架