SpringMVC学习三步走系列------第一部分之传值、取值、回显

配套视频:文章底部

框架怎么学?分成三部分:
第一部分:跳转、传值、取值 (前台和Controller搞来搞去)
第二部分:MyBatis整合写出Dao(数据库的CRUD)
第三部分:Controller调Service(前后台整合)
pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.hr</groupId>
  <artifactId>spirngmvc-1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>spirngmvc-1 Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>
    <!---->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.apache.log4j</groupId>
      <artifactId>com.springsource.org.apache.log4j</artifactId>
      <version>1.2.16</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>com.springsource.org.apache.commons.logging</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.9.4</version>
    </dependency>
    <!--切面-->

    <!--jdbc-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <!--事务-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <!--数据库驱动-->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.47</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-dbcp2</artifactId>
      <version>2.5.0</version>
    </dependency>
    <!--servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>
    <!--jstl-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!--spring-web-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.0.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>spirngmvc-1</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">
    <!--spring监听器 加载spring.xml文件 全局-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
    <!--springmvc前端控制器 加载springmvc.xml  局部-->
  <servlet>
      <servlet-name>springMvc</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>
      <load-on-startup>1</load-on-startup>
  </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--配置过滤器-->
    <filter>
        <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
        <url-pattern>/*
    
    
        index.jsp
    



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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--扫包-->
    <context:component-scan base-package="com.hr"/>
    <!--配前缀和后缀(视图解析器)IRVR-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/admin/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

entity

package com.hr.entity;

/**
 * @ClassName Book
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/5/28 0028
 * @Version V1.0
 **/
public class Book {
    private Integer id;
    private String name;
    private String author;
    private double price;
    private String kind;//下拉框
    private int state;//单选
    private String chapter;//章节 复选框

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Book() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public String getChapter() {
        return chapter;
    }

    public void setChapter(String chapter) {
        this.chapter = chapter;
    }
}

前台
list.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/27 0027
  Time: 15:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script>
        function del(id) {
           if(window.confirm("你确定要删除吗?")){
               alert(id);
               window.location.href="${pageContext.request.contextPath}/book/del?id=";
           }


        }
    </script>
</head>
<body>
<h1>显示所有图书</h1>
<a href="${pageContext.request.contextPath}/book/toAdd">添加图书</a>
<hr/>
<table border="1" cellspacing="0" cellpadding="0" width="80%">
    <thead>
    <tr>
        <th>id</th>
        <th>name</th>
        <th>author</th>
        <th>price</th>
        <th>info</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>1</td>
        <td>
            <a href="${pageContext.request.contextPath}/book/toUpdate/1001">编辑</a>
            |
            <a href="javascript:void(0)" onclick="del(1)">删除</a>
        </td>
    </tr>

    </tbody>
</table>

</body>
</html>

toAdd.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/27 0027
  Time: 15:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加图书</title>
</head>
<body>
<h1>添加图书</h1>
<form action="${pageContext.request.contextPath}/book/add" method="post">
    <table border="1" cellpadding="0" cellspacing="0" width="60%">
        <tr>
            <td>id:</td><td><input type="text" name="id" id="id"/></td>
        </tr> <tr>
            <td>name:</td><td><input type="text" name="name" id="name"/></td>
    </tr> <tr>
        <td>author:</td><td><input type="text" name="author" id="author"/></td>
    </tr> <tr>
        <td>price:</td><td><input type="text" name="price" id="price"/></td>
    </tr> <tr>
        <td>kind:</td>
            <td>
                <select name="kind" id="kind">
                    <option value="">请选择</option>
                    <option value="文学">文学</option>
                    <option value="科幻">科幻</option>
                    <option value="武侠">武侠</option>
                </select>
             </td>
    </tr><tr>
            <td>state:</td><td>
            <input type="radio" name="state" id="ok" value="1">已归还
            <input type="radio" name="state" id="no" value="0">借出
            </td>
    </tr><tr>
            <td>chapter:</td>
            <td>
                <input type="checkbox" name="chapter" value="上册">上册
                <input type="checkbox" name="chapter" value="中册">中册
                <input type="checkbox" name="chapter" value="下册">下册
            </td>
        </tr>
        <tr><td colspan="2">
            <input type="submit" value="添加">
            <input type="reset" value="重置">
        </td> </tr>
    </table>
</form>
</body>
</html>

toUpdate.jsp

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/5/27 0027
  Time: 15:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>修改图书</h1>
    <form action="${pageContext.request.contextPath}/book/update" method="post">
        <table border="1" cellpadding="0" cellspacing="0" width="60%">
            <tr>
                <td>id:</td><td><input type="text" name="id" id="id" value="${book.id}"/></td>
            </tr> <tr>
            <td>name:</td><td><input type="text" name="name" id="name" value="${book.name}"/></td>
        </tr> <tr>
            <td>author:</td><td><input type="text" name="author" id="author" value="${book.author}"/></td>
        </tr> <tr>
            <td>price:</td><td><input type="text" name="price" id="price" value="${book.price}"/></td>
        </tr> <tr>
            <td>kind:</td>
            <td>
                <select name="kind" id="kind">
                    <option value="">请选择</option>
                    <option value="文学" ${book.kind=='文学'?'selected':''}>文学</option>
                    <option value="科幻" ${book.kind=='科幻'?'selected':''}>科幻</option>
                    <option value="武侠" ${book.kind=='武侠'?'selected':''}>武侠</option>
                </select>
            </td>
        </tr><tr>
            <td>state:</td><td>
            <input type="radio" name="state" id="ok" value="1" ${book.state=='1'?'checked':''}>已归还
            <input type="radio" name="state" id="no" value="0" ${book.state=='0'?'checked':''}>借出
        </td>
        </tr><tr>
            <td>chapter:</td>
            <td>
                <input type="checkbox" name="chapter" value="上册" <c:forTokens items="${book.chapter}" delims="," var="c">${c.contains('上册')?'checked':''}</c:forTokens> >上册
                <input type="checkbox" name="chapter" value="中册" <c:forTokens items="${book.chapter}" delims="," var="c">${c.contains('中册')?'checked':''}</c:forTokens> >中册
                <input type="checkbox" name="chapter" value="下册" <c:forTokens items="${book.chapter}" delims="," var="c">${c.contains('下册')?'checked':''}</c:forTokens> >下册
            </td>
        </tr>
            <tr><td colspan="2">
                <input type="submit" value="添加">
                <input type="reset" value="重置">
            </td> </tr>
        </table>

    </form>
</body>
</html>

controller

package com.hr.web;

import com.hr.entity.Book;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName BookController
 * @Description: TODO
 * @Author 汤永红
 * @Date 2020/5/27 0027
 * @Version V1.0
 **/
//@ComponentScan注解及其XML配置
//  开发中会经常使用包扫描,只要标注了@Controller、@Service、@Repository,@Component 注解的类会自动加入到容器中,ComponentScan有注解和xml配置两种方式。
@Controller
@RequestMapping("/book")
public class BookController {
    @RequestMapping(value = "/sayHello",produces = "application/json;charset=utf-8")
    @ResponseBody //不跳转,直接输出字符串
    public String sayHello(){
        return "我的第一个SpringMVC";
    }

    //跳list.jsp
    @RequestMapping("/list")
    public String list(){

        //return "/WEB-INF/admin/list.jsp"; //物理路径
        //相对路径
        return "list";
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "toAdd";
    }
    @RequestMapping("/add")
    public String add(Book book){//是否能自动填充前台的数据???
        System.out.println(book);
        return "msg";
    }

    @RequestMapping("/msg")
    public String msg(){

        return "msg";
    }

//https://blog.csdn.net/zalan01408980/article/details/82904126
    @RequestMapping(value = "/toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id, Model model){
        System.out.println("斜杠传值:"+id);
        //根据id查到一条记录
        Book book = new Book();
        book.setId(1001);
        book.setName("月亮与六便士");
        book.setAuthor("毛姆");
        book.setKind("科幻");//文学科幻武侠
        book.setChapter("上册,下册");
        book.setState(1);
        book.setPrice(100.0);
        model.addAttribute("book",book);
        return "toUpdate";
    }

    @RequestMapping("/del") //前台传来的时必须是  id=xxx
    public String del(@RequestParam(value = "id",required = false)Integer id){
        System.out.println("问号传值"+id);
        return "msg";
    }
}

有没有值,可以用debug测试

最重要的是修改功能的回显
SpringMVC学习三步走系列------第一部分之传值、取值、回显_第1张图片

93-spirngmvc-2-1.传值拿值

93-spirngmvc-2-2.三个大控件

你可能感兴趣的:(springmvc,spring,SPRINGMVC)