我想在我的springbootproject中实现session(http请求)概念,但是它给出了这个错误

bt1cpqcv  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(357)

用户注册时出错
白标错误

This application has no explicit mapping for /error, so you are 
  seeing this as a fallback.
  Wed Oct 24 16:46:26 IST 2018
  There was an unexpected error (type=Bad Request, status=400).
  Failed to convert value of type 'java.lang.String' to required type 
  'int'; nested exception is java.lang.NumberFormatException: For input string: ""

有两种形式的登记和办公
注册(用户)

@Entity
@Table(name="mytable")
public class User {

@Id
private int id;  //Primary key
private String name;
private String mobile;
private String email;
private String college;
private String branch;
private String semester;
private String address;
private String internship;
private String batch;
private String startdate;
private String enddate;
//getters and setters

办公室

@Entity
@Table(name="officeinfo")
public class Office {

@Id
private int sno;
private String batchno;
private int  id;  
private String fees;
private String reciptno;
private String trainer;
//getters and setters

注册中的会话实现,以便填写(office的)id值

@PostMapping("/save-user")
 public String registerUser(@ModelAttribute User user, BindingResult 
 bindingResult, HttpServletRequest request,HttpSession 
 session,@RequestParam("id") int id) {
    userService.saveMyUser(user);
   session.setAttribute("id", id);
    int x = user.getId();
   request.setAttribute("mode", "MODE_HOME");
    return "welcomepage";
}

id的office页面jsp

<div class="form-group">
    <label class="control-label col-md-3">Candidate 
        Registe rNumber</label>
    <div class="col-md-5">
        <input type="text" class="form-control                       
    placeholder="Enter Register Number" 
         name="id" required value="${sessionScope.id }" />
     </div>
    </div>

/项目概念/
一旦用户注册的(值将存储在数据库中),他的id应该自动填写在office窗体页,并保存在数据库中使用会话的概念,但我得到这个错误请帮助!

bsxbgnwa

bsxbgnwa1#

解决方案是将user和office中的int都改为integer,并为user中的id包含“@generatedvalue(strategy=generationtype.identity)”。

@Entity
  @Table(name="mytable")
  public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Integer id;  //Primary key
  private String name;
  private String mobile;
  private String email;
  private String college;
  private String branch;
  private String semester;
  private String address;
  private String internship;
  private String batch;
  private String startdate;
  private String enddate;
  //getters and setters

办公室

@Entity
  @Table(name="officeinfo")
  public class Office {
  @Id
  private int sno;
  private String batchno;
  private Integer  id;  
  private String fees;
  private String reciptno;
  private String trainer;
  //getters and setters`

在控制器中

@PostMapping("/save-user")
  public String registerUser(@ModelAttribute User user, BindingResult 
  bindingResult, HttpServletRequest request,HttpSession 
  session,@RequestParam("id") Integer id) {
  userService.saveMyUser(user);
  session.setAttribute("id", id);
  int x = user.getId();
  request.setAttribute("mode", "MODE_HOME");
  return "welcomepage";
  }
5us2dqdw

5us2dqdw2#

根据错误

Failed to convert value of type 'java.lang.String' to required type 
  'int'; nested exception is java.lang.NumberFormatException: For input string: ""

需要整数值,但应用程序获取空字符串值。这就是为什么它会出错。我建议你用 Integer 类而不是 int . 我是说 int idInteger id . 我认为在这篇声明中 session.setAttribute("id", id); id的值没有设置。所以我建议你更换 session.setAttribute("id", id);session.setAttribute("id", user.getId());

相关问题