SSM17.3【SSM框架整合--Spring+SpringMVC】

搭建和测试SpringMVC的开发环境

在web.xml中配置DispatcherServlet前端控制器和CharacterEncodingFilter编码过滤器

复制代码
 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app>
 6   <display-name>Archetype Created Web Application</display-name>
 7 
 8   <!--配置springMVC的前端控制器,实质就是一个servlet-->
 9   <servlet>
10     <servlet-name>dispatcherServlet</servlet-name>
11     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
12     <!--加载springMVC.xml配置文件-->
13     <init-param>
14       <param-name>contextConfigLocation</param-name>
15       <param-value>classpath:springMVC.xml</param-value>
16     </init-param>
17     <!--期望启动服务器,就创建该servlet-->
18     <load-on-startup>1</load-on-startup>
19   </servlet>
20   <servlet-mapping>
21     <servlet-name>dispatcherServlet</servlet-name>
22     <url-pattern>/</url-pattern>
23   </servlet-mapping>
24 
25   <!--配置springMVC中解决中文乱码的过滤器-->
26   <filter>
27     <filter-name>characterEncodingFilter</filter-name>
28     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
29     <!--指定编码集-->
30     <init-param>
31       <param-name>encoding</param-name>
32       <param-value>UTF-8</param-value>
33     </init-param>
34   </filter>
35   <filter-mapping>
36     <filter-name>characterEncodingFilter</filter-name>
37     <url-pattern>/*</url-pattern>
38   </filter-mapping>
39 
40 </web-app>
复制代码

创建和编写springMVC.xml配置文件

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:mvc="http://www.springframework.org/schema/mvc"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 6        xsi:schemaLocation="
 7         http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/mvc
10         http://www.springframework.org/schema/mvc/spring-mvc.xsd
11         http://www.springframework.org/schema/context
12         http://www.springframework.org/schema/context/spring-context.xsd
13 ">
14 
15     <!--开启注解扫描,只扫描Controller注解-->
16     <context:component-scan base-package="com.haifei">
17         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
18     </context:component-scan>
19 
20     <!--配置视图解析器对象,实质就是一个bean-->
21     <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22         <property name="prefix" value="/WEB-INF/pages/"/>
23         <property name="suffix" value=".jsp"/>
24     </bean>
25 
26     <!--过滤静态资源-->
27     <mvc:resources location="/css/" mapping="/css/**" />
28     <mvc:resources location="/images/" mapping="/images/**" />
29     <mvc:resources location="/js/" mapping="/js/**" />
30 
31     <!--开启springMVC的注解支持-->
32     <mvc:annotation-driven/>
33     
34 </beans>
复制代码

测试SpringMVC框架是否搭建成功

复制代码
 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <title>index.jsp</title>
 5 </head>
 6 <body>
 7     <h2>Hello World!</h2>
 8     <a href="account/findAll">springMVC测试</a>
 9 </body>
10 </html>
复制代码
复制代码
1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
2 <html>
3 <head>
4     <title>list.jsp</title>
5 </head>
6 <body>
7     <h2>查询到所有账户信息,在此页面展示</h2>
8 </body>
9 </html>
复制代码
复制代码
 1 package com.haifei.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 /**
 7  * web层-账户
 8  */
 9 @Controller
10 @RequestMapping("/account")
11 public class AccountController {
12 
13     @RequestMapping("/findAll")
14     public String findAll(){
15         System.out.println("表现层:查询所有账户信息");
16         return "list";
17     }
18 
19 }
复制代码

 

 

 

 

 

 

 

 

Spring整合SpringMVC

 

 

 

 

 

 

 

复制代码
 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app>
 6   <display-name>Archetype Created Web Application</display-name>
 7 
 8   <!--配置Spring的监听器;默认只加载WEB-INF目录下的applicationContext.xml配置文件,所以需要设置配置文件的路径-->
 9   <listener>
10     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
11   </listener>
12   <!--设置配置文件的路径-->
13   <context-param>
14     <param-name>contextConfigLocation</param-name>
15     <param-value>classpath:applicationContext.xml</param-value>
16   </context-param>
17 
18 
19   <!--配置springMVC的前端控制器,实质就是一个servlet-->
20   <servlet>
21     <servlet-name>dispatcherServlet</servlet-name>
22     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
23     <!--加载springMVC.xml配置文件-->
24     <init-param>
25       <param-name>contextConfigLocation</param-name>
26       <param-value>classpath:springMVC.xml</param-value>
27     </init-param>
28     <!--期望启动服务器,就创建该servlet-->
29     <load-on-startup>1</load-on-startup>
30   </servlet>
31   <servlet-mapping>
32     <servlet-name>dispatcherServlet</servlet-name>
33     <url-pattern>/</url-pattern>
34   </servlet-mapping>
35 
36   <!--配置springMVC中解决中文乱码的过滤器-->
37   <filter>
38     <filter-name>characterEncodingFilter</filter-name>
39     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
40     <!--指定编码集-->
41     <init-param>
42       <param-name>encoding</param-name>
43       <param-value>UTF-8</param-value>
44     </init-param>
45   </filter>
46   <filter-mapping>
47     <filter-name>characterEncodingFilter</filter-name>
48     <url-pattern>/*</url-pattern>
49   </filter-mapping>
50 
51 </web-app>
复制代码
复制代码
 1 package com.haifei.controller;
 2 
 3 import com.haifei.service.AccountService;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.stereotype.Controller;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 
 8 /**
 9  * web层-账户
10  */
11 @Controller
12 @RequestMapping("/account")
13 public class AccountController {
14 
15     @Autowired //根据类型进行自动注入
16     private AccountService accountService;
17 
18 
19     @RequestMapping("/findAll")
20     public String findAll(){
21         System.out.println("表现层:查询所有账户信息");
22         accountService.findAll();
23         return "list";
24     }
25 
26 }
复制代码

 

 

 

posted @   yub4by  阅读(60)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示