【SSM整合】2—Spring和SpringMVC整合

⭐⭐⭐⭐⭐⭐
Github主页https://github.com/A-BigTree
笔记链接https://github.com/A-BigTree/Code_Learning
⭐⭐⭐⭐⭐⭐


Spring专栏https://blog.csdn.net/weixin_53580595/category_12279588.html

SpringMVC专栏https://blog.csdn.net/weixin_53580595/category_12281721.html

Mybatis专栏https://blog.csdn.net/weixin_53580595/category_12279566.html

如果可以,麻烦各位看官顺手点个star~

如果文章对你有所帮助,可以点赞收藏⭐支持一下博主~


文章目录

  • 2 Spring和SpringMVC整合
    • 2.1 配置`web.xml`
    • 2.2 配置SpringMVC
    • 2.3 显示数据列表
      • 2.3.1 显示首页
        • 声明view-controller
        • 创建页面
      • 2.3.2 显示数据列表
        • 创建处理类
        • 页面显示

2 Spring和SpringMVC整合

2.1 配置web.xml



<context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:spring-persist.xmlparam-value>
context-param>
 

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
 

<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>
    <load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
servlet-mapping>
 


<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>
    <init-param>
        <param-name>forceRequestEncodingparam-name>
        <param-value>trueparam-value>
    init-param>
    <init-param>
        <param-name>forceResponseEncodingparam-name>
        <param-value>trueparam-value>
    init-param>
filter>
<filter-mapping>
    <filter-name>characterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>
 

<filter>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
<filter-mapping>
    <filter-name>hiddenHttpMethodFilterfilter-name>
    <url-pattern>/*url-pattern>
filter-mapping>

2.2 配置SpringMVC



<context:component-scan base-package="com.atguigu.ssm.handler"/>
 

<bean id="thymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    <property name="order" value="1"/>
    <property name="characterEncoding" value="UTF-8"/>
    <property name="templateEngine">
        <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
            <property name="templateResolver">
                <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                    <property name="prefix" value="/WEB-INF/templates/"/>
                    <property name="suffix" value=".html"/>
                    <property name="characterEncoding" value="UTF-8"/>
                    <property name="templateMode" value="HTML5"/>
                bean>
            property>
        bean>
    property>
bean>
 

<mvc:annotation-driven/>
 

<mvc:default-servlet-handler/>
 

<mvc:view-controller path="/" view-name="portal"/>
<mvc:view-controller path="/index.html" view-name="portal"/>

2.3 显示数据列表

2.3.1 显示首页

声明view-controller


<mvc:view-controller path="/" view-name="portal"/>
<mvc:view-controller path="/index.html" view-name="portal"/>

创建页面

DOCTYPE html>
<html lang="en" xml:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
head>
<body>
	<a th:href="@{/get/all}">显示全部数据a>
body>
html>

2.3.2 显示数据列表

创建处理类

@Controller
public class EmpHandler {
 
    @Autowired
    private EmpService empService;
     
    @RequestMapping("/get/all")
    public String getAll(Model model) {
 
        // 1、查询数据
        List<Emp> empList = empService.getAll();
         
        // 2.存入模型
        model.addAttribute("empList", empList);
 
        return "emp-list";
    }
 
}

页面显示

<table>
    <tr>
        <th>IDth>
        <th>NAMEth>
        <th>SALARYth>
    tr>
    <tbody th:if="${#lists.isEmpty(empList)}">
        <tr>
            <td colspan="3">抱歉!没有查询到数据!td>
        tr>
    tbody>
    <tbody th:if="${not #lists.isEmpty(empList)}">
        <tr th:each="emp : ${empList}">
            <td th:text="${emp.empId}">这里显示员工IDtd>
            <td th:text="${emp.empName}">这里显示员工NAMEtd>
            <td th:text="${emp.empSalary}">这里显示员工SALARYtd>
        tr>
    tbody>
table>
 
<a th:href="@{/}">回首页a>

你可能感兴趣的:(微服务,#SSM,spring,java,mybatis)