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年8月4日
    62
  • Xperf Basics: Recording a Trace (the easy way)(转)

    Xperf Basics: Recording a Trace (the easy way)(转) http://randomascii.wordpress.com/2013/04/20/xperf-basics-recording-a-trace-the-easy-way/ SometimeagoIwrotealonganddetailedpostabouthowtorecordtracesusingxperf.Thestepsneeded…

    2022年5月30日
    42
  • 【编解码】从零开始写H264解码器(1) 总纲

    【编解码】从零开始写H264解码器(1) 总纲关于编解码的原理的书和文章自己一直在看,从入门到略懂,但是感觉有些零零碎碎,或不完整,似乎串不成体系。有些小功能,知道是知道,并不知道它的意义和作用,时间一长也会慢慢忘记。反思了一下,或许很多东西,还是需要自己动手做一遍,会理解的更深更透彻一些,就像费曼学习法,你能讲出来,才说明懂了,这个也一样,你能把功能实现出来,才说明你真的明白了里面的流程和逻辑。

    2022年6月22日
    62
  • js全局替换回车换行符

    js全局替换回车换行符踩了个坑,记录一下。全局换行符是这样用php加上的因为显示的时候需要换行显示但是保存的时候不能把回车换行符保存进数据库呀,所以在保存之前要再次把回车换行符替换没了,发现用js替换\r\n无效,思考了一下,可能是html显示是自动过滤了\r,而以\n来显示吧。于是把替换代码改成:varemialStr=$(“#mail”).val();emialStr=emialStr.r

    2022年5月23日
    130
  • PHP导入excel数据到MYSQL

    这里介绍一个直接将excel文件导入mysql的例子。我花了一晚上的时间测试,无论导入简繁体都不会出现乱码,非常好用。PHP-ExcelReader,下载地址:http://sourceforge.

    2021年12月24日
    61
  • java 图片base64 转 pdf

    java 图片base64 转 pdf思路 base64 gt multipartFil gt pdfbase64 gt multipartFil 将图片 base64 gt MultipartFil parambase64 return publicstatic Stringbase64 try String baseStr base64 spli

    2025年6月24日
    5

发表回复

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

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