任务-competiton
遇到的问题:
-
问题1:在add方法中给集合添加元素,再启动测试方法打印遍历打印集合元素
解释:add方法和测试方法是两个进程,两者毫无联系,互不影响,所以add方法中给集合添加元素在test方法中打印是看不到add方法中添 加的元素 -
问题2 idea上能接收到前端发来的的代码,但是不能将响应数据展示在浏览器页面上
解决方法 -
1
进入Controller访问不到指定html文件。检查是否引入thymeleaf依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 2
依赖引入之后SpringBoot会默认到类路径(resources)下的templates中扫描,所以需要将html的代码文件粘贴到(resources)下的templates中
代码目录
统一封装类result代码:
package com.itsen.springbootquickstart.polo;
/**
* 统一响应结果封装类
*/
public class Result {
private Integer code ;//1 成功 , 0 失败
private String msg; //提示信息
private Object data; //数据 date
public Result() {
}
public Result(Integer code, String msg, Object data) {
this.code = code;
this.msg = msg;
this.data = data;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static Result success(Object data){
return new Result(200, "OK", data);
}
public static Result success(){
return new Result(1, "success", null);
}
public static Result error(String msg){
return new Result(0, msg, null);
}
@Override
public String toString() {
return "Result{" +
"code=" + code +
", msg='" + msg + '\'' +
", data=" + data +
'}';
}
}
javabean类competiton代码:
package com.itsen.springbootquickstart.polo;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
/**
* 赛事
*/
public class Competition {
/**
* Id
*/
private String id;
/**
* 年度
*/
private String year;
/**
* 名称
*/
private String name;
/**
* 主办单位
*/
private String organizer;
/**
* 所属专业
*/
private String major;
/**
* 描述
*/
private String description;
/**
* 对ALL进行增删改即可
*/
public static List<Competition> ALL = IntStream.range(0, 10)
.mapToObj(index -> {
Competition el = new Competition();
el.setId("id_" + index);
el.setYear("year_" + index);
el.setName("name_" + index);
el.setOrganizer("organizer_" + index);
el.setMajor("major_" + index);
el.setDescription("description_" + index);
return el;
}).collect(Collectors.toList());
// region getter/setter...
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getOrganizer() {
return organizer;
}
public void setOrganizer(String organizer) {
this.organizer = organizer;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public String toString() {
return "Competition{" +
"id='" + id + '\'' +
", year='" + year + '\'' +
", name='" + name + '\'' +
", organizer='" + organizer + '\'' +
", major='" + major + '\'' +
", description='" + description + '\'' +
'}';
}
// endregion
}
controller代码-Json 交互:
package com.itsen.springbootquickstart.controller;
import com.itsen.springbootquickstart.polo.Competition;
import com.itsen.springbootquickstart.polo.Result;
import org.junit.Test;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
public class CompetitionRequest {
private static List<Competition> All=Competition.ALL;
//add
@RequestMapping("/add")
// @Transactional(rollbackFor = Exception.class)
// @PutMapping("/add")
public static Result add(@RequestBody Competition competition)
{
System.out.println(competition);
System.out.println(" ------ ");
All.add(competition);
All.forEach(i->System.out.println(i));
return Result.success(All);
}
//注意:add方法和测试方法是两个进程,两者毫无影响,所以add方法中给集合添加元素
//但是在test方法中打印是看不到add方法中添加的元素
// //查看集合内部是否发生改变
// @Test
// public void test() {
// All.forEach(i->System.out.println(i));
// }
//delete
@GetMapping("/delete/{index}")
public static Result delete(@PathVariable int index)
{
System.out.println(All);
// index.intValue();
All.remove(index);
return Result.success(All);
}
//modify
@RequestMapping("/modify/{index}")
public static Result modify(@PathVariable int index,@RequestBody Competition competition)
{
All.set(index,competition);
return Result.success(All);
}
}
html交互控制层代码
@RestController
public class CompetitionRequestHtml {
private static List<Competition> All=Competition.ALL;
//新增
@RequestMapping("/addh")
public Result add(@ModelAttribute Competition competition)
{
System.out.println(competition);//打印请求过来的数据
System.out.println(" ------ ");
All.add(competition);
// All.forEach(i->System.out.println(i));//打印请求后的集合元素
return Result.success(All);
}
//删除
@RequestMapping("/deleteh")
public Result deleteh(@ModelAttribute Index index)
{
// int i=Integer.parseInt(index);
System.out.println(index.getIndex());
All.remove(index.getIndex());
return Result.success(All);
}
//修改
@RequestMapping("/update")
public Result update(@ModelAttribute Index index, @ModelAttribute Competition competition)
{
// int i=Integer.parseInt(index);
System.out.println(index.getIndex());
System.out.println(competition);
All.set(index.getIndex() ,competition);
return Result.success(All);
}
}
html下标bean代码
暂时找不到html参数传数字过来的接收方式,百度过一些注解和方法都无效,就封装到类中接收下标
public class Index {
private int index;
public Index() {
}
public Index(int index) {
this.index = index;
}
/**
* 获取
* @return index
*/
public int getIndex() {
return index;
}
/**
* 设置
* @param index
*/
public void setIndex(int index) {
this.index = index;
}
public String toString() {
return "Index{index = " + index + "}";
}
}
html代码
add
<!DOCTYPE html>
<html lang="en">
<head >
<meta charset="UTF-8">
<title >赛事-新增</title >
</head >
<body >
<form action="http://localhost:8080/addh" method="POST">
<p>id<input type = "text" name = "id"/></p>
<p>year<input type = "text" name = "year"/></p>
<p>name<input type = "text" name = "name"/></p>
<p>organizer<input type = "text" name = "organizer"/></p>
<p>major<input type = "text" name = "major"/></p>
<p>description<input type = "text" name = "description"/></p>
<p><input type = "submit" value = "submit"/></p>
</form >
</body >
</html >
delete
<!DOCTYPE html>
<html lang="en">
<head >
<meta charset="UTF-8">
<title >赛事-删除</title >
</head >
<body >
<form action="http://localhost:8080/deleteh" method="POST">
<p>index<input type = "number" name = "index"/></p>
<p><input type = "submit" value = "submit"/></p>
</form >
</body >
</html >
update
<!DOCTYPE html>
<html lang="en">
<head >
<meta charset="UTF-8">
<title >赛事-修改</title >
</head >
<body >
<form action="http://localhost:8080/update" method="POST">
<p>index<input type = "number" id="number" name = "index"/></p>
<p>id<input type = "text" name = "id"/></p>
<p>year<input type = "text" name = "year"/></p>
<p>name<input type = "text" name = "name"/></p>
<p>organizer<input type = "text" name = "organizer"/></p>
<p>major<input type = "text" name = "major"/></p>
<p>description<input type = "text" name = "description"/></p>
<p><input type = "submit" value = "submit"/></p>
</form >
</body >
</html >