Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:03:43+00:00 2026-05-29T05:03:43+00:00

I have a problem with the my Controller code. GET works fine (both empty

  • 0

I have a problem with the my Controller code. GET works fine (both empty form + form populated from db), POST works fine only for creating new object, but doesn’t work for editing. Part of my @Controller class:

    @RequestMapping(value = "/vehicle_save.html", method = RequestMethod.GET)
public String setUpForm(@RequestParam(value="id", required = false) Long id, ModelMap model) {
    Vehicle v;
    if (id == null) {
        v =  new Vehicle();
    } else {
        v = vehicleManager.findVehicle(id);
    }
    model.addAttribute("vehicle", v);
    return "vehicle_save";
}

@RequestMapping(value = "/vehicle_save.html", method = RequestMethod.POST)
public String save(@ModelAttribute("vehicle") Vehicle vehicle, BindingResult result, SessionStatus status) {
    vehicleValidator.validate(vehicle, result);
    if (result.hasErrors()) {
        return "vehicle_save";
    } 
    if(vehicle.getId() == null) {
        vehicleManager.createVehicle(vehicle);  
    } else {
        vehicleManager.updateVehicle(vehicle);  
    }
    status.setComplete();
    return "redirect:vehicle_list.html";
}

The first method creates a vehicle object (including its ID). But the second method gets the same object without the ID field (set to null).

What could I do: manually set vehicle.setID(id from parameters) and then save it to database. This causes JPAOptimisticLockException + I don’t like that solution.

Is there a way to pass my Vehicle object with ID to the second method? BTW, I would like to avoid adding hidden ID field to the JSP.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:03:45+00:00Added an answer on May 29, 2026 at 5:03 am

    the example you suggested is using session to store the value. the @SessionAttribute is to bind an existing model object to the session. Look at the source code the class is annotated with @SessionAttributes("pet").Which means your model attribute named “pet” is getting stored in session.Also look at the code in processSubmit method of EditPetForm class

        @RequestMapping(method = { RequestMethod.PUT, RequestMethod.POST })
        public String processSubmit(@ModelAttribute("pet") Pet pet, BindingResult result, SessionStatus status) {
            new PetValidator().validate(pet, result);
            if (result.hasErrors()) {
                return "pets/form";
            }
            else {
                this.clinic.storePet(pet);
                status.setComplete(); //look at its documentation
                return "redirect:/owners/" + pet.getOwner().getId();
            }
        }
    

    I havnt used something like this before.But i guess putting ur id in session is the way

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I have the following code: $.ajax({ type: GET, url: /Search/GetNewData //Controller/ActionMethod ---snip--- )}; This
I have tried running the code without the collision, and it works fine. But
I have the problem that my view controller class has too many delegates and
I'm struggling here with a problem: I have a controller questions which has action
I have no problems processing jQuery GET requests in a controller, however I cannot
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
Can't get my head around this Jquery problem I have.. I've been looking around
problem description: This model works fine with one user at a time. As soon
I have a strange problem with the following code: MembersDetailViewController *m = [[MembersDetailViewController alloc]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.