【SpringMvc框架】springmvc框架配置与操作

导读:本篇文章讲解 【SpringMvc框架】springmvc框架配置与操作,文章出自:https://blog.csdn.net/weixin_43316702/article/details/105309153希望对大家有帮助,欢迎收藏,转发!站点地址:www.javazhiyin.com.com

>>强大,10k+点赞的 SpringBoot 后台管理系统竟然出了详细教程!

SpringMvc:
SpringMVC是Web层的框架,是一种基于Java,实现了Web MVC设计模式,Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,每一个角色都可以由一个专门的对象来实现。

传统的MVC模式:
在这里插入图片描述
SpringMvc设计流程:
在这里插入图片描述根据上图流程我们来配置一下:
创建springmvc的配置文件,设置处理器映射器处理器适配器视图解析器

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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交给spring管理 -->
    <context:component-scan base-package="com.springmvc"></context:component-scan>

    <!--
    <mvc:annotation-driven/>默认创建RequestMappingHandlerMapping,RequestMappingHandlerAdapter
    提供对json数据格式支持
    -->
    <!--<mvc:annotation-driven/>-->
    <!-- 配置注解处理器映射器
        功能:负责寻找执行类:Controller
    -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>

    <!-- 配置注解处理器适配器
        功能:执行Controller
    -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <!-- 对json数据格式的支持-->
        <property name="messageConverters">
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
        </property>
    </bean>

    <!-- 配置视图解析器:解析出真正的物理视图
        后台返回逻辑视图:index
        解析出真正的物理视图,前端+逻辑视图+后缀==/WEB-INF/index.jsp(/index.jsp)
    -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

注意:
<mvc:annotation-driven/>默认创建RequestMappingHandlerMapping,RequestMappingHandlerAdapter且
提供对json数据格式支持,即这一个配置可同时创建映射器和适配器。

在web.xml加入配置文件(复制修改路径即可使用)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!-- 处理编码格式 -->
    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- springmvc默认加载springmvc的配置文件,这个需要满足以下规则:
             命名规则:servlet-name-servlet.xml====>springmvc-servlet.xml
             路径规范:springmvc-servlet.xml必须放在WEB-INF下面
        -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:dispatcher.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

User:

package com.springmvc.domain;
import java.util.Date;

public class User {
    private Integer id;
    private String username;
    private Integer age;
    private Date birthday;
    private String address;

    public User(Integer id, String username, Integer age, Date birthday, String address) {
        this.id = id;
        this.username = username;
        this.age = age;
        this.birthday = birthday;
        this.address = address;
    }

    public User() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", age=" + age +
                ", birthday=" + birthday +
                ", address='" + address + '\'' +
                '}';
    }
}

UserController:

@Controller//<bean id="userController" class="UserController路径">
public class UserController {
//接收javaBean参数
    @RequestMapping("recieveUser")
    public String recieveUser(User user){
        System.out.println(user);

        return "success";
    }
}

jsp:

<form action="${pageContext.request.contextPath}/recieveUser.do" method="get" >
    姓名<input type="text" name="username" id="username">
    年龄<input type="text" name="age" id="age">
    生日<input type="text" name="birthday" id="birthday">
    地址<input type="text" name="address" id="address">
    <input type="submit"  value="提交">
</form>

操作截图:
在这里插入图片描述在这里插入图片描述谢谢阅读。