【问题标题】:Spring Boot, Html button which will add parameters to table in databaseSpring Boot,Html 按钮,它将参数添加到数据库中的表中
【发布时间】:2018-08-14 17:54:13
【问题描述】:

我想创建一个链接或另一个 html 元素,它将使用 JPA 和 Thymeleaf 将参数添加到 MySQL 数据库中的表中。我已经创建了一个具有良好 url 的链接(在表中创建一个新参数),但是在单击此元素后 firefox 说:“地址不被理解”但手动输入相同的 url 有效。我需要 index.html 中的那个元素,点击后它会插入到表值中。谢谢。

控制器:

@Controller
public class UserController {

@RequestMapping("/")
public String home() {
    return "index";
}

@Autowired
private UserDao userDao;

@RequestMapping("/create")
@ResponseBody
public String create(String email, String name) {
    String userId="";
    try {
        User user = new User(email, name);
        userDao.save(user);
        userId = String.valueOf(user.getId());
    }
    catch (Exception exception) {
        return "Error creating the user" + exception.toString();
    }
    return "User succesfully created with id = " + userId;
}

index.html:

<!DOCTYPE html>
<html lang="en"> 
<head>
<meta charset="UTF-8">
<title>HomePage</title>
</head>
<body>
<div id="container">
<a href="localhost:8080/create/email=john10234@gmail.com&name=John">Click to 
add: ([id], 'john10234@gmail.com', 'John')
</a>
</div>
</body>
</html>

【问题讨论】:

    标签: java hibernate jpa spring-boot thymeleaf


    【解决方案1】:

    你错过了吗?在网址中 你的网址将 a href="localhost:8080/create?email=john10234@gmail.com&amp;name=John"&gt; 而不是href="localhost:8080/create/email=john10234@gmail.com&amp;name=John"&gt;

    也可以使用@RequestParam

    @RequestMapping("/create") @ResponseBody public String create(@RequestParam("email")String email, @RequestParam("name")String name)

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 2016-09-09
    相关资源
    最近更新 更多