Use Model to transfer the value of List<class> to the jsp page and display

Realize the title two-step operation

@Controller class

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


import java.util.List;

@Controller
public class LoginController {
    
    
    @RequestMapping("/login")
    public String login(Model model){
    
    

        List<Book> books=new Ing().run();
        model.addAttribute("classname",books);
        return "login";
    }
}

Displayed jsp (as shown in the basic information of the book as an example)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<table border="1" align="center" cellspacing="0" >
<tr>
    <th>BOOKID</th>
    <th>BOOKNAME</th>
    <th>BOOKNUMBER</th>
</tr>
    <c:forEach items="${classname}" var="user">
        <tr>
            <td>${
    
    user.bookid}</td>
            <td>${
    
    user.bookname}</td>
            <td>${
    
    user.booknumber}</td>
        </tr>
    </c:forEach>
</table>
</body>
</html>

The details are as follows

Preparation steps in advance

First.
Second.
Third.

Precautions

1. Observe your own MySQL version, you can view it through cmd command line and other software, after confirming the version, click on the corresponding version number to add the corresponding dependency.
As shown:
Insert picture description here

Insert picture description here
Put it into the dependencies of the corresponding pom.xml

2. To write code in a layered way, you must scan each layer of files

<?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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
   <!-- 开启注解 配置扫描路径 -->
   <context:component-scan base-package="com.lucas"/>
   <!-- 如果有多个路径 ,号隔开
   <context:component-scan base-package="com.lucas.service,com.lucas.dao"/>-->
</beans>
//如多个路径用逗号分隔开,扫描到每一层
//spring xml文件都要使用注解扫描  

Actual operation

Non-hierarchical structure directory:
Insert picture description here

1.LoginContro

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


import java.util.List;

@Controller
public class LoginController {
    
    
    @RequestMapping("/login")
    public String login(Model model){
    
    

        List<Book> books=new Ing().run();
        model.addAttribute("classname",books);

        return "login";
    }
}

2.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_3_1.xsd"
         version="3.1">
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--//加載spring容器-DispatcherServlet为中央控制器->
        <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
        <!--//启动服务时加载dispatcher-servlet.xml文件->
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
        <!--//使用/来响应每一个请求->
    </servlet-mapping>
</web-app>

2.dispatcher-servlet.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--打開注解-->
    <context:component-scan base-package="mvc"></context:component-scan>
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value ="/WEB-INF/jsp/"></property>
        <property name="suffix" value =".jsp"></property>
    </bean>
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!-- 1.1.数据库驱动 -->
        <property name="driverClassName"
                  value="com.mysql.cj.jdbc.Driver"></property>
        <!-- 1.2.连接数据库的url -->
        <property name="url"
                  value="jdbc:mysql://localhost:3306/chat?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true"></property>
        <!-- 3306后的chat为数据库名 -->
        
        <!-- 1.3.连接数据库的用户名 -->
        <property name="username" value="root"></property>
        <!-- 1.4.连接数据库的密码 -->
        <property name="password" value="root"></property>
    </bean>

    <!-- 2配置JDBC模板 -->
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 默认必须使用数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--    配置数据源的事务管理-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

3.index.jsp

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<a href="login">Displays book information</a>
</body>
</html>

4.login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
    <style>
        .unnamed1 {
    
    
            padding-top: 300px;
        }
    </style>
<head>
<body>
<table border="1" align="center" valign="middle" cellspacing="0" c>
<tr>
    <th>BOOKID</th>
    <th>BOOKNAME</th>
    <th>BOOKNUMBER</th>
</tr>
    <c:forEach items="${classname}" var="user">
        <tr>
            <td>${
    
    user.bookid}</td>
            <td>${
    
    user.bookname}</td>
            <td>${
    
    user.booknumber}</td>
        </tr>
    </c:forEach>
</table>
<h3>${
    
    classname.size()}</h3>
</body>
</html>

5.Book

/*
CREATE table BookInfo(
bookid INT not NULL,
bookname VARCHAR(15),
booknumber INT);

desc BookInfo;

SELECT *from BookInfo;

DROP TABLE BookInfo;

INSERT into BookInfo (?,?,?) VALUES (01,'interesting',22);
*/
public class Book {
    
    
    int bookid;
    String bookname;
    int booknumber;

    public int getBookid() {
    
    
        return bookid;
    }

    public void setBookid(int bookid) {
    
    
        this.bookid = bookid;
    }

    public String getBookname() {
    
    
        return bookname;
    }

    public void setBookname(String bookname) {
    
    
        this.bookname = bookname;
    }

    public int getBooknumber() {
    
    
        return booknumber;
    }

    public void setBooknumber(int booknumber) {
    
    
        this.booknumber = booknumber;
    }

    @Override
    public String toString() {
    
    
        return "Book{" +
                "bookid=" + bookid +
                ", bookname='" + bookname + '\'' +
                ", booknumber=" + booknumber +
                '}';
    }
}

6.BookInfo

import java.util.List;
public interface BookInfo {
    
    
        public int update(String sql,Object[] param);
        public List<Book> query(String sql, Object[]  param);
}

7.BookInfoImp


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("bookInfo")
public class BookInfoImp implements BookInfo{
    
    

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int update(String sql,Object[] param){
    
    
        return  jdbcTemplate.update(sql,param);
    }

    public List<Book> query(String sql, Object[]  param){
    
    
        RowMapper<Book> rowMapper = new BeanPropertyRowMapper<>(Book.class);
        return  jdbcTemplate.query(sql,rowMapper,param);
    }
}

8.Ing


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class Ing {
    
    
    List<Book>  run(){
    
    
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookInfo bookInfo = (BookInfo) applicationContext.getBean("bookInfo");
        String str="select * from bookInfo";


        List<Book> books=bookInfo.query(str,null);

        for(Book  book:books ){
    
    
            System.out.println(book);
        }
        return books;
    }

}

Then I will insist on QAQ from time to time to blog ( first blog is purely a joke, wanted it off the ground )
These are my job to write the JavaEE passing the pit, if I remember watching the hierarchical model to write the above considerations
I Konjac hits the road, if there are any shortcomings, please correct me!

We are all tortoises, carrying responsibilities and dreams, and we were resentful. One day, you will find that they have become the only support you can never leave.

Guess you like

Origin blog.csdn.net/qq_45398544/article/details/109324499