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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 微服务架构-实现技术之具体实现工具与框架3:Spring Cloud概述和基本讲解

    微服务架构-实现技术之具体实现工具与框架3:Spring Cloud概述和基本讲解目录一、基本定义二、SpringCloud相关组件成员框架SpringCloudEurekaSpringCloudRibbonSpringCloudFeignSpringCloudHytrixSpringCloudZuulSpringCloudGatewaySpringCloudConfigSpringCloudAdmin…

    2022年4月29日
    31
  • shiro面试必问_面试过程中的问题与对策

    shiro面试必问_面试过程中的问题与对策一,什么是Shiro?Shiro是apache旗下一个开源安全框架,它将软件的安全认证相关的功能抽取出来,实现用户身份授权,加密,会话管理等功能,组成了一个通用的安全认证框架二,目前市场主流的安全框架有哪些?(1),shiro:轻量级的,使用方便,灵活,是apache旗下一个开源安全框架,适合任何框架(2),SpringSeurity:是spring家族的一部分很多项目会使用到Spring全家桶,相对shiro来说SpringSeurity更轻量,必须要求spring环境,相对shiro

    2022年10月10日
    0
  • 哈希表(散列表)原理详解

    哈希表(散列表)原理详解什么是哈希表?哈希表(Hashtable,也叫散列表),是根据关键码值(Keyvalue)而直接进行访问的数据结构。也就是说,它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。记录的存储位置=f(关键字)这里的对应关系f称为散列函数,又称为哈希(Hash函数),采用散列技术将记录存储在一块连续的存储空间中,这块…

    2022年5月15日
    50
  • Apache安装

    Apache安装

    2022年4月2日
    37
  • idea查看接口的实现类快捷键_idea查看方法在哪里被调用快捷键

    idea查看接口的实现类快捷键_idea查看方法在哪里被调用快捷键Ctrl+B快速打开光标处的类或方法Ctrl+Alt+B查看接口的实现类Ctrl+Shift+B跳转到类型声明处

    2022年8月15日
    7
  • python中的imread函数_python open函数参数

    python中的imread函数_python open函数参数cv2.imread()除了最常用的路径参数之外,第二个参数也至关重要:imread(conststring&filename,intflag=1)filename:需要打开图片的路径,可以是绝对路径或者相对路径,路径中不能出现中文。flag:图像的通道和色彩信息(默认值为1)。flag=-1,8位深度,原通道flag=0,8位深度,1通道f…

    2022年9月25日
    0

发表回复

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

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