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


相关推荐

  • Graphics2D绘制多图片水印方法

    /***@paramsrcPath需要添加水印的完整地址*@paramids需要添加的水印的id集合,结果以”,”分隔*@return返回包含水印图片的输入流*@throwsException*/publicInputStreamaddWatermark(StringsrcPath,Str…

    2022年4月13日
    47
  • 博客备份小工具3「建议收藏」

    博客备份小工具3「建议收藏」接着博客转发小工具2,又弄了一个第三版。主要功能有:博客备份到本地、浏览备份到本地的博客、关键字搜索本地的博客和转发博客可以选择个人分类填写Tag标签。其实想了想,转发博客干嘛非要在本地客户端转发,

    2022年7月3日
    21
  • 10 种机器学习算法的要点(附 Python 和 R 代码)「建议收藏」

    10 种机器学习算法的要点(附 Python 和 R 代码)「建议收藏」前言谷歌董事长施密特曾说过:虽然谷歌的无人驾驶汽车和机器人受到了许多媒体关注,但是这家公司真正的未来在于机器学习,一种让计算机更聪明、更个性化的技术。也许我们生活在人类历史上最关键的时期:从使用大

    2022年7月6日
    24
  • 各种门平面图画法_关于CAD各种门怎么画平面图就行?[通俗易懂]

    各种门平面图画法_关于CAD各种门怎么画平面图就行?[通俗易懂]回答:CAD怎么画钢琴平面图CAD怎么画出钢琴的平面图呢?很简单的,有需要的朋友动手试试吧。1、启动中望CAD软件,执行“矩形”命令(rec),绘制1575mmX230mm和1575X50mm的直角矩形。2、执行矩形命令和移动命令(M),绘制出如图所示图形。3、执行移动命令,按[F8]键打开“正交”模式,捕捉上一步绘制的矩形中点,将二部分直角矩形图形组合在一起。4、执行“矩形”命令(rec),绘制…

    2022年5月25日
    59
  • Java设计模式之策略模式「建议收藏」

    本文属于23种设计模式系列。 介绍的是策略模式。

    2022年3月11日
    34
  • 风云私服架设教程_风云端游

    风云私服架设教程_风云端游需要软件:SQLsever2005pro本站提供下载将服务器端解压缩到D:盘D:\Sagasevr下有6个文件夹1:databaseSQL数据库文件夹2:LoginServer帐号登陆管理器3:WorldServer服务器数据管理器4:GameServer1游戏数据管理器4:GameServer2游戏数据管理器5:GameServer3游戏数据管理器6:GameS…

    2022年10月6日
    3

发表回复

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

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