Spring Hibernate:错误400“客户端发送的请求在语法上不正确”

时间:2017-06-21 05:52:15

标签: java spring hibernate

这是我的semester.java类

    @Entity
    @Table(name = "semester")
    @NamedQueries({
        @NamedQuery(name = "Semester.findAll", query = "SELECT s FROM Semester s")})
    public class Semester implements Serializable {

        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "id")
        private Integer id;
        @OneToMany(mappedBy = "semesterid")
        private List<Marks> marksList;
        @JoinColumn(name = "facultyid", referencedColumnName = "facultyid")
        @ManyToOne
        private Faculty facultyid;
        @JoinColumn(name = "semesterid", referencedColumnName = "semesterid")
        @ManyToOne
        private Semestername semesterid;
        @JoinColumn(name = "subjectcode", referencedColumnName = "subjectcode")
        @ManyToOne
        private Subject subjectcode;

        public Semester() {
        }

        public Semester(Integer id) {
            this.id = id;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        public List<Marks> getMarksList() {
            return marksList;
        }

        public void setMarksList(List<Marks> marksList) {
            this.marksList = marksList;
        }

        public Faculty getFacultyid() {
            return facultyid;
        }

        public void setFacultyid(Faculty facultyid) {
            this.facultyid = facultyid;
        }

        public Semestername getSemesterid() {
            return semesterid;
        }

        public void setSemesterid(Semestername semesterid) {
            this.semesterid = semesterid;
        }

        public Subject getSubjectcode() {
            return subjectcode;
        }

        public void setSubjectcode(Subject subjectcode) {
            this.subjectcode = subjectcode;
        }       
    }

这是管理员控制器

    @Controller
    public class AdminController {

        @Autowired
        AdminDao adminDao;

     @RequestMapping(value="/addsemester",method=RequestMethod.GET)
        public String addsemester(Model model){

            model.addAttribute("semesterlist", new Semester());
            return "semesterentry";
        }

         @RequestMapping(value="/addsemester",method=RequestMethod.POST)
        public String _addsemester(@ModelAttribute("semesterlist") Semester semester){

            boolean check = adminDao.insertsemester(semester);
            if(check){
                return "redirect:/addsemester";
            }
            else{
            return "dashboard";
        }
        }
    }

这是admindaoImpl.java类,当我用来插入学期400错误时,合成错误就面临了。

    @Repository("adminDao")
    public class AdminDaoImpl implements AdminDao {

        @Autowired
        SessionFactory sessionFactory;

    @Transactional
         public boolean insertsemester(Semester semester){
              boolean check = false;
            try {
                sessionFactory.getCurrentSession().save(semester);
            } catch (Exception e) {
            }
            if(sessionFactory.getCurrentSession().save(semester)!=null){
                check = true;
            }
            return check;
         }

    }

当我尝试将数据插入数据库时​​,我得到400错误。并且我再次检查了jsp名称与数据库的所有值,我遇到了这些问题。

1 个答案:

答案 0 :(得分:0)

您必须在modelAttribute代码而不是<form:form中使用<form

<form:form method="post" modelAttribute="semesterlist" action="${pageContext.servletContext.contextPath}/addsemeste‌​r" > 
<form:label path="semesterid">Semester ID</form:label> 
<form:input path="semesterid"/>
 <!-- provide the rest of fields --> 

<input type="submit" value="add" /> <input type="reset" value="cancel"/> 
</form:form>

在控制器中:

@RequestMapping(value="/addsemester",method=RequestMethod.POST)
        public String _addsemester(@ModelAttribute("semesterlist") Semester semester){
...........
}