如何用springboot和jpaRepository续写一个登录界面

木易
  • 1
新手上路,请多包涵

题目描述

如何用springboot和jpaRepository续写一个登录界面

相关代码

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>

<meta charset="UTF-8">
<title>商品列表</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>
<h1>商品列表</h1>
<div class="container">

<div class="form-group">
  <div class="col-sm-2 control-label">
      <a href="/shop/toAdd">添加</a>
  </div>
</div>

le class="table table-hover">
<thead>
<tr align="center">
    <th>ID</th>
    <th>商品名</th>
    <th>种类</th>
    <th>价格</th>
    <th>生产地</th>
    <th colspan="2">操作</th>
</tr>
</thead>

<tbody>

<tr th:each="goods:${lists}">
    <td th:text="${goods.Id}">id</td>
    <td th:text="${goods.name}">name</td>
    <td th:text="${goods.type}">type</td>
    <td th:text="${goods.price}">price</td>
    <td th:text="${goods.address}">address</td>
    <td><a th:href="@{/shop/toEdit(Id=${goods.Id})}">编辑</a></td>
    <td><a th:href="@{/shop/delete(Id=${goods.Id})}">删除</a></td>
</tr>

</tbody>
</table>
</div>
</body>
</html>

package com.yhj.springboot.demo.model;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Data
@Entity
public class admins {

@Id
@GeneratedValue
Integer Id;

String username;
String password;

}

package com.yhj.springboot.demo.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import com.yhj.springboot.demo.model.admins;

public interface LogRepository extends JpaRepository<admins,Integer> {
}

<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8" http-equiv="Content-Type" content="text/html">
<title>登录</title>

</head>
<body>
<h1>登录</h1>
<div class="container">

<form action="/shop/login" method="post" class="form-horizontal">
    <div class="form-group">
        <label class="col-sm-2 control-label">用户名</label>
        <div class="col-sm-10">
            <input class="form-control" type="text" name="username" placeholder="请输入用户名">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">密码</label>
        <div class="col-sm-10">
            <input class="form-control" type="text" name="password" placeholder="请输入密码">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-10">
            <input class="btn btn-info" type="submit" value="提交">
            <input class="btn btn-info" type="reset" value="重置">
        </div>
    </div>
</form>

</div>
</body>
</html>

package com.yhj.springboot.demo.controller;

import com.yhj.springboot.demo.model.goods;
import com.yhj.springboot.demo.repository.ShopRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**

  • @desc 商品管理
  • @author yhj
  • @date 2021/12/27
    */

@Controller
@RequestMapping("/shop")
public class ShopController {

@Autowired
private ShopRepository shopRepository;
@RequestMapping("/list")
public String list(Model model) {
    /*model.addAttribute("msg","hello,springboot!");*/
    List<goods> goodsList = shopRepository.findAll();
   // System.out.println(goodsList);
    model.addAttribute("lists",goodsList);
    return "shop/list";
}

/**
 *跳转编辑页面
 */

@RequestMapping("/toEdit")

public String toEdit(Model model,int Id){
  goods gs = shopRepository.getById(Id);
   model.addAttribute("goods", gs);
    return "shop/edit";
}

/**
 * 根据Id编辑商品
 */
@RequestMapping("/edit")
public String edit(goods gs) {
    shopRepository.save(gs);
    return "redirect:/shop/list";
}


/**
 * 删除
 */
@RequestMapping("/delete")
public String delete(int Id){
    shopRepository.deleteById(Id);
    return "redirect:/shop/list";
}

/**
 * 跳转到商品添加页面
 */
@RequestMapping("/toAdd")
public String toAdd(){
    return "/shop/add";
}
/**
 * 添加
 */
@RequestMapping("/add")
public String add(goods gs){
    shopRepository.save(gs);
    return "redirect:/shop/list";
}

}

回复
阅读 467
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏