我想在我的Springbootproject中实现session(HTTP request)concept但它给出了这个错误

时间:2018-10-24 11:35:41

标签: mysql jsp spring-boot session

用户注册时发生错误

白色标签错误

  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

注册时执行会话,以便填写办公室的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>

/ 项目概念 /

用户注册后(值将存储在DB中),他的ID应该自动在Office表单页面中填写,并使用会话概念保存在数据库中,但是我遇到此错误,请帮忙!

2 个答案:

答案 0 :(得分:1)

根据错误

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

您需要Integer值,但应用程序获取空的String值。那就是为什么它得到错误。我建议您使用Integer类而不是int。我是说int idInteger id。而且我认为在此语句session.setAttribute("id", id);的后控制器中,id的值始终未设置。因此,我建议您将session.setAttribute("id", id);替换为session.setAttribute("id", user.getId());

答案 1 :(得分:1)

该解决方案将用户和Office中的int都更改为Integer,其中包括`       @GeneratedValue(strategy = GenerationType.IDENTITY)中的ID       用户。

  @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";
  }