Java学习之SpringMVC(1)


下面是我的学习笔记 这是我跟着B站的一位UP主学习的笔记,方便自己以后复习
先上学习链接


SpringMVC

  • SpringMVC流程非常复杂,实际开发中很简单,因为大部分的组件不需要开发者创建,管理,只需要通过配制文件的方式完成配制即可。真正需要开发者进行处理的只有Handler、View.
  • Handler 是用来写业务的
  • View 是用来怎么展示它,以JSP方式或者其他方式。

创建SpringMVC工程

在这里插入图片描述

  • 创建Maven工程的时候选择webapp ,工程创建好后是没有红框框里的东西,需要自己创建。
  • pom.xml
<!--添加MVC配制-->
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
  </dependencies>
  • web.xml 中配制 DispatcherServlet
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
<!--  配制SpringMVC-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <!--上下文的一个配制路径-->
      <param-name>contextConfigLocation</param-name>
     <!--根目录 clsspath-->
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
<!--  拦截所有请求-->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
  • 配制springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

<!--    包扫描 自动扫描-->
    <context:component-scan base-package="com.southwind"/>

<!--    配制视图解析器 例如 返回一个 “index” 逻辑视图,然后变成物理视图  /index.jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--        前缀-->
        <property name="prefix" value="/"></property>
<!--        后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>
  • 需要给 IDLE 配制上一个Tomcat 来进行测试
    在这里插入图片描述
    在这里插入图片描述

  • 写一个测试类Test.java

package com.southwind.controller;

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

@Controller
public class HelloHandler {
//相当于一个业务方法
    @RequestMapping("/index")
    public String index(){
        System.out.println("执行了index...");
        return "index";
    }
}

  • 运行结果

在这里插入图片描述

  • SpringMVC 的初体验就此完结

SpringMVC的常用注解

@Controller 在类定义处添加,同时使其成为一个控制器接收客户端请求。记得在springmvc.xml中配制自动扫描

package com.southwind.controller;

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

@Controller
public class HelloHandler {
//相当于一个业务方法
    @RequestMapping("/index")
    public String index(){
        System.out.println("执行了index...");
        return "index";
    }
    
}

@RequestMapping 在类定义处添加 或者 方法前添加

  • 无参数
//http://localhost:8088/index
@RequestMapping("/index")
    public String index(){
        System.out.println("执行了index...");
        return "index";
    }
  • 有参数 请求类型
//http://localhost:8088/index_1?name=zasn&id=12
@RequestMapping("/index?name=oldbai&id=520")
    public String index_1(){
        System.out.println("执行了index...");
        return "index";
    }
//http://localhost:8088/index_1?name=zasn&id=12
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"name","id"})
    public String index_1(){
        System.out.println("执行了index...");
        return "index";
    }
  • 获取参数 参数绑定
//http://localhost:8088/index_1?name=zasn&id=12
// 参数名字 必须和 传过来的一致  使用这种方法的话
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"name","id"})
    public String index_1(String name,int id){
        System.out.println(name);
        System.out.println(id);
        System.out.println("执行了index...");
        return "index";
    }
//http://localhost:8088/index_1?name=zasn&id=12
// 参数名字 可以不和 传过来的一致  使用这种方法的话
@RequestMapping(value = "/index",method = RequestMethod.GET,params = {"name","id"})
    public String index_1(@RequestParam("name") String dstr, @RequestParam("id") int age){
        System.out.println(dstr);
        System.out.println(age);
        System.out.println("执行了index...");
        return "index";
    }
  • RESTful 风格 参数绑定
//http://localhost:8088/rest/zas/12
@RequestMapping(value = "/rest/{name}/{id}")
    public String index_2(@PathVariable("name") String dstr, @PathVariable("id") int age){
        System.out.println(dstr);
        System.out.println(age);
        System.out.println("rest...");
        return "index";
    }

  • 获取cookie
 @RequestMapping(value = "/cookie")
    public String cookie(@CookieValue(value = "JSESSIONID") String sessionid){
        System.out.println(sessionid);
        System.out.println("cookie...");
        return "index";
    }
  • 获取的参数是一个对象 需要添加以下东西
    loading.jsp
<%--
  Created by IntelliJ IDEA.
  User: 老白
  Date: 2020/5/4
  Time: 16:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="/hello/loading" method="post">
    用户名ID:<input type="text" name="id"><br>
    用户名:<input type="text" name="name"><br>
    地址:<input type="text" name="address.news"><br>
    <input type="submit" value="注册">
</form>
</body>
</html>

HelloHandler.java

package com.southwind.controller;

import com.southwind.controller.userValue.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/hello")
public class HelloHandler {
//    使用javaBean 属性名自动匹配,自动为对象填充属性值,同时支持级联属性
    @RequestMapping(value = "/loading",method = RequestMethod.POST)
    public String loading(User user){
        System.out.println(user);
        return "index";
    }

}

User类

package com.southwind.controller.userValue;

import lombok.Data;

@Data
public class User {
    private String name;
    private int id;
}

address类 级联属性

package com.southwind.controller.userValue;

import lombok.Data;

@Data
public class Address {
    private String news;
}

pom.xml 添加 lombok

<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.6</version>
      <scope>provided</scope>
</dependency>

防止乱码 web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <!--防止乱码-->
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
<!--  添加过滤器-->
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!--  配制SpringMVC-->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
  </servlet>
<!--  拦截所以请求-->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>


JSP的转发和重定向 ,注意 return 里面的东西

//    SpringMVC默认是以转发的形式响应JSP
//    上方代码都是转发 这是另一种转发格式 注意 return 里面的东西
    @RequestMapping("/forword")
    public String forword(){
        return "forward:/index.jsp";
    }
//    这是重定向 注意 return 里面的东西
    @RequestMapping("/redirects")
    public String redirect(){
        return "redirect:/index.jsp";
    }


总结

  • 到此为止 我的Spring MVC 学了这一些基础,如果有人刷到了这里,看我的笔记不懂,可以前往UP主楠哥教你学JAVA跟着他学,反正我是跟着他学的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值