error HTTP Status 400 – after form submission

first of all I have two tables job, category which are in diagram as

Job to Cat

and my entities are :

@Entity
@Table( name = TableName.JOB_TABLE)
public class Job {


    @Id
    @GeneratedValue
    private Integer id;

    private String title;

    private String description;     

    @OneToMany(mappedBy = "job")
    private List<Category> categories;

    // omitting setters an getters for brevity  

}

and

@Entity
@Table( name = TableName.CATEGORY_TABLE)
public class Category {

    @Id
    @GeneratedValue
    private Integer id;

    private String name;

    @ManyToOne
    @JoinColumn(name = "job_id")
    private Job job;

    // omitting setters an getters for brevity  

}

JobService is

@Service
public class JobService implements IDatabaseCrud<Job>{

    @Autowired
    private JobRepository jobRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public void save(Job obj) {
        // TODO Auto-generated method stub
        jobRepository.save(obj);
    }
}

JobRepository is

public interface JobRepository extends JpaRepository<Job, Integer> {

}

now I don’t have any idea to save new job using the form

<form:form commandName="job">
   <form:input path="title"/><br>
    <form:input path="company"/><br>
    <form:input path="location"/><br>
    <form:input path="url"/><br>
    <form:input path="email"/><br>
    <form:input path="description"/><br>
    <form:select path="categories">
    <form:options items="${categories}" itemValue="id" itemLabel="name"/>
    </form:select><br>
    <form:input path="createdAt"/><br>
    <form:input path="toApply"/><br>
    <input type="submit" value="Add Job">
</form:form>

with the following controller I want to save these details to DB

@Controller
public class JobController {

    @Autowired
    private JobService jobService;

    @Autowired
    private CategoryService categoryService;

    @ModelAttribute("job")
    public Job constructJob() {
        return new Job();
    }

    @RequestMapping(value = "/show-add-job", method = RequestMethod.GET)
    public String showJobForm(Model model) {
        model.addAttribute("categories", categoryService.findAll());
        return "add-job";
    } 

    @RequestMapping(value = "/show-add-job", method = RequestMethod.POST)
    public String addJobDetail(@ModelAttribute("job") Job job) {
        jobService.save(job);
        return "redirect:/jobs";
    } 

}

with the above stuff when I submit the form I get HTTP Status 400.
This is URL to that project.


Source: forms

Leave a Reply