简体   繁体   中英

I want to implement session(HTTP request)concept in my Springbootproject but its giving this error

The Error occurs when user is registering

Whitelabel error

  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: ""

There are two forms Registration and Office

Regitrstion(User)

@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

Office

@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

Session implementation in registration so that the id value(of office) will be filled

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

Office page JSP for id

    <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>

/ Project Concept /

Once a user register's(values will be stored in DB),his id should automatically be filled in the office form page and be saved in the database using the session concept but I'm getting this error please help!

According to the error

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

you need Integer value but app getting empty String value. Thats why It getting error. I will suggest you to use Integer class instead of int . I mean int id to Integer id . And I think in post controller in this statement session.setAttribute("id", id); value of id is not setting anyway. So I will suggest you to replace session.setAttribute("id", id); to session.setAttribute("id", user.getId());

The solution was changing int to Integer in both User and Office and including ` @GeneratedValue(strategy = GenerationType.IDENTITY) for the id in user.

  @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

Office

  @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`

In controller

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM