Java学习之Spring MVC入门

Java学习之SpringMVC入门0x00前言前面写了SSM的两大框架,分别是Mybatis和Spring,这里来写一下SpringMVC框架的相关内容。0x01SpringMVC

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

Java学习之Spring MVC入门

0x00 前言

前面写了SSM 的两大框架,分别是Mybatis和Spring,这里来写一下Spring MVC框架的相关内容。

0x01 Spring MVC概述

  1. 是一种基于Java实现的MVC设计模型的请求驱动类型的轻量级WEB框架。

  2. Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring 框架提供
    了构建 Web 应用程序的全功能 MVC 模块。

  3. 使用 Spring 可插入的 MVC 架构,从而在使用Spring进行WEB开发时,可以选择使用Spring的
    SpringMVC框架或集成其他MVC开发框架,如Struts1(现在一般不用),Struts2等。

0x02 Spring MVC 代码实现

配置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">
    <servlet>
<!--设置前端控制器名字-->
        <servlet-name>dispatcherServlet</servlet-name>
<!--        设置前端控制器引用-->
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--        设置默认加载spring 配置文件-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        
    </servlet>
<!--    使用dispatcherServlet 并设置拦截路径-->
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

这里设置了读取sprng mvc.xml的文件,我们还需要创建一个spring mvc.xml文件,然后对其进行配置

配置spring mvc.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="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">
<!--    注解扫描-->
<context:component-scan base-package="com.test">

</context:component-scan>
<!--配置视图解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
<!--配置spring开启注解mvc支持-->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

使用context:component-scan开启注解扫描,后面就可以直接使用注解的方式,将类加载到容器里面了。

colltroller类:

package com.test.domain;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloContraoller {
    @RequestMapping(path = "/hello")
    public String sayHello(){
        System.out.println("Hello spring mvc");
        return "success";
    }
}

@Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象。分发处理器将会扫描使用了该注解的类的方法。

@RequestMapping 注解方法表示如果该注解指定的路径将会执行被注解的方法。

RequestMapping的属性:

1. path 指定请求路径的url
2. value value属性和path属性是一样的
3. mthod 指定该方法的请求方式
4. params 指定限制请求参数的条件
5. headers 发送的请求中必须包含的请求头

后面就可以编写jsp页面来测试了。

index.jsp页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<a href="/hello">入门</a>

</body>
</html>

设置请求方式

我们还可以在@RequestMapping注解中指定请求方式。

@Controller
public class HelloContraoller {
    @RequestMapping(path = "/hello",method = RequestMethod.POST)
    public String sayHello(){
        System.out.println("Hello spring mvc");
        return "success";
    }
}

在注解中加入method属性,指定为post属性,就可以了。
如果使用get的方式去请求,会发现请求会失败。

设置请求参数

在RequestMapping注解里面添加params属性指定参数。

@Controller
public class HelloContraoller {
    @RequestMapping(path = "/hello",params = {"username"})
    public String sayHello(){
        System.out.println("Hello spring mvc");
        return "success";
    }
}

如果没有参数,则不执行该方法。

参数绑定

方法中添加参数值,在提交参数的时候,mvc框架会帮我们拿到该值并传入到方法里面。

@Controller
public class HelloContraoller {
    @RequestMapping(path = "/hello",params = {"username","password"})
    public String sayHello(String username,String password){
        System.out.println("username"+username);
        System.out.println("password"+password);
        return "success";
    }
}

参数绑定实体类

定义一个接收参数的实体类


public class Person {
    private String username;
    private  String password;

    public String getUsername() {
        return username;
    }

    @Override
    public String toString() {
        return "Person{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

contraoller类:

   }
    @RequestMapping(path = "/tijiao",params = {"username","password"},method = RequestMethod.POST)
    public String submit(Person person){
        System.out.println(person);

        return "success";
    }

这里只需要设置传入参数为定义的实体类就行。

submit页面:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<form action="/tijiao" method="post" >
    账户:<input type="text" name="username">
    密码: <input type="password" name="password">
    <input type="submit" name="提交">

</form>

</body>
</html>

jsp页面里面表单提交的的名字要和实体类的成员变量名一样,不然无法自动封装。

0x03 结尾

Spring mvc其实比前面的都简单不少,但是在xml的配置上会麻烦一些。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/119928.html原文链接:https://javaforall.net

(0)
上一篇 2021年12月12日 下午12:00
下一篇 2021年12月12日 下午1:00


相关推荐

  • Android多线程开发之Callback

    Android多线程开发之Callback多线程中的返回 Callback 回调 interface

    2026年3月20日
    2
  • SWOT分析和PEST分析[通俗易懂]

    SWOT分析和PEST分析[通俗易懂]1.SWOT分析定义:SWOT(StrengthsWeaknessOpportunityThreats)分析法,又称态势分析法或优劣势分析法,用来确定企业自身的竞争优势(strength)、竞争劣势(weakness)、机会(opportunity)和威胁(threat),从而将公司的战略与公司内部资源、外部环境有机地结合起来。如何用:首先要明白使用SWOT的目的,为什么要用它,用它的目的

    2022年6月14日
    66
  • mybatis动态SQL常用的标签

    mybatis动态SQL常用的标签在使用 sql 片段时使用 include 标签通过 sql 片段的 id 进行引用 sql 片段的 id 在当前空间是唯一的 sql 片段中也可以写其他的内容 只要符合语法规范都是可以的

    2026年3月17日
    2
  • realsense深度图像保存方法

    realsense深度图像保存方法一般使用realsense时会保存视频序列,当保存深度图像时,需要注意保存的图像矩阵的格式,不然可能造成深度值的丢失。在众多图像库中,一般会使用opencv中的imwrite()函数进行深度图像的保存。一般深度图像中深度值的单位是mm,因此一般使用np.uint16作为最终数据格式保存。例子:importnumpyasnpimportcv2deffun1(…

    2022年4月25日
    169
  • 万字长文:被阿里收购有多好?凉了有多少?

    万字长文:被阿里收购有多好?凉了有多少?本文由作者盐汽水发布于社区前段时间虾米音乐正式关停 引发了诸多网友对于阿里收购公司的质疑 有人总结了公司被阿里收购之后的 7 个标配步骤 如下图 并表示他们公司被收购之后 目前走到了第五步

    2026年3月18日
    2
  • c4d怎么导入fbx文件(c4d怎么导入fbx)

    如何将3DMAX模型带材质导入C4D有很多人都在问,苑长怎么将网站上3DMAX文件的模型使用在C4D里面呢,C4D外文名CINEMA4D,也是很多三维爱好者经常使用的软件,我们今天就教大家如何将3DMAX文件连带材质贴图导入C4D里面。1.首先我们在转换之前下载一个MaxToC4D这样的插件,可以把MAX文件转换成FBX的格式。2.MaxToC4D安装——步骤如下:①将To3DsM…

    2022年4月16日
    1.1K

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号