这是我的java应用程序
package com.example.abdo8;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CodeJavaApplication {
public static void main(String[] args) {
SpringApplication.run(CodeJavaApplication.class, args);
}
}
这是我的管理员
package com.example.abdo8.contoller;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.abdo8.models.User;
import com.example.abdo8.service.userservice;
@RestController
public class Admincontroller {
@Autowired
userservice uDao;
//@RequestMapping(value="/admin/addUser",method = RequestMethod.POST)
//@PostMapping(path="/admin/addUser",consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
//@RequestMapping(value = "/admin/addUser", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE,
// produces = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(
value = "admin/addUser", consumes = "application/json", produces = "application/json")
public User addUser(@RequestBody User usr)
{
//ResponseEntity<String>
User n = new User();
System.out.println(usr);
n.setEmail(usr.getEmail());
n.setFirstname(usr.getFirstname());
n.setLastname(usr.getLastname());
n.setPassword(usr.getPassword());
n.setUsername(usr.getUsername());
uDao.saveUser(n);
HttpHeaders httpHeaders = new HttpHeaders();
// return new ResponseEntity<>("{}", httpHeaders, HttpStatus.CREATED);
return n;
}
@GetMapping("/admin/getAll")
public Collection<User>getAllUsers()
{
return uDao.getAllUsers();
}
@DeleteMapping("admin/deleteUser/{id}")
public void removeById(@RequestParam(name="id") int id)
{
uDao.removeUser(id);
}
@RequestMapping("/admin/testMethods/hi")
@ResponseBody
public String printHi()
{
return "hi";
}
}
这是我的课程
package com.example.abdo8.DOA;
import javax.persistence.Table;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.example.abdo8.models.User;
@Repository
public interface RepoCourse extends JpaRepository<User,Integer> {
}
这是我的课程
package com.example.abdo8.models;
import java.io.Serializable;
public class Course implements Serializable{
private static final long serialVersionUID = 1L;
}
这是我的用户
package com.example.abdo8.models;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String email;
@Column
private String userpassword;
@Column
private String firstname;
@Column
private String lastname;
@Column
private String username;
public User() {
}
public User( String email, String userpassword, String firstname, String lastname, String username) {
super();
this.email = email;
this.userpassword = userpassword;
this.firstname = firstname;
this.lastname = lastname;
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return userpassword;
}
public void setPassword(String password) {
this.userpassword = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
这是我的用户服务
package com.example.abdo8.service;
import java.util.Collection;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.abdo8.DOA.RepoCourse;
import com.example.abdo8.models.User;
@Service
@Transactional
public class userservice {
@Autowired
private RepoCourse dao;
public User saveUser(User u)
{
return dao.save(u);
}
public Collection<User> getAllUsers()
{
return dao.findAll();
}
public void removeUser(int id )
{
dao.deleteById(id);
}
public User updateUser(User u )
{
return dao.save(u);
}
}
这是我的应用程序属性
# server.port=7000
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url = jdbc:mysql://localhost:3306/abdoDb?allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=admin
spring.datasource.password=Alma12345
spring.jpa.open-in-view=false
logging.level.org.hibernate.SQL=DEBUG
logging.level.org.hibernate.type=TRACE
spring.security.user.name=adam
spring.security.user.password=shlaz2009
这是我的index.html
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" >
<style type="text/css">
<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300' rel='stylesheet' type='text/css'>
<style type="text/css">
.form-style-8{
font-family: 'Open Sans Condensed', arial, sans;
width: 500px;
padding: 30px;
background: #FFFFFF;
margin: 50px auto;
box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.22);
-moz-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.22);
-webkit-box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.22);
}
.form-style-8 h2{
background: #4D4D4D;
text-transform: uppercase;
font-family: 'Open Sans Condensed', sans-serif;
color: #797979;
font-size: 18px;
font-weight: 100;
padding: 20px;
margin: -30px -30px 30px -30px;
}
.form-style-8 input[type="text"],
.form-style-8 input[type="date"],
.form-style-8 input[type="datetime"],
.form-style-8 input[type="email"],
.form-style-8 input[type="number"],
.form-style-8 input[type="search"],
.form-style-8 input[type="time"],
.form-style-8 input[type="url"],
.form-style-8 input[type="password"],
.form-style-8 textarea,
.form-style-8 select
{
box-sizing: border-box;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
outline: none;
display: block;
width: 100%;
padding: 7px;
border: none;
border-bottom: 1px solid #ddd;
background: transparent;
margin-bottom: 10px;
font: 16px Arial, Helvetica, sans-serif;
height: 45px;
}
.form-style-8 textarea{
resize:none;
overflow: hidden;
}
.form-style-8 input[type="button"],
.form-style-8 input[type="submit"]{
-moz-box-shadow: inset 0px 1px 0px 0px #45D6D6;
-webkit-box-shadow: inset 0px 1px 0px 0px #45D6D6;
box-shadow: inset 0px 1px 0px 0px #45D6D6;
background-color: #2CBBBB;
border: 1px solid #27A0A0;
display: inline-block;
cursor: pointer;
color: #FFFFFF;
font-family: 'Open Sans Condensed', sans-serif;
font-size: 14px;
padding: 8px 18px;
text-decoration: none;
text-transform: uppercase;
}
.form-style-8 input[type="button"]:hover,
.form-style-8 input[type="submit"]:hover {
background:linear-gradient(to bottom, #34CACA 5%, #30C9C9 100%);
background-color:#34CACA;
}
table{
border-width: thick;
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
</style>
<style type="text/css">
*:lang(ar) { direction:rtl }
*:lang(en) { direction:ltr }
</style>
<title> login page </title>
</head>
<body dir="rtl" >
<div class="form-style-8">
<h2>Login to your account</h2>
<form>
<input type="text" id="a1" name="field1" placeholder="FirstName" />
<input type="text" id="a2" name="field2" placeholder="LastName" />
<input type="text" id="a3" name="field4" placeholder="email" />
<input type="text" id="a4" name="field5" placeholder="username" />
<input type="text" id="a5" name="field6" placeholder="password" />
<button type = "submit" class="btn btn-success btn-lg" onclick="postApi(this);" dir="rtl"> Sign Up</button>
</form>
</div>
<table class="table">
<thead>
<tr>
<td>
id
</td>
<td>
firstname
</td>
<td>
lastname
</td>
<td>
email
</td>
<td>
username
</td>
<td>
password
</td>
</tr>
</thead>
</table>
<!-- --script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" ></script-->
<!-- -script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" ></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script>
function postApi(val){
var model = {
"firstname": this.$("#a1").val(),
"lastname": this.$("#a2").val(),
"email" : this.$("#a3").val(),
"username" : this.$("#a4").val(),
"userpassword" : this.$("#a5").val()
};
console.log("post data:"+model);
$.ajax(
{
type:"POST",
url : "admin/addUser",
ContentType:"application/json ;charset=UTF-8",
crossOrigin:true,
dataType:"json",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
data:this.JSON.stringify(model),
success:function(data)
{
console.log("successfuly"+data);
alert("done");
},error:function(jqXHR,testStatus,errorThrown)
{
alert("error");
}
}
);
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax(
{
type:"get",
url : "admin/getAll",
ContentType:"application/json ;charset=UTF-8",
crossOrigin:true,
dataType:"json",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
success:function(result)
{
console.log(result)
var table = $(".table")
for(i=0;i<result.length;i++)
{
table.append('<tr><td>'+ result[i]["id"] + '</td>' +
'<td>'+result[i]["firstname"] + '</td>'+
'<td>'+ result[i]["lastname"] + '</td>'+
'<td>'+ result[i]["email"] + '</td>'+
'<td>'+ result[i]["username"] + '</td>'+
'<td>'+ result[i]["password"]+ '</td>'+
'<td><button class="btn btn-danger" id="remove" onclick="removeUser('+result[i]["id"]+')">حذف</button></td></tr>'
);
$('message').html(table);
}
console.log("successfuly"+data);
alert("done");
},error:function(jqXHR,testStatus,errorThrown)
{
alert("error");
}
});
});
</script>
<script>
function removeUser(id){
$.ajax({
url: "admin/deleteUser/id?id="+id,
method:'DELETE',
dataType:'json',
success:function(data){
alert("the user number "+id+" is deleted");
},error: function(jqXHR,testStatus,errorThrown){
alert("faild !!!");
}
});
}
</script>
</body>
</html>
这是我的pom.xml
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>abdo-8</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>abdo-8</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
这是我的腹肌
package com.example.abdo8;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class abdotest {
@Test
void contextLoads() {
}
}
当我在我的索引页面中点击“注册”按钮,然后我回到mysql数据库,然后我回到表中,表中没有任何内容
1条答案
按热度按时间pkwftd7m1#
表单元素没有动作或方法。只是
把它改成
这将向控制器发送请求。更新html,在控制器中设置断点并在debug中运行它。