1. 1 # SSM框架
  2. 2
  3. 3 # **什么是框架**
  4. 4
  5. 5 就是模板,将一些基础性的程序和代码由框架模板提供,然后程序员补充和业务需求相关的代码。
  6. 6
  7. 7 # **ssm框架组成**
  8. 8
  9. 9 s: springMvc 子框架 代替servlet类,control
  10. 10
  11. 11 s: spring 框架系列
  12. 12
  13. 13 基本作用:创建对象(控制反转和依赖注入)
  14. 14
  15. 15 m: mybatis 框架 代替dao包,和数据库进行交互(jdbc技术),表 model层(service包和dao包)
  16. 16
  17. 17 **补充**
  18. 18
  19. 19 view层:视图层,还是使用jsp实现
  20. 20
  21. 21 # **项目演示**
  22. 22
  23. 23 ## 目标:用户管理项目
  24. 24
  25. 25 ## 操作步骤:
  26. 26
  27. 27 ### 1、创建maven项目,并配置tomcat服务器
  28. 28
  29. 29 **配置tomcat时无artifact选项解决方案**
  30. 30
  31. 31 <img src="img/1573611676801.png" alt="1573611676801" style="zoom:33%;" />
  32. 32
  33. 33
  34. 34
  35. 35 打开
  36. 36
  37. 37 <img src="img/1573611734587.png" alt="1573611734587" style="zoom:33%;" />
  38. 38
  39. 39 选择
  40. 40
  41. 41 <img src="img/1573611802769.png" alt="1573611802769" style="zoom:33%;" />
  42. 42
  43. 43
  44. 44
  45. 45 ### 2、创建java和resources目录,并mark一下,新建文件夹变色
  46. 46
  47. 47 ![1573519990356](img/ssm.md)
  48. 48
  49. 49 java文件夹:存放程序员编写的java代码
  50. 50
  51. 51 resources文件夹:存放配置文件(和ssm框架相关)
  52. 52
  53. 53 ### 3、myBatis框架(替代jdbc技术)
  54. 54
  55. 55 #### 3.1 导入和mybatis相关的依赖(包)
  56. 56
  57. 57 mybatis
  58. 58
  59. 59 mybatis-spring
  60. 60
  61. 61 mysql-connector-java
  62. 62
  63. 63 ```xml
  64. 64 <!--mybatis-->
  65. 65 <dependency>
  66. 66 <groupId>org.mybatis</groupId>
  67. 67 <artifactId>mybatis</artifactId>
  68. 68 <version>3.5.2</version>
  69. 69 </dependency>
  70. 70 <!--mybatis-spring 二者的整合-->
  71. 71 <dependency>
  72. 72 <groupId>org.mybatis</groupId>
  73. 73 <artifactId>mybatis-spring</artifactId>
  74. 74 <version>2.0.2</version>
  75. 75 </dependency>
  76. 76 <!--mysql-connector-java 用于java程序连接mysql数据库-->
  77. 77 <dependency>
  78. 78 <groupId>mysql</groupId>
  79. 79 <artifactId>mysql-connector-java</artifactId>
  80. 80 <version>8.0.17</version>
  81. 81 </dependency>
  82. 82 ```
  83. 83
  84. 84 #### 3.2 在resources文件夹中创建文件mybatis-config.xml
  85. 85
  86. 86 ```xml
  87. 87 <?xml version="1.0" encoding="UTF-8" ?>
  88. 88 <!DOCTYPE configuration
  89. 89 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  90. 90 "http://mybatis.org/dtd/mybatis-3-config.dtd">
  91. 91 <configuration>
  92. 92 <!--该文件中可以对mybatis框架进行一些配置设置-->
  93. 93
  94. 94 </configuration>
  95. 95 ```
  96. 96
  97. 97 ### 4.spring框架
  98. 98
  99. 99 #### 4.1添加和spring相关的常用的依赖
  100. 100
  101. 101 spring-beans
  102. 102
  103. 103 spring-context/spring-context-support
  104. 104
  105. 105 spring-core
  106. 106
  107. 107 spring-expression
  108. 108
  109. 109 spring-jdbc
  110. 110
  111. 111 spring-tx
  112. 112
  113. 113 spring-web
  114. 114
  115. 115 commons-dbcp
  116. 116
  117. 117 ```xml
  118. 118 <dependency>
  119. 119 <groupId>org.springframework</groupId>
  120. 120 <artifactId>spring-beans</artifactId>
  121. 121 <version>5.1.8.RELEASE</version>
  122. 122 </dependency>
  123. 123 <dependency>
  124. 124 <groupId>org.springframework</groupId>
  125. 125 <artifactId>spring-context</artifactId>
  126. 126 <version>5.1.8.RELEASE</version>
  127. 127 </dependency>
  128. 128 <dependency>
  129. 129 <groupId>org.springframework</groupId>
  130. 130 <artifactId>spring-context-support</artifactId>
  131. 131 <version>5.0.10.RELEASE</version>
  132. 132 </dependency>
  133. 133 <dependency>
  134. 134 <groupId>org.springframework</groupId>
  135. 135 <artifactId>spring-core</artifactId>
  136. 136 <version>5.1.8.RELEASE</version>
  137. 137 </dependency>
  138. 138 <dependency>
  139. 139 <groupId>org.springframework</groupId>
  140. 140 <artifactId>spring-expression</artifactId>
  141. 141 <version>5.1.8.RELEASE</version>
  142. 142 </dependency>
  143. 143 <dependency>
  144. 144 <groupId>org.springframework</groupId>
  145. 145 <artifactId>spring-jdbc</artifactId>
  146. 146 <version>5.0.10.RELEASE</version>
  147. 147 </dependency>
  148. 148 <dependency>
  149. 149 <groupId>org.springframework</groupId>
  150. 150 <artifactId>spring-tx</artifactId>
  151. 151 <version>5.1.8.RELEASE</version>
  152. 152 </dependency>
  153. 153 <dependency>
  154. 154 <groupId>org.springframework</groupId>
  155. 155 <artifactId>spring-web</artifactId>
  156. 156 <version>5.1.8.RELEASE</version>
  157. 157 </dependency>
  158. 158
  159. 159 <!--commons-dbcp 用于创建dataSource数据源-->
  160. 160 <dependency>
  161. 161 <groupId>commons-dbcp</groupId>
  162. 162 <artifactId>commons-dbcp</artifactId>
  163. 163 <version>1.4</version>
  164. 164 </dependency>
  165. 165 ```
  166. 166
  167. 167
  168. 168
  169. 169 #### 4.2在resources文件夹创建applicationContext.xml文件,用于对spring框架配置的设置
  170. 170
  171. 171 ```xml
  172. 172 <?xml version="1.0" encoding="UTF-8"?>
  173. 173 <beans xmlns="http://www.springframework.org/schema/beans"
  174. 174 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  175. 175 xmlns:context="http://www.springframework.org/schema/context"
  176. 176 xsi:schemaLocation="http://www.springframework.org/schema/beans
  177. 177 http://www.springframework.org/schema/beans/spring-beans.xsd
  178. 178 http://www.springframework.org/schema/context
  179. 179 https://www.springframework.org/schema/context/spring-context.xsd">
  180. 180 <!--用于spring框架的配置设置-->
  181. 181
  182. 182 </beans>
  183. 183 ```
  184. 184
  185. 185 ### 5.springmvc框架
  186. 186
  187. 187 #### 5.1添加依赖(jar包)
  188. 188
  189. 189 spring-mvc
  190. 190
  191. 191 ```xml
  192. 192 <!--springmvc的依赖-->
  193. 193 <dependency>
  194. 194 <groupId>org.springframework</groupId>
  195. 195 <artifactId>spring-webmvc</artifactId>
  196. 196 <version>5.1.5.RELEASE</version>
  197. 197 </dependency>
  198. 198 ```
  199. 199
  200. 200
  201. 201
  202. 202 #### 5.2在resources目录下创建springmvc-servlet.xml文件,对springmvc进行的配置设置
  203. 203
  204. 204 ```xml
  205. 205 <?xml version="1.0" encoding="UTF-8"?>
  206. 206 <beans xmlns="http://www.springframework.org/schema/beans"
  207. 207 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  208. 208 xmlns:mvc="http://www.springframework.org/schema/mvc"
  209. 209 xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans
  210. 210 http://www.springframework.org/schema/beans/spring-beans.xsd
  211. 211 http://www.springframework.org/schema/context
  212. 212 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
  213. 213 <!--该文件用于对springmc框架的配置设置-->
  214. 214
  215. 215
  216. 216 </beans>
  217. 217 ```
  218. 218
  219. 219 #### **注意处理**
  220. 220
  221. 221 ![1573526006338](img/1573526006338.png)
  222. 222
  223. 223
  224. 224
  225. 225 ### 6.在java目录下创建一些基本的包
  226. 226
  227. 227 dao :用于连接数据库
  228. 228
  229. 229 service:用于进行业务逻辑的处理
  230. 230
  231. 231 controller:用于对请求的派发和处理
  232. 232
  233. 233 pojo:将数据库的表名定义为类名,字段定义为属性,此类放在此包中
  234. 234
  235. 235 interceptor:拦截器包,使用的场景:处理非法登陆。
  236. 236
  237. 237 util:工具包,存放一些工具类,比如jdbcUtil,分页插件类等等。
  238. 238
  239. 239 test:测试包,用于测试,junit等进行测试。
  240. 240
  241. 241 <img src="img/1573526271379.png" alt="1573526271379" style="zoom:33%;" />
  242. 242
  243. 243 ### 7.pojo中类的创建和编写
  244. 244
  245. 245 根据数据库中的表创建对应类,好处是通过操作pojo类,来代替操作数据库中的表(对象关系映射orm技术)
  246. 246
  247. 247 ```java
  248. 248 //表名对应类名
  249. 249 public class User {
  250. 250
  251. 251 //表字段对应类属性
  252. 252 private int id;
  253. 253 private String username;
  254. 254 private String password;
  255. 255
  256. 256 public int getId() {
  257. 257 return id;
  258. 258 }
  259. 259
  260. 260 public void setId(int id) {
  261. 261 this.id = id;
  262. 262 }
  263. 263
  264. 264 public String getUsername() {
  265. 265 return username;
  266. 266 }
  267. 267
  268. 268 public void setUsername(String username) {
  269. 269 this.username = username;
  270. 270 }
  271. 271
  272. 272 public String getPassword() {
  273. 273 return password;
  274. 274 }
  275. 275
  276. 276 public void setPassword(String password) {
  277. 277 this.password = password;
  278. 278 }
  279. 279 }
  280. 280
  281. 281 ```
  282. 282
  283. 283 ### 8、在resources目录下,创建database.properties文件,该文件中编写:driver,url,username,password等连接数据库的参数
  284. 284
  285. 285 ```xml
  286. 286 driver=com.mysql.jdbc.Driver
  287. 287 url=jdbc:mysql://127.0.0.1:3306/school?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
  288. 288 user=root
  289. 289 password=root
  290. 290 ```
  291. 291
  292. 292 ### 9、进行java应用项目连接数据库
  293. 293
  294. 294 **在applicationContext.xml文件下**
  295. 295
  296. 296 ```xml
  297. 297 <!--1 引入database.properties文件-->
  298. 298 <context:property-placeholder location="classpath:database.properties"/>
  299. 299
  300. 300 <!--2 创建数据源-->
  301. 301 <!--基本数据源(配置数据源)-->
  302. 302 <!-- 配置数据源,核心类:基本数据源BasicDataSource类 -->
  303. 303 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
  304. 304 <property name="driverClassName" value="${driver}"/>
  305. 305 <property name="url" value="${url}"/>
  306. 306 <property name="username" value="${user}"/>
  307. 307 <property name="password" value="${password}"/>
  308. 308 </bean>
  309. 309
  310. 310 <!--3 对数据源进行事务管理-->
  311. 311 <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  312. 312 <property name="dataSource" ref="dataSource"/>
  313. 313 </bean>
  314. 314
  315. 315 <!--4 配置mybatis核心类SqlSessionFactoryBean,引入数据源,引入mybatis-config.xml-->
  316. 316 <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
  317. 317 <property name="dataSource" ref="dataSource"/>
  318. 318 <property name="configLocation" value="classpath:mybatis-config.xml"/>
  319. 319 </bean>
  320. 320 ```
  321. 321
  322. 322 ### 10、在dao包中创建接口和mapper文件,在spring配置文件中配置
  323. 323
  324. 324 mapper文件中编写sql语句
  325. 325
  326. 326 <img src="img/1573530423150.png" alt="1573530423150" style="zoom:25%;" />
  327. 327
  328. 328 #### **a 创建接口UserMapper**
  329. 329
  330. 330 ```java
  331. 331 public interface UserMapper {
  332. 332 //登陆功能
  333. 333 public User login(@Param("xiaomao")String username,@Param("xiaogou")String password);
  334. 334 }
  335. 335 ```
  336. 336
  337. 337
  338. 338
  339. 339 #### **b 创建UserMapper文件**
  340. 340
  341. 341 ```xml
  342. 342 <?xml version="1.0" encoding="UTF-8" ?>
  343. 343 <!DOCTYPE mapper
  344. 344 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  345. 345 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  346. 346 <mapper namespace="com.baidu.dao.UserMapper">
  347. 347 <!--namespace 赋值:对应的接口全路径-->
  348. 348 <!--select * from user where username=? and password=?-->
  349. 349 <!--id 填写 对应的接口方法名, resultType 填写接口方法的返回类型-->
  350. 350 <!--登陆方法-->
  351. 351 <select id="login" resultType="com.baidu.pojo.User">
  352. 352 select * from user where username=#{xiaomao} and password=#{xiaogou}
  353. 353 </select>
  354. 354 </mapper>
  355. 355 ```
  356. 356
  357. 357 #### c 在applicationContext中配置对mapper文件扫描
  358. 358
  359. 359 ```xml
  360. 360 <!--映射扫描配置器(引入sql映射文件)-->
  361. 361 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  362. 362 <property name="basePackage" value="com.baidu.dao"/>
  363. 363 </bean>
  364. 364 ```
  365. 365
  366. 366 #### d 在pom.xml中<build>内添加
  367. 367
  368. 368 **将src/main/java目录下的所有*.xml文件编译到class内,不然报错:找不到mapper文件**
  369. 369
  370. 370 ```xml
  371. 371 <build>
  372. 372 <!--将java目录下的所有*.xml文件编译到class内,不然报错:找不到mapper文件-->
  373. 373 <resources>
  374. 374 <resource>
  375. 375 <directory>src/main/java</directory>
  376. 376 <includes>
  377. 377 <include>**/*.xml</include>
  378. 378 </includes>
  379. 379 <filtering>false</filtering>
  380. 380 </resource>
  381. 381 </resources>
  382. 382 </build>
  383. 383 ```
  384. 384
  385. 385
  386. 386
  387. 387 ### 11、在service包中创建接口和实现类,编写业务逻辑代码
  388. 388
  389. 389 #### 接口
  390. 390
  391. 391 ```java
  392. 392 public interface UserService {
  393. 393 //登陆功能
  394. 394 public boolean login(String username,String password);
  395. 395 }
  396. 396 ```
  397. 397
  398. 398 #### 实现类
  399. 399
  400. 400 ```java
  401. 401 public class UserServiceImpl implements UserService {
  402. 402
  403. 403 @Resource//此时报错,缺少依赖:javax.annotation-api
  404. 404 private UserMapper userMapper=null;
  405. 405
  406. 406 //登陆方法
  407. 407 public boolean login(String username, String password) {
  408. 408 User user = userMapper.login(username, password);
  409. 409 boolean flag=false;
  410. 410 if(user!=null){//用户存在
  411. 411 flag=true;
  412. 412 }
  413. 413 return flag;
  414. 414 }
  415. 415 }
  416. 416 ```
  417. 417
  418. 418 #### **补充支持@Resource注解的依赖**
  419. 419
  420. 420 ```xml
  421. 421 <!--@Resource-->
  422. 422 <dependency>
  423. 423 <groupId>javax.annotation</groupId>
  424. 424 <artifactId>javax.annotation-api</artifactId>
  425. 425 <version>1.3.1</version>
  426. 426 </dependency>
  427. 427 ```
  428. 428
  429. 429
  430. 430
  431. 431 ### 12、在applicationContext.xml文件中对组件进行定义和扫描
  432. 432
  433. 433 #### **组件扫描**
  434. 434
  435. 435 ##### a、定义组件
  436. 436
  437. 437 编写userController类,将@Controller 放在controller包的类上
  438. 438
  439. 439 ```java
  440. 440 @Controller
  441. 441 public class UserController {
  442. 442 //编写对请求的派发和处理
  443. 443
  444. 444 }
  445. 445 ```
  446. 446
  447. 447 @Service 放在service包的实现类上
  448. 448
  449. 449 ```java
  450. 450 public interface UserService {
  451. 451 //登陆功能
  452. 452 public boolean login(String username,String password);
  453. 453 }
  454. 454 ```
  455. 455
  456. 456 ```
  457. 457 @Service
  458. 458 public class UserServiceImpl implements UserService {
  459. 459
  460. 460 @Autowired//@Resource同一功能
  461. 461 private UserMapper userMapper=null;
  462. 462
  463. 463 //登陆方法
  464. 464 public boolean login(String username, String password) {
  465. 465 User user = userMapper.login(username, password);
  466. 466 boolean flag=false;
  467. 467 if(user!=null){//用户存在
  468. 468 flag=true;
  469. 469 }
  470. 470 return flag;
  471. 471 }
  472. 472 }
  473. 473 ```
  474. 474
  475. 475
  476. 476
  477. 477 @Repository 放在dao包的实现类上(如果没有实现类,就省略)
  478. 478
  479. 479 ```java
  480. 480 public interface UserMapper {
  481. 481 //登陆功能
  482. 482 public User login(@Param("xiaomao") String username, @Param("xiaogou") String password);
  483. 483 }
  484. 484 ```
  485. 485
  486. 486 ##### b、在applicationContext.xml容器中配置,使组件生效
  487. 487
  488. 488 ```xml
  489. 489 <!--组件浏览-->
  490. 490 <context:component-scan base-package="com.baidu.service"/>
  491. 491 <context:component-scan base-package="com.baidu.dao"/>
  492. 492
  493. 493 <!--注解配置-->
  494. 494 <context:annotation-config/>
  495. 495 ```
  496. 496
  497. 497 ##### c、在springmvc-serlvet.xml容器中配置,使组件生效
  498. 498
  499. 499 ```java
  500. 500 <!-- 支持组件-->
  501. 501 <context:component-scan base-package="com.baidu.controller"/>
  502. 502 ```
  503. 503
  504. 504 ### 13、配置web.xml文件
  505. 505
  506. 506 #### a."<web-app>"中添加属性
  507. 507
  508. 508 ```xml
  509. 509 <web-app
  510. 510 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  511. 511 xmlns="http://java.sun.com/xml/ns/javaee"
  512. 512 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  513. 513 id="WebApp_ID" version="3.0">
  514. 514 ```
  515. 515
  516. 516 #### b、配置监听器
  517. 517
  518. 518 ```xml
  519. 519 <!-- 配置监听器 -->
  520. 520 <listener>
  521. 521 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  522. 522 </listener>
  523. 523 ```
  524. 524
  525. 525 #### c、引入spring配置文件
  526. 526
  527. 527 ```xml
  528. 528 <!--引入spring的配置文件,制定文件的位置-->
  529. 529 <context-param>
  530. 530 <param-name>contextConfigLocation</param-name>
  531. 531 <param-value>classpath:applicationContext.xml</param-value>
  532. 532 </context-param>
  533. 533 ```
  534. 534
  535. 535 #### d、配置DispatcherServlet(类似于“总机调度”)
  536. 536
  537. 537 ```xml
  538. 538 <!--配置DispatcherServlet-->
  539. 539 <servlet>
  540. 540 <servlet-name>DispatcherServlet</servlet-name>
  541. 541 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  542. 542 <init-param>
  543. 543 <param-name>contextConfigLocation</param-name>
  544. 544 <param-value>classpath:springmvc-servlet.xml</param-value>
  545. 545 </init-param>
  546. 546 <load-on-startup>1</load-on-startup>
  547. 547 </servlet>
  548. 548 <servlet-mapping>
  549. 549 <servlet-name>DispatcherServlet</servlet-name>
  550. 550 <!--所有的请求都要被拦截,然后进行转发-->
  551. 551 <url-pattern>/</url-pattern>
  552. 552 </servlet-mapping>
  553. 553 ```
  554. 554
  555. 555 ### 14、配置springmvc-servlet.xml
  556. 556
  557. 557 #### a、配置对组件@Controller的扫描(上述已经操作)
  558. 558
  559. 559 ```xml
  560. 560 <!-- 支持组件-->
  561. 561 <context:component-scan base-package="com.baidu.controller"/>
  562. 562 ```
  563. 563
  564. 564 #### b、支持注解
  565. 565
  566. 566 (@RequestMapping@RequestParam等)
  567. 567
  568. 568 ```xml
  569. 569 <!--支持注解-->
  570. 570 <mvc:annotation-driven/>
  571. 571 ```
  572. 572
  573. 573 #### c、配置视图解析器
  574. 574
  575. 575 - /WEB-INF/下创建jsp文件夹
  576. 576
  577. 577 - 通过逻辑视图名(jsp文件的文件名)找到真实的视图名(jsp文件的全名)
  578. 578
  579. 579 举例: 真实的视图名 index.jsp 《---逻辑视图名 jsp
  580. 580
  581. 581 比如: /WEB-INF/jsp/index.jsp
  582. 582
  583. 583 ```xml
  584. 584 <!--配置视图解析器-->
  585. 585 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  586. 586 <property name="prefix" value="/WEB-INF/jsp/"/>
  587. 587 <property name="suffix" value=".jsp"/>
  588. 588 </bean>
  589. 589 ```
  590. 590
  591. 591 #### d、引入静态资源
  592. 592
  593. 593 - webapp下创建statics文件夹
  594. 594 - springmvc-servlet.xml配置文件中添加
  595. 595
  596. 596 ```xml
  597. 597 <!--引入静态资源--><mvc:resources mapping="/statics/**" location="/statics/"/>
  598. 598 ```
  599. 599
  600. 600 ### 15、在controller类中创建控制器方法
  601. 601
  602. 602 ```java
  603. 603 /**
  604. 604 * @author 四两数字先生(CSDN/公众号)
  605. 605 */
  606. 606 @Controller
  607. 607 @RequestMapping("/user")
  608. 608 public class UserController {
  609. 609
  610. 610 //private UserService userService=new UserServiceImpl();
  611. 611 @Resource //自动装配
  612. 612 private UserService userService;
  613. 613
  614. 614 @RequestMapping("/index")
  615. 615 public String index(){
  616. 616 return "index";//index就是逻辑视图名
  617. 617 }
  618. 618 @RequestMapping("/login")
  619. 619 public String login(@RequestParam(value = "xiaogou",required = false) String username,
  620. 620 @RequestParam(value = "xiaomao",required = false) String password){
  621. 621 boolean flag = this.userService.login(username, password);
  622. 622 if(flag){//用户存在
  623. 623 return "success";
  624. 624 }
  625. 625 return "failure";
  626. 626 }
  627. 627 }
  628. 628 ```
  629. 629
  630. 630 ### 16、编写jsp文件index.jsp
  631. 631
  632. 632 a.编写登陆表单
  633. 633
  634. 634 ```html
  635. 635 <form method="get" action="<%=request.getContextPath() %>/user/login">
  636. 636 用户名:<input type="text" name="xiaogou" value="">
  637. 637 密码:<input type="password" name="xiaomao" value="">
  638. 638 <input type="submit" name="submit" value="登陆">
  639. 639 </form>
  640. 640 ```
  641. 641
  642. 642 b.添加 sevlet-api依赖
  643. 643
  644. 644 ```xml
  645. 645 <!--servlet-api-->
  646. 646 <dependency>
  647. 647 <groupId>javax.servlet</groupId>
  648. 648 <artifactId>javax.servlet-api</artifactId>
  649. 649 <version>4.0.1</version>
  650. 650 </dependency>
  651. 651 ```
  652. 652
  653. 653 b.编写登陆成功页面success.jsp
  654. 654
  655. 655 ```
  656. 656 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  657. 657 <html>
  658. 658 <head>
  659. 659 <title>登陆成功</title>
  660. 660 </head>
  661. 661 <body>
  662. 662 <h1>登陆成功</h1>
  663. 663 </body>
  664. 664 </html>
  665. 665 ```
  666. 666
  667. 667
  668. 668
  669. 669 c.编写登陆失败页面failure.jsp
  670. 670
  671. 671 ```
  672. 672 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  673. 673 <html>
  674. 674 <head>
  675. 675 <title>登陆失败</title>
  676. 676 </head>
  677. 677 <body>
  678. 678 <h1>登陆失败</h1>
  679. 679 </body>
  680. 680 </html>
  681. 681 ```
  682. 682
  683. 683 # Spring的两大核心技术
  684. 684
  685. 685 ## 控制反转/依赖注入
  686. 686
  687. 687 1、添加依赖
  688. 688
  689. 689 spring-core
  690. 690
  691. 691 spring-context
  692. 692
  693. 693 spring-beans
  694. 694
  695. 695 spring-expression
  696. 696
  697. 697 日志依赖
  698. 698
  699. 699 log4j
  700. 700
  701. 701 commons-logging
  702. 702
  703. 703
  704. 704
  705. 705 2、编写代码
  706. 706
  707. 707 验证控制反转/依赖注入
  708. 708
  709. 709 applicationContext.xml
  710. 710
  711. 711 ```xml
  712. 712 <?xml version="1.0" encoding="UTF-8"?>
  713. 713 <beans xmlns="http://www.springframework.org/schema/beans"
  714. 714 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  715. 715 xmlns:context="http://www.springframework.org/schema/context"
  716. 716 xsi:schemaLocation="http://www.springframework.org/schema/beans
  717. 717 http://www.springframework.org/schema/beans/spring-beans.xsd
  718. 718 http://www.springframework.org/schema/context
  719. 719 https://www.springframework.org/schema/context/spring-context.xsd">
  720. 720
  721. 721 <bean id="person1" class="com.baidu.Person">
  722. 722 <property name="name" value="张三"/>
  723. 723 </bean>
  724. 724
  725. 725
  726. 726 </beans>
  727. 727 ```
  728. 728
  729. 729 person.java
  730. 730
  731. 731 ```java
  732. 732 /**
  733. 733 * @author 四两数字先生(CSDN/公众号)
  734. 734 */
  735. 735 public class Person {
  736. 736 String name;
  737. 737
  738. 738 public String getName() {
  739. 739 return name;
  740. 740 }
  741. 741
  742. 742 public void setName(String name) {
  743. 743 this.name = name;
  744. 744 }
  745. 745
  746. 746 //speak方法
  747. 747 public void speak(){
  748. 748 System.out.println(name+"speak hello world");
  749. 749 }
  750. 750 }
  751. 751
  752. 752 ```
  753. 753
  754. 754 MyTest.java
  755. 755
  756. 756 ```java
  757. 757 /**
  758. 758 * @author 四两数字先生(CSDN/公众号)
  759. 759 */
  760. 760 public class MyTest {
  761. 761
  762. 762 @Test
  763. 763 public void test1(){
  764. 764 /**
  765. 765 * 通过代码创建对象
  766. 766 * 通过代码给属性赋值
  767. 767 */
  768. 768 //创建对象
  769. 769 Person person=new Person();
  770. 770 //给属性赋值
  771. 771 person.setName("张三");
  772. 772 //调用speak方法输出
  773. 773 person.speak();
  774. 774
  775. 775 }
  776. 776
  777. 777 @Test
  778. 778 public void test2(){
  779. 779 /**
  780. 780 * 通过spring技术之一:控制反转/依赖注入实现来实现
  781. 781 * 1 由spring容器创建对象
  782. 782 * 2 由spring容器给属性赋值
  783. 783 *
  784. 784 * 控制反转/依赖注入:
  785. 785 * 控制权正转:通过java代码创建对象和给属性赋值
  786. 786 * 控制权反转: 将创建对象和给属性赋值的权利反转给spring容器;
  787. 787 * 控制反转就等价于依赖注入
  788. 788 *
  789. 789 */
  790. 790 //调用speak方法输出
  791. 791 //通过spring配置文件,获得对象ApplicationContext
  792. 792 ApplicationContext applicationContext =
  793. 793 new ClassPathXmlApplicationContext
  794. 794 ("applicationContext.xml");
  795. 795
  796. 796 Person person1 = (Person) applicationContext.getBean("person1");
  797. 797
  798. 798 person1.speak();
  799. 799
  800. 800 }
  801. 801 }
  802. 802
  803. 803 ```
  804. 804
  805. 805 ## 面向切面编程技术
  806. 806
  807. 807 在编写代码的过程中,有些代码和业务相关(比如增删改查功能),有些代码和业务没有直接关系,但是是必不可少(日志,安全权限等)
  808. 808
  809. 809 1、添加依赖
  810. 810
  811. 811 spring-core
  812. 812
  813. 813 spring-context
  814. 814
  815. 815 spring-beans
  816. 816
  817. 817 spring-expression
  818. 818
  819. 819 **和日志相关**
  820. 820
  821. 821 commons-logging
  822. 822
  823. 823 log4j
  824. 824
  825. 825 **和aop相关**
  826. 826
  827. 827 spring-aop
  828. 828
  829. 829 aopalliance
  830. 830
  831. 831 aspectjweave
  832. 832
  833. 833 # springMvc
  834. 834
  835. 835 ## **参数传递(View to Controller)**
  836. 836
  837. 837 ### **url含参传递(@RequestParam)**
  838. 838
  839. 839 ```java
  840. 840 @Controller
  841. 841 public class IndexController{
  842. 842
  843. 843 @RequestMapping(“/welcome”)
  844. 844
  845. 845 public String index(@RequestParam(value=”username”, required=false)String username){
  846. 846
  847. 847
  848. 848 }
  849. 849
  850. 850 }
  851. 851 ```
  852. 852
  853. 853 ### **更精确的url含参传递(@RequestMapping)**
  854. 854
  855. 855 注意:http请求信息不仅不含请求url地址,还包含请求方法/请求参数/请求头
  856. 856
  857. 857 ```
  858. 858 @RequestMapping(value=”/welcome”, method=RequestMethod.GET, params=”username”)
  859. 859
  860. 860 public String index(@RequestParam String username){
  861. 861
  862. 862 logger.info(username);
  863. 863
  864. 864 return “index”;
  865. 865
  866. 866 }
  867. 867 ```
  868. 868
  869. 869 ## **参数传递(Controller to View)**
  870. 870
  871. 871 ### **ModelAndView实现参数传递**
  872. 872
  873. 873 ```java
  874. 874 Controller
  875. 875
  876. 876 @Request Mapping(“/welcome”)
  877. 877 public String index(@RequestParam(value=”username”,required=false) String username){
  878. 878 logger.info();
  879. 879 ModelAndView m=new ModelAndView();
  880. 880 m.addObject(username);
  881. 881 m.setViewName(“index”);
  882. 882 Return m;
  883. 883 }
  884. 884 ```
  885. 885
  886. 886 index.jsp 获得容器中的参数
  887. 887
  888. 888 ```html
  889. 889 <h2 >modelAndView(key:default)--->${string}</h2>
  890. 890 ```
  891. 891
  892. 892 ### **Model实现参数传递**
  893. 893
  894. 894 需要使用到el表达式需要添加什么?
  895. 895
  896. 896 web.xml需要修改?
  897. 897
  898. 898 添加 web-app 后面的属性内容才能支持el表达式
  899. 899
  900. 900 ```xml
  901. 901 <?xml version="1.0" encoding="UTF-8"?>
  902. 902
  903. 903 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  904. 904 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  905. 905 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  906. 906 version="3.0">
  907. 907 </web-app>
  908. 908 ```
  909. 909
  910. 910 Controller
  911. 911
  912. 912 ```java
  913. 913 @RequestMapping(“/welcome”)
  914. 914 public String index(@RequestParam String username, Model model){
  915. 915 logger.info();
  916. 916
  917. 917 //1
  918. 918 model.addAttribute(“username”,username);
  919. 919
  920. 920 //2
  921. 921 model.addAttribute(username);
  922. 922
  923. 923 //3
  924. 924 User user= new User();
  925. 925 user.setUserName(username); model.addAttribute(“currentUserName”,user);
  926. 926 Return “index”;
  927. 927
  928. 928 }
  929. 929 ```
  930. 930
  931. 931 Index.jsp
  932. 932
  933. 933 ```html
  934. 934 <h1>model(key:username)----> ${username}</h1>
  935. 935
  936. 936 <h1>model(key:default)-------> ${string}</h1>
  937. 937
  938. 938 <h1>model(key:currentUserName)---->${currentUseName.userName}</h1>
  939. 939 ```
  940. 940
  941. 941 ### **Map实现参数传递**
  942. 942
  943. 943 Controller
  944. 944
  945. 945 注意:Map<String,Object> model 必须以形参的方式引入
  946. 946
  947. 947 ```java
  948. 948 @RequestMapping(“/welcome”)
  949. 949 public String index(@RequestParam(value=”username”) String username,Map<String,Object> map){
  950. 950 map.put(“username”,username);
  951. 951 return “index”;
  952. 952 }
  953. 953 ```
  954. 954
  955. 955 # Ajax技术
  956. 956
  957. 957 1 register.jsp
  958. 958
  959. 959 ```java
  960. 960 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  961. 961 <html>
  962. 962 <head>
  963. 963 <title>注册</title>
  964. 964 <script src="statics/js/jquery-3.4.1.js"></script>
  965. 965 <script>
  966. 966 $(document).ready(function () {
  967. 967 $("#name").blur(function () {
  968. 968 var name=this.value;
  969. 969 if(name==null || name==""){
  970. 970 $("#nameDiv").html("用户名不能为空");
  971. 971 }else {
  972. 972 $.ajax({
  973. 973
  974. 974 "url" :"<%=request.getContextPath()%>/AjaxServlet",//action
  975. 975 "data":"name1="+name, //含参 &name=lisi
  976. 976 "type" :"get",//method 请求方式
  977. 977
  978. 978 "success":callBack,//返回结果成功,写函数名
  979. 979 "dataType":"text",//返回的数据类型
  980. 980 "error": function () {
  981. 981 alert("返回结果失败");
  982. 982 }
  983. 983 });
  984. 984
  985. 985 function callBack(data) {//返回的结果
  986. 986 if(data=="true"){//== 字符串
  987. 987 $("#nameDiv").html("该用户已被使用");
  988. 988 }else{
  989. 989 $("#nameDiv").html("该用户可以使用");
  990. 990 }
  991. 991 }
  992. 992 }
  993. 993 });
  994. 994 });
  995. 995 </script>
  996. 996 </head>
  997. 997 <body>
  998. 998 <form action="" method="">
  999. 999 <%--请求方式
  1000. 1000 get 表单中参数会出现url路径中
  1001. 1001 post 表单的参数不会出现在url路径中,更加安全
  1002. 1002 --%>
  1003. 1003 <h1>注册页面</h1>
  1004. 1004 姓名:<input type="text" name="username" value="" id="name">
  1005. 1005 <div id="nameDiv"></div>
  1006. 1006 密码:<input type="password" name="password" value="">
  1007. 1007 再次确认密码:<input type="password" name="password" value="">
  1008. 1008 <input type="submit" name="register" value="注册">
  1009. 1009 </form>
  1010. 1010 </body>
  1011. 1011 </html>
  1012. 1012 ```
  1013. 1013
  1014. 1014 2 AjaxServlet.jsp
  1015. 1015
  1016. 1016 ```java
  1017. 1017 package com.baidu.servlet;
  1018. 1018
  1019. 1019 import com.baidu.service.UserService;
  1020. 1020 import com.baidu.service.UserServiceImpl;
  1021. 1021
  1022. 1022 import javax.servlet.ServletException;
  1023. 1023 import javax.servlet.annotation.WebServlet;
  1024. 1024 import javax.servlet.http.HttpServlet;
  1025. 1025 import javax.servlet.http.HttpServletRequest;
  1026. 1026 import javax.servlet.http.HttpServletResponse;
  1027. 1027 import java.io.IOException;
  1028. 1028 import java.io.PrintWriter;
  1029. 1029
  1030. 1030 /**
  1031. 1031 * @author 四两数字先生(CSDN/公众号)
  1032. 1032 */
  1033. 1033 public class AjaxServlet extends HttpServlet {
  1034. 1034
  1035. 1035
  1036. 1036 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  1037. 1037 this.doGet(request,response);
  1038. 1038 }
  1039. 1039
  1040. 1040 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  1041. 1041 //编写代码
  1042. 1042 //防止中文乱码
  1043. 1043 request.setCharacterEncoding("utf-8");
  1044. 1044 String name = request.getParameter("name1");//name是哪里
  1045. 1045 boolean flag=false;
  1046. 1046
  1047. 1047 //模拟数据库:假设数据库中存在用户名"admin"
  1048. 1048 if(name.equals("admin")){
  1049. 1049 flag=true;//boolean 型返回前端时自动变成字符串"true"
  1050. 1050 }
  1051. 1051
  1052. 1052 response.setContentType("text/html;charset=utf-8");
  1053. 1053 PrintWriter writer = response.getWriter();
  1054. 1054 writer.print(flag);//将flag返回给前端
  1055. 1055 writer.flush();
  1056. 1056 writer.close();
  1057. 1057
  1058. 1058 }
  1059. 1059 }
  1060. 1060 ```
  1061. 1061
  1062. 1062 3 web.xml
  1063. 1063
  1064. 1064 ```xml
  1065. 1065 <!DOCTYPE web-app PUBLIC
  1066. 1066 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  1067. 1067 "http://java.sun.com/dtd/web-app_2_3.dtd" >
  1068. 1068
  1069. 1069 <web-app
  1070. 1070 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  1071. 1071 xmlns="http://java.sun.com/xml/ns/javaee"
  1072. 1072 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  1073. 1073 id="WebApp_ID" version="3.0"
  1074. 1074 >
  1075. 1075 <display-name>Archetype Created Web Application</display-name>
  1076. 1076
  1077. 1077 <servlet>
  1078. 1078 <servlet-name>AjaxServlet</servlet-name>
  1079. 1079 <servlet-class>com.baidu.servlet.AjaxServlet</servlet-class>
  1080. 1080 </servlet>
  1081. 1081 <servlet-mapping>
  1082. 1082 <servlet-name>AjaxServlet</servlet-name>
  1083. 1083 <url-pattern>/AjaxServlet</url-pattern>
  1084. 1084 </servlet-mapping>
  1085. 1085
  1086. 1086 <servlet>
  1087. 1087 <servlet-name>MyServlet</servlet-name>
  1088. 1088 <servlet-class>com.baidu.servlet.MyServlet</servlet-class>
  1089. 1089 </servlet>
  1090. 1090 <servlet-mapping>
  1091. 1091 <servlet-name>MyServlet</servlet-name>
  1092. 1092 <url-pattern>/MyServlet</url-pattern>
  1093. 1093 </servlet-mapping>
  1094. 1094 </web-app>
  1095. 1095 ```
  1096. 1096
  1097. 1097 # Json格式数据
  1098. 1098
  1099. 1099 ```html
  1100. 1100 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  1101. 1101 <html>
  1102. 1102 <head>
  1103. 1103 <title>Title</title>
  1104. 1104 <script src="statics/js/jquery-3.4.1.js"></script>
  1105. 1105 <script>
  1106. 1106 $(document).ready(function() {
  1107. 1107
  1108. 1108 //1、定义JSON格式的user对象,并在div中输出
  1109. 1109 var user = {
  1110. 1110 "id" : 1,
  1111. 1111 "name" : "张三",
  1112. 1112 "pwd" : "000"
  1113. 1113 };
  1114. 1114 $("#objectDiv").append("ID:" + user.id + "<br>")
  1115. 1115 .append("用户名:" + user.name + "<br>")
  1116. 1116 .append("密码:" + user.pwd + "<br>");
  1117. 1117
  1118. 1118 //2、定义JSON格式的字符串数组,在ul中输出
  1119. 1119 var ary = [ "中", "美", "俄" ];
  1120. 1120 var $ary = $(ary);
  1121. 1121 var $ul = $("#arrayUl"); // 展示数据的ul元素
  1122. 1122 $ary.each(function() {
  1123. 1123 $ul.append("<li>"+this+"</li>");
  1124. 1124 });
  1125. 1125
  1126. 1126 //3、定义JSON格式的字符串数组,在select中输出
  1127. 1127 var $sel = $("#arraySel"); // 展示数据的select元素
  1128. 1128 $ary.each(function(i) {//index 索引
  1129. 1129 /**
  1130. 1130 * <select name=“国籍” id=“arraySel”>
  1131. 1131 * <option value=1>中</option>
  1132. 1132 * <option value=2>美</option>
  1133. 1133 * <option value=3>俄</option>
  1134. 1134 * </select>
  1135. 1135 */
  1136. 1136 //"<option value='"+(i+1)+"'>"
  1137. 1137 $sel.append("<option value='"+(i+1)+"'>"+this+"</option>");
  1138. 1138 });
  1139. 1139
  1140. 1140 //4、定义JSON格式的user对象数组,并使用<table>输出
  1141. 1141 var userArray = [ {
  1142. 1142 "id" : 2,
  1143. 1143 "name" : "admin",
  1144. 1144 "pwd" : "123"
  1145. 1145 }, {
  1146. 1146 "id" : 3,
  1147. 1147 "name" : "詹姆斯",
  1148. 1148 "pwd" : "11111"
  1149. 1149 }, {
  1150. 1150 "id" : 4,
  1151. 1151 "name" : "梅西",
  1152. 1152 "pwd" : "6666"
  1153. 1153 } ];
  1154. 1154 /**<div id="objectArrayDiv">
  1155. 1155 * <table border='1'>
  1156. 1156 * <tr><td>ID</td><td>用户名</td><td>密码</td></tr>
  1157. 1157 * <tr><td>2</td><td>admin</td><td>123</td></tr>
  1158. 1158 * <tr><td>3</td><td>詹姆斯</td><td>11111</td></tr>
  1159. 1159 * <tr><td>4</td><td>梅西</td><td>666</td></tr>
  1160. 1160 * </table>
  1161. 1161 * <div>
  1162. 1162 * 我是中国人我爱中国我爱人民人民爱我
  1163. 1163 */
  1164. 1164 var $table = $("<table border='1'></table>").append(
  1165. 1165 "<tr><td>ID</td><td>用户名</td><td>密码</td></tr>");
  1166. 1166 $(userArray).each(function() {
  1167. 1167 $table.append("<tr><td>" + this.id + "</td><td>"
  1168. 1168 + this.name + "</td><td>"
  1169. 1169 + this.pwd + "</td></tr>");
  1170. 1170 });
  1171. 1171 $("#objectArrayDiv").append($table);
  1172. 1172
  1173. 1173 });
  1174. 1174 </script>
  1175. 1175 </head>
  1176. 1176 <body>
  1177. 1177 <%--div中呈现普通对象--%>
  1178. 1178 <div id="objectDiv"></div>
  1179. 1179 <%--无序列表形式呈现数组--%>
  1180. 1180 <div>
  1181. 1181 <ul id="arrayUl">
  1182. 1182
  1183. 1183 </ul>
  1184. 1184 </div>
  1185. 1185 <%--下拉列表形式呈现数组--%>
  1186. 1186 <div>
  1187. 1187 <select id="arraySel"></select>
  1188. 1188 </div>
  1189. 1189 <%--表格形式呈现对象数组--%>
  1190. 1190 <div id="objectArrayDiv">
  1191. 1191
  1192. 1192 </div>
  1193. 1193 </body>
  1194. 1194 </html>
  1195. 1195 ```
  1196. 1196
  1197. 1197 # SpringMVC参数传递
  1198. 1198
  1199. 1199 ```java
  1200. 1200 @Controller
  1201. 1201 @RequestMapping("/user")
  1202. 1202 public class UserController {
  1203. 1203
  1204. 1204 //private UserService userService=new UserServiceImpl();
  1205. 1205 @Resource //自动装配
  1206. 1206 private UserService userService;
  1207. 1207
  1208. 1208 //无参数的url :localhost:8080/项目名/user/index
  1209. 1209 @RequestMapping("/index")
  1210. 1210 public String index(){
  1211. 1211 return "index";//index就是逻辑视图名
  1212. 1212 }
  1213. 1213 //含参的url:localhost:8080/项目名/user/login?xiaogou=lisi&xiaomao=lisi
  1214. 1214 @RequestMapping(value = "/login")
  1215. 1215 public String login(@RequestParam(value = "xiaogou",required = false) String username,
  1216. 1216 @RequestParam(value = "xiaomao",required = false) String password){
  1217. 1217 boolean flag = this.userService.login(username, password);
  1218. 1218 if(flag){//用户存在
  1219. 1219 return "success";
  1220. 1220 }
  1221. 1221 return "failure";
  1222. 1222 }
  1223. 1223 //含参的url:localhost:8080/项目名/login?xiaogou=lisi&xiaomao=lisi
  1224. 1224 //更精确的含参url:method = RequestMethod.POST
  1225. 1225 @RequestMapping(value = "/login1" ,method = RequestMethod.POST)
  1226. 1226 public String login1(@RequestParam(value = "xiaogou",required = false) String username,
  1227. 1227 @RequestParam(value = "xiaomao",required = false) String password){
  1228. 1228 boolean flag = this.userService.login(username, password);
  1229. 1229 if(flag){//用户存在
  1230. 1230 return "success";
  1231. 1231 }
  1232. 1232 return "failure";
  1233. 1233 }
  1234. 1234
  1235. 1235 //Controller层传递到View
  1236. 1236 //1 ModelAndView 作为返回对象
  1237. 1237 @RequestMapping("/index1")
  1238. 1238 public ModelAndView index1(){
  1239. 1239 User user = new User();
  1240. 1240 user.setId(1);
  1241. 1241 user.setUsername("zhangsan");
  1242. 1242 user.setPassword("123456");
  1243. 1243
  1244. 1244 ModelAndView modelAndView = new ModelAndView();
  1245. 1245 //存放数据
  1246. 1246 modelAndView.addObject("user",user);//key-value
  1247. 1247 //设置逻辑视图名
  1248. 1248 modelAndView.setViewName("nankeyuan");
  1249. 1249 return modelAndView;
  1250. 1250 }
  1251. 1251
  1252. 1252 //Model 作为一个容器,保存数据
  1253. 1253 @RequestMapping("/index2")
  1254. 1254 public String index2(Model model){
  1255. 1255 model.addAttribute("str","hello model");
  1256. 1256 //字符串类型 --key:string
  1257. 1257 model.addAttribute("hello model2");
  1258. 1258 return "nankeyuan";
  1259. 1259 }
  1260. 1260
  1261. 1261 //Map作为容器,保存数据
  1262. 1262 @RequestMapping("/index3")
  1263. 1263 public String index3(Map<String,Object> map){
  1264. 1264 map.put("mapStr","map demo");
  1265. 1265 return "nankeyuan";
  1266. 1266 }
  1267. 1267 }
  1268. 1268
  1269. 1269 ```
  1270. 1270
  1271. 1271 # 字符乱码过滤器
  1272. 1272
  1273. 1273 web.xml
  1274. 1274
  1275. 1275 ```
  1276. 1276 <filter>
  1277. 1277 <filter-name>characterFilter</filter-name>
  1278. 1278 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  1279. 1279 <!-- 设置过滤器中的属性值 -->
  1280. 1280 <init-param>
  1281. 1281 <param-name>encoding</param-name>
  1282. 1282 <param-value>UTF-8</param-value>
  1283. 1283 </init-param>
  1284. 1284 <!-- 启动过滤器 -->
  1285. 1285 <init-param>
  1286. 1286 <param-name>forceEncoding</param-name>
  1287. 1287 <param-value>true</param-value>
  1288. 1288 </init-param>
  1289. 1289 </filter>
  1290. 1290 <filter-mapping>
  1291. 1291 <filter-name>characterFilter</filter-name>
  1292. 1292 <!--表示项目名下的所有url都被拦截过滤-->
  1293. 1293 <url-pattern>/*</url-pattern>
  1294. 1294 </filter-mapping>
  1295. 1295
  1296. 1296 ```
  1297. 1297
  1298. 1298 ## Restful风格路径
  1299. 1299
  1300. 1300 ```java
  1301. 1301 //url中含参传递 restful风格路径
  1302. 1302 //localhost:8080/userPro-ssm/user/index6/lisi 等价(?username=lisi)
  1303. 1303 @RequestMapping("/index6/{username}")
  1304. 1304 public String index6(@PathVariable(name = "username") String username){
  1305. 1305 System.out.println("用户名为"+username);
  1306. 1306 return "nankeyuan";
  1307. 1307 }
  1308. 1308 ```
  1309. 1309
  1310. 1310

小菜鸟之SSM框架的更多相关文章

  1. (SSM框架)实现小程序图片上传(配小程序源码)

    阅读本文约"2分钟" 又是一个开源小组件啦! 因为刚好做到这个小功能,所以就整理了一下,针对微信小程序的图片(文件)上传! 原业务是针对用户反馈的图片上传.(没错,本次还提供小程序 ...

  2. ssm框架与shiro的整合小demo,用idea开发+maven管理

    shiro安全框架是目前为止作为登录注册最常用的框架,因为它十分的强大简单,提供了认证.授权.加密和会话管理等功能 . shiro能做什么? 认证:验证用户的身份 授权:对用户执行访问控制:判断用户是 ...

  3. 菜鸟 ssm 框架的学习之路

    跟着老师学习了两个月的java语言,现在学习到了框架的部分,一直想在博客上写点东西的,只是自己一直没有时间,其实到底也是懒,鲁迅说过:"时间就像海绵里的水,只要愿意去挤还是有的", ...

  4. SSM框架学习之高并发秒杀业务--笔记2-- DAO层

    上节中利用Maven创建了项目,并导入了所有的依赖,这节来进行DAO层的设计与开发 第一步,创建数据库和表. 首先分析业务,这个SSM匡济整合案例是做一个商品的秒杀系统,要存储的有:1.待秒杀的商品的 ...

  5. SSM框架——以注解形式实现事务管理

    上一篇博文<SSM三大框架整合详细教程>详细说了如何整合Spring.SpringMVC和MyBatis这三大框架.但是没有说到如何配置mybatis的事务管理,在编写业务的过程中,会需要 ...

  6. 百度富文本编辑器ueditor在jsp中的使用(ssm框架中的应用)

    折腾了一下午终于把百度富文本编辑器ueditor搞定了!   项目地址:https://github.com/724888/lightnote_new     首先我参考了一个ueditor的demo ...

  7. 整合最优雅SSM框架:SpringMVC + Spring + MyBatis

    我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...

  8. 最优雅SSM框架:SpringMVC + Spring + MyBatis

    在写代码之前我们先了解一下这三个框架分别是干什么的? 相信大以前也看过不少这些概念,我这就用大白话来讲,如果之前有了解过可以跳过这一大段,直接看代码! SpringMVC:它用于web层,相当于con ...

  9. 手把手教你 基础 整合最优雅SSM框架:SpringMVC + Spring

    我们看招聘信息的时候,经常会看到这一点,需要具备SSH框架的技能:而且在大部分教学课堂中,也会把SSH作为最核心的教学内容. 但是,我们在实际应用中发现,SpringMVC可以完全替代Struts,配 ...

随机推荐

  1. P3469 割点的应用

    https://www.luogu.org/problem/P3469 题目就是说封锁一个点,会导致哪些点(对)连不通: 用tarjan求割点,如果这个点是割点,那么不能通行的点对数就是(乘法法则)儿 ...

  2. Qt读写Json格式配置文件

    头文件Config.h #pragma once #include <QVariantMap> class Config { public: Config(const QString &a ...

  3. tomcat发布web项目

    转:https://www.cnblogs.com/skyblue-li/p/7888951.html Tomcat是一种Web服务器,我们自己做好了一个Web项目,就可以通过Tomcat来发布.服务 ...

  4. mysql 使用service mysqld start 提示未识别服务 进入/etc/rc.d/init.d 下面未发现有mysqld解决方法

    1.执行whereis mysql会有如下打印: mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/mysql ...

  5. EntityFramework Core Code First 已有数据库

    问题场景:我已经有一个数据库,想用 EF core Code First,怎么办? 首先,可以参考微软的API文档:通过现有数据库在 ASP.NET Core 上开始使用 EF Core, 这一步可以 ...

  6. vue devtools无法使用

    vue devtools无法使用 一.总结 一句话总结: 没显示vue devtools调试工具的原因是用了生产环境的版本或是压缩的vue版本,或是没有勾选:允许访问文件网址 二.vue调试工具Dev ...

  7. lareval重命名created_at和updated_at字段

    lareval重命名created_at和updated_at字段 一.总结 一句话总结: 要改变created_at和updated_at的名称,模型和数据迁移里面都需要改变 在模型中指定数据类型之 ...

  8. Flutter移动电商实战 --(32)列表页_小类高亮交互效果制作

    点击大类右侧的横向的小类红色显示当前的小类别 解决之前溢出的问题: 先解决一个bug,之前右侧的这里设置的高度是1000,但是有不同的虚拟机和手机设别的问题造成了溢出的问题 Expaned是有伸缩能力 ...

  9. The first one spawns an additional process forwarding requests to a series of workers (think about it as a form of shield, at the same level of apache or nginx), while the second one sets workers to n

    Things to know (best practices and “issues”) READ IT !!! — uWSGI 2.0 documentationhttps://uwsgi-docs ...

  10. UNIX 历史问题 分布式系统的Thundering Herd效应 惊群效应

    https://uwsgi-docs.readthedocs.io/en/latest/articles/SerializingAccept.html One of the historical pr ...