栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringMVC学习(一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringMVC学习(一)

什么是SpringMVC

基于MVC开发模式的框架,用来优化控制器,他是Spring家族的一员,具有IOC和AOP。

MVC是一种开发模式,它是模型视图控制器的简称,所有的web应用都是基于MVC开发

M:模型层,包含实体类,业务逻辑层,数据访问层

V:视图层,html,javaScript,vue等都是视图层,用来显示数据

C:控制器,它是用来接收客户端的请求,并返回响应到客户端的组件,Servlet就是组件

SpringMVC框架的优点

1.轻量级,基于MVC的框架

2.易于上手,易理解,功能强

3.具备IOC,AOP

4.完全基于注解开发

基于注解的SpringMVC框架开发的步骤

1.新建项目,选择webapp模板

2.修改目录

3.修改pom.xml文件,添加SpringMVC的依赖,添加Servlet的依赖




  4.0.0

  org.example
  SpringMVC_001
  1.0-SNAPSHOT
  war

  SpringMVC_001 Maven Webapp
  

  
    UTF-8
    1.8
    1.8
  

  
    
      junit
      junit
      4.11
      test
    

      
          org.springframework
          spring-webmvc
          5.2.5.RELEASE
      

      
          javax.servlet
          javax.servlet-api
          3.1.0
      
  

    
        
            
                src/main/java
                
                    ***.xml
                
                false
            
        
        
            
                maven-compiler-plugin
                3.1
                
                    1.8
                    1.8
                
            
        
    

4.添加Springmvc.xml配置文件,指定包扫描,添加视图解析器





    


    

        
        

        
        

    

5.删除web.xml,新建web.xml

删除后新建

 

创建后,需要再原有的名字基础上做一些改动,否则无法创建,例如web1.xml

创建好后在项目中找到文件并改名为web.xml

6.在web.xml文件中注册SpringMVC框架(所有的web请求都是基于servlet的)







    
        springmvc
        org.springframework.web.servlet.DispatcherServlet

        
            contextConfigLocation
            classpath:springmvc.xml
        
    
    
        springmvc

        *.action
    

分析web请求流程

index.jap(页面)<------------------>DispatcherServlet(核心处理器)<------------------->SpringMVC的处理器是一个普通方法

7.删除index.jsp页面,并新建,发送请求给服务器

<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 2022-06-24
  Time: 14:28
  To change this template use File | Settings | File Templates.
--%>

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


    Title





访问服务器

8.开发控制器(Servlet),是一个普通的类

package com.ys.controller;

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

@Controller //交给Spring创建对象
public class DemoAction {
//    action中所有的功能实现都是由方法来完成的
    
    @RequestMapping("/demo")//此名称需要和index.jsp中的对应的访问服务器的页面名称相同
    public String demo(){
        System.out.println("服务器被访问到了");
        return "admin";//直接跳转到/admin/admin.jsp
    }
}

9.添加Tomcat进行测试

 

 

详解@RequestMapping注解----映射服务器访问的路径

1.此注解可以加在方法上,是为此方法注册一个访问的名称(路径)

2.可以加在类上,相当于包名(虚拟路径),用于区分不同的类中相同action的名称,所加的前缀

一:

@RequestMapping("/A")
public class Demo1{
 @RequestMapping("/demo")
 public String Student(){
   }
}

二:
@RequestMapping("/B")
public class Demo2{
 @RequestMapping("/demo")
 public String School()
   }
}

3.此注解可以区分get请求和post请求

<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 2022-06-25
  Time: 16:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title





测试不同请求提交方式
package com.ys.controller;

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

@Controller
public class ReqAction {
    @RequestMapping(value = "/req",method = RequestMethod.GET)
    public String demo(){
        System.out.println("服务器被访问了,get请求");
        return "main";
    }

    @RequestMapping(value = "/req",method = RequestMethod.POST)
    public String demo2(){
        System.out.println("服务器被访问了,post请求");
        return "main";
    }

}
五种数据提交方式

注意:数据提交为中文时,当请求为GET时,且Tomcat版本处于7以上则会自动解析中文,即不会乱码,当请求为POST时则会乱码

1.单个数据提交

index.jsp

<%--
  Created by IntelliJ IDEA.
  User: HP
  Date: 2022-06-25
  Time: 16:06
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title





1.单个数据提交
姓名:
年龄:

 action.java

package com.ys.controller;

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


@Controller
public class ReqAction {
  @RequestMapping("/one")
    public String demo(String myname,int age){//方法参数名称与index.jsp中提交参数的名称相同
      System.out.println("myname="+myname+",age="+age);
      return "main";
  }

}
2.对象封装提交数据

在提交请求中,保证请求参数的名称与实体类成员变量的名称一致,则可以自动提交数据,自动转,自动封装数据到对象中。

创建实体类Uesrs

package com.ys.controller;

public class Uesrs {
    private String twoname;
    private int twoage;

    public Uesrs(String twoname, int twoage) {
        this.twoname = twoname;
        this.twoage = twoage;
    }

    @Override
    public String toString() {
        return "Uesrs{" +
                "twoname='" + twoname + ''' +
                ", twoage=" + twoage +
                '}';
    }

    public String getTwoname() {
        return twoname;
    }

    public void setTwoname(String twoname) {
        this.twoname = twoname;
    }

    public int getTwoage() {
        return twoage;
    }

    public void setTwoage(int twoage) {
        this.twoage = twoage;
    }

    public Uesrs() {
    }
}

在index.jsp中添加

2.对象封装数据提交
姓名:
<%-- 需要与实体类定义的名称相同--%> 年龄:

在action中添加

 @RequestMapping("/two")
    public String demo2(Uesrs uesrs) {//方法参数名称与实体类相同
        System.out.println(uesrs);
        return "main";
    }

3.动态占位符

仅限于超链接或地址栏提交数据,它是一杠一值,一杠一大括号,使用注解@PathVariable来解析。

注解用于action中的方法参数前  有几个参数就有几个注解

index.jsp中添加


3.动态占位符提交数据 动态提交

action中添加

 @RequestMapping("/three/{name}/{age}")//括号里的参数名自定义(路径中的名称)
    public String demo3(@PathVariable//路径中的变量取值注入给形参
    String name, @PathVariable int age) {//方法参数名称与自定义的路径中的名称相同,否则需要再@PathVariable后加上路径名称
        //如:@PathVariable("name")
        System.out.println("myname=" + name + ",age=" + age);
        return "main";
    }
4.映射名称不一样数据提交

提交请求参数与action方法的形参的名称不一致,使用注解@RequestParam来解析

index.jsp中添加


4.参数名称不一致数据提交
姓名:
年龄:

action中添加

@RequestMapping("/four")
    public String demo4(
            @RequestParam("name") String username, @RequestParam("age")int userage) {//与index.jsp对应的参数名称不同,无法进行单一数据传参
            //注解@RequestParam用于方法参数前,后面跟括号,里面的值为,注入的参数值
//        @RequestParam("name")
        System.out.println("myname=" + username + ",age=" + userage);
        return "main";
    }

5.手动提取数据(作为了解)

index.jsp中添加


5.手工提取数据
姓名:
年龄:

action中添加

  @RequestMapping("/five")
    public String demo5(HttpServletRequest request){
        String username=request.getParameter("name");
        String userage=request.getParameter("age");
        System.out.println("myname=" + username + ",age=" + userage);
        return "main";
    }
请求参数中的中文乱码解决

在web.xml中添加


    
        encode
        org.springframework.web.filter.CharacterEncodingFilter

        
        
            encoding
            UTF-8
        
        
            forceRequestEncoding
            true
        
        
            forceResponseEncoding
            true
        
    
    
        encode
        /*

    

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995839.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号