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

SpringMvc学习--注解式开发

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

SpringMvc学习--注解式开发

springmvc注解开发 @RequestMapping定义请求规则

此注解就是来映射服务器访问的路径
此注解可加在方法上,是为此方法注册一个可以访问的名称(路径)

package com.peihj.controller;

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

@Controller // 交给Spring去创建对象
public class DemoAction2 {
    
    @RequestMapping("demo.action")
    public String demo(){
        System.out.println("user服务器被访问到了.............");
        return "main";  // 可以直接跳到/admin/main.jsp页面上
    }

}


  访问服务器

此注解可以加在类上,相当于是包名(虚拟路径),区分不同类中相同的action的名称。

package com.peihj.controller;

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

@Controller // 交给Spring去创建对象
@RequestMapping("user")
public class DemoAction2 {
    
    @RequestMapping("demo.action")
    public String demo(){
        System.out.println("user服务器被访问到了.............");
        return "main";  // 可以直接跳到/admin/main.jsp页面上
    }
}

  访问服务器

此注解可区分get请求和post请求

@Controller
	public class ReqAction {
	    @RequestMapping(value = "/req",method = RequestMethod.GET)
	    public String req(){
	        System.out.println("我是处理get请求的........");
	        return "main";
	    }
	    @RequestMapping(value = "/req" ,method = RequestMethod.POST)
	    public String req1(){
	        System.out.println("我是处理post请求的........");
	        return "main";
	    }
	}
五种数据提交方式的优化

我们首先建立一个工程文件,具体流程见:https://blog.csdn.net/weixin_45522528/article/details/124672914

pom文件




  4.0.0

  com.peihj
  springmvc-002-datasubmit
  1.0
  war

  
    UTF-8
    1.8
    1.8
  

  
    
      junit
      junit
      4.11
      test
    
    
    
      org.springframework
      spring-webmvc
      5.3.18
    
    
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    
    
      javax.servlet.jsp
      jsp-api
      2.2
      provided
    
    
      jstl
      jstl
      1.2
    
    
      taglibs
      standard
      1.1.2
    
  

  
    
      
        src/main/java
        
          ***.xml
        
        false
      

      
        src/main/resources
        
          ***.xml
        
        false
      
    

    
      
        org.apache.tomcat.maven
        tomcat7-maven-plugin
        2.2
        
          7070
          UTF-8
        
      
    
  



Springmvc编写





    
    
    
    
        
        
    


创建admin包文件以及main.jsp文件夹,删除重新创建index.jsp文件,删除并重新编写web.xml文件



   
    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc.xml
        
    
    
        springmvc
        *.action
    

五种数据注入
<%--
  Created by IntelliJ IDEA.
  User: peihj
  Date: 2022/5/9
  Time: 14:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title





测试不同的请求提交方式




1.单个数据的提交

姓名:
年龄:
<%-- private String name; private int age; 保证名称一致以便于自动注入 --%>

2.对象封装提交

姓名:
年龄:

3.动态占位符提交

动态提交


4.参数名称不一致解决方案>

姓名:
年龄:

5.手工提取数据>

姓名:
年龄:
package com.peihj.controller;

import com.peihj.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class DataSubmitAction {
    @RequestMapping("/one.action")
    public String one(String myname,int age){
        System.out.println("myname="+myname+",age="+(age+0));
        return "main";
    }

    @RequestMapping("/two.action")
    public String two(Users u){
        System.out.println(u);
        return "main";
    }

    @RequestMapping("/three/{uname}/{uage}.action")
    public String three(@PathVariable("uname") String name, @PathVariable("uage") int age){
        System.out.println("myname="+name+",age="+(age+0));
        return "main";
    }

    @RequestMapping("four.action")
    public String four(@RequestParam("name") String uname,@RequestParam("age") int uage){
        System.out.println("myname="+uname+",age="+(uage+0));
        return "main";
    }

    @RequestMapping("five.action")
    public String five(HttpServletRequest httpServletRequest){
        String name = httpServletRequest.getParameter("name");
        Integer age = Integer.parseInt(httpServletRequest.getParameter("age"));
        System.out.println(name);
        System.out.println(age);
        return "main";
    }
}

单个提交数据

在方法中声明一个和表单提交的参数名称相同的参数,由框架按照名称直接注入,只要表单name属性的值与action方法参数的名称一致就可以实现自动注入

对象封装注入

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

首先我们先创建对象类

package com.peihj.pojo;

public class Users {
    private String name;
    private int age;

    public Users() {
    }

    public Users(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Users{" +
                "name='" + name + ''' +
                ", age=" + age +
                '}';
    }
}


动态占位符提交(仅用于超链接)

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


请求参数名称与形参名称不一致

请求与形参中的名字不对应,可以使用
@RequestParam(value=“name1”,required=true)String namea来进行参数名称绑定


手工提取数据


中文乱码解决方案 配置过滤器

过滤器代码


    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
        
        
            encoding
            UTF-8
        
        
            forceRequestEncoding
            true
        
        
            forceResponseEncoding
            true
        
    
    
        characterEncodingFilter
        /*
    

完整



    
    
    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
        
        
            encoding
            UTF-8
        
        
            forceRequestEncoding
            true
        
        
            forceResponseEncoding
            true
        
    
    
        characterEncodingFilter
        /*
    


    
    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc.xml
        
    
    
        springmvc
        *.action
    

参考

https://www.bilibili.com/video/BV1oP4y1K7QT?p=25&t=96.0

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

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

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