使用Model将List<类>的值传入jsp页面并显示

实现标题两步操作即可

@Controller

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";
    }
}

显示的jsp(如图书基本信息为例)

<%@ 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>

具体细节如下

提前准备步骤

First.
Second.
Third.

注意事项

1.观察自己的MySQL版本,可以通过cmd命令行等软件查看,确认版本后,click.对应的版本号,添加对应的dependence。
如图:
在这里插入图片描述

在这里插入图片描述
放入到对应的pom.xml中dependencies中

2.分层方式写代码必须扫描到每一层文件

<?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文件都要使用注解扫描  

实际操作

未分层的结构目录:
在这里插入图片描述

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;
    }

}

以后我会坚持时不时写博客的QAQ(第一篇博客纯粹是个笑话,就想开个头
这些都是我写这次JavaEE作业路过的坑,如果要写分层模型记得看上文的 注意事项
本人蒟蒻上路,如有不足,望大佬orz指正!

我们都是乌龟,背负着责任和梦想,也曾怨愤,终有一天,你会发现,它们成了你唯一不离不弃的依靠

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值