gpt4 book ai didi

java - 提交表单时出错-客户端发送的请求在语法上不正确-Spring DAO

转载 作者:行者123 更新时间:2023-11-29 20:20:29 25 4
gpt4 key购买 nike

起初存在一个问题,就是无法识别一些使用驼峰式大小写和下划线命名的 bean 属性。所以我删除了它们并重新生成了实体类。尽管该问题现已解决,但当我提交以下表单时,它会出现错误“客户端发送的请求在语法上不正确”,HTTP 状态为 400。尽管有类似的问题,但我找不到我的问题的答案。

JSP页面

    <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form:form action="addDistrict" method="POST" modelAttribute="district">
<form:select class="form-control" style="border-radius: 0px" placeholder="ADA Zone" path="adaid">
<c:forEach items="${adas}" var="result">
<form:option value="${result.adaid}">${result.adaname}</form:option>
</c:forEach>
</form:select>
<form:input path="districtid" type="hidden"></form:input>
<form:input path="districtname" placeholder="District Name:"></form:input>
<button type="submit" class="btn btn-app"><i class="material-icons md-18" style="font-size:1.0em;">mode_edit</i></button>
</form:form>
</body>
</html>

Controller

package edu.cdms.project.dao.impl;

import edu.cdms.project.dao.BranchDAO;
import edu.cdms.project.orm.Adazone;
import edu.cdms.project.orm.Ascenter;
import edu.cdms.project.orm.District;
import edu.cdms.project.orm.Ds;
import java.util.ArrayList;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class BranchDAOImpl implements BranchDAO{

@Autowired
private SessionFactory sessionFactory;

public Session getSession(){
return sessionFactory.openSession();
}


public ArrayList<Adazone> getADAList(){
Session session=getSession();
session.beginTransaction();

Query namedQuery=session.getNamedQuery("Adazone.findAll");
ArrayList<Adazone> adaZones=(ArrayList<Adazone>) namedQuery.list();
session.getTransaction().commit();
session.close();
return adaZones;
}


public boolean addDistrict(District district) {
Session session=getSession();
session.beginTransaction();
session.save(district);
session.getTransaction().commit();
session.close();
return false;
}

}

服务

package edu.cdms.project.service;

import edu.cdms.project.orm.Adazone;
import edu.cdms.project.orm.Ascenter;
import edu.cdms.project.orm.District;
import edu.cdms.project.orm.Ds;
import java.util.ArrayList;

public interface BranchService {

public ArrayList<Adazone> getADAList();

public boolean addDistrict(District district);

}

已实现的服务

package edu.cdms.project.service.impl;

import edu.cdms.project.dao.BranchDAO;
import edu.cdms.project.orm.Adazone;
import edu.cdms.project.orm.Ascenter;
import edu.cdms.project.orm.District;
import edu.cdms.project.orm.Ds;
import edu.cdms.project.service.BranchService;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class BranchServiceImpl implements BranchService{

@Autowired
private BranchDAO branchDAO;


public ArrayList<Adazone> getADAList() {
return branchDAO.getADAList();
}

public boolean addDistrict(District district){
return branchDAO.addDistrict(district);
}

}

DAO

package edu.cdms.project.dao;

import edu.cdms.project.orm.Adazone;
import edu.cdms.project.orm.Ascenter;
import edu.cdms.project.orm.District;
import edu.cdms.project.orm.Ds;
import java.util.ArrayList;

public interface BranchDAO {

public ArrayList<Adazone> getADAList();

public boolean addDistrict(District district);


}

实现 DAO

    package edu.cdms.project.contrl;

import edu.cdms.project.orm.Adazone;
import edu.cdms.project.orm.Ailist;
import edu.cdms.project.orm.Ascenter;
import edu.cdms.project.orm.District;
import edu.cdms.project.orm.Ds;
import edu.cdms.project.service.BranchService;
import java.util.ArrayList;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@Controller
@RequestMapping("/")
public class BranchController {

@Autowired
private BranchService branchService;


@RequestMapping(value = "/addDistrict", method = RequestMethod.POST)
public String addDistrict(@Valid District district, ModelMap map){

branchService.addDistrict(district);
ArrayList<Adazone> adaZones=branchService.getADAList();
map.addAttribute("adas", adaZones);
map.addAttribute("district", district);
return "test";
}
}

ORMAdazone.java

package edu.cdms.project.orm;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


@Entity
@Table(name = "adazone")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Adazone.findAll", query = "SELECT a FROM Adazone a"),
@NamedQuery(name = "Adazone.findByAdaid", query = "SELECT a FROM Adazone a WHERE a.adaid = :adaid"),
@NamedQuery(name = "Adazone.findByAdaname", query = "SELECT a FROM Adazone a WHERE a.adaname = :adaname")})
public class Adazone implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "adaid")
private Integer adaid;
@Size(max = 50)
@Column(name = "adaname")
private String adaname;
@OneToMany(mappedBy = "adaid", fetch = FetchType.LAZY)
private List<Ailist> ailistList;
@OneToMany(mappedBy = "adaid", fetch = FetchType.LAZY)
private List<District> districtList;

public Adazone() {
}

public Adazone(Integer adaid) {
this.adaid = adaid;
}

public Integer getAdaid() {
return adaid;
}

public void setAdaid(Integer adaid) {
this.adaid = adaid;
}

public String getAdaname() {
return adaname;
}

public void setAdaname(String adaname) {
this.adaname = adaname;
}

@XmlTransient
public List<Ailist> getAilistList() {
return ailistList;
}

public void setAilistList(List<Ailist> ailistList) {
this.ailistList = ailistList;
}

@XmlTransient
public List<District> getDistrictList() {
return districtList;
}

public void setDistrictList(List<District> districtList) {
this.districtList = districtList;
}

@Override
public int hashCode() {
int hash = 0;
hash += (adaid != null ? adaid.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Adazone)) {
return false;
}
Adazone other = (Adazone) object;
if ((this.adaid == null && other.adaid != null) || (this.adaid != null && !this.adaid.equals(other.adaid))) {
return false;
}
return true;
}

@Override
public String toString() {
return "edu.cdms.project.orm.Adazone[ adaid=" + adaid + " ]";
}

}

区.java

package edu.cdms.project.orm;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;


@Entity
@Table(name = "district")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "District.findAll", query = "SELECT d FROM District d"),
@NamedQuery(name = "District.findByDistrictid", query = "SELECT d FROM District d WHERE d.districtid = :districtid"),
@NamedQuery(name = "District.findByDistrictname", query = "SELECT d FROM District d WHERE d.districtname = :districtname")})
public class District implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "districtid")
private Integer districtid;
@Size(max = 50)
@Column(name = "districtname")
private String districtname;
@OneToMany(mappedBy = "districtid", fetch = FetchType.LAZY)
private List<Ailist> ailistList;
@OneToMany(mappedBy = "districtid", fetch = FetchType.LAZY)
private List<Ds> dsList;
@JoinColumn(name = "adaid", referencedColumnName = "adaid")
@ManyToOne(fetch = FetchType.LAZY)
private Adazone adaid;

public District() {
}

public District(Integer districtid) {
this.districtid = districtid;
}

public Integer getDistrictid() {
return districtid;
}

public void setDistrictid(Integer districtid) {
this.districtid = districtid;
}

public String getDistrictname() {
return districtname;
}

public void setDistrictname(String districtname) {
this.districtname = districtname;
}

@XmlTransient
public List<Ailist> getAilistList() {
return ailistList;
}

public void setAilistList(List<Ailist> ailistList) {
this.ailistList = ailistList;
}

@XmlTransient
public List<Ds> getDsList() {
return dsList;
}

public void setDsList(List<Ds> dsList) {
this.dsList = dsList;
}

public Adazone getAdaid() {
return adaid;
}

public void setAdaid(Adazone adaid) {
this.adaid = adaid;
}

@Override
public int hashCode() {
int hash = 0;
hash += (districtid != null ? districtid.hashCode() : 0);
return hash;
}

@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof District)) {
return false;
}
District other = (District) object;
if ((this.districtid == null && other.districtid != null) || (this.districtid != null && !this.districtid.equals(other.districtid))) {
return false;
}
return true;
}

@Override
public String toString() {
return "edu.cdms.project.orm.District[ districtid=" + districtid + " ]";
}

}

最佳答案

在您的 District 类中,您有 Adazone 字段。在您的 jsp 页面中,您传递的不是对象而是一个整数,即 value="${result.adaid}"

您可以进行以下更改吗?请告诉我这些更改是否对您有用。在您的 jsp 页面中传递一个对象而不是 Integer

<c:forEach items="${adas}" var="result">
<form:option value="${result}">${result.adaname}</form:option>
</c:forEach>

那么你必须声明两个转换器

public class IntegerToAdazone implements Converter<Integer, Adazone > {

@Override
public Adazone convert(Integer source) {
Adazone a = new Adazone(source);
return a;
}

}

public class AdazoneToInteger implements Converter<Adazone, Integer> {

@Override
public Integer convert(Adazone source) {
return source.getId();
}

}

然后您必须配置转换服务。看看spring文档http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html (9.5.5 配置ConversionService)

A default ConversionService can convert between strings, numbers, enums, collections, maps, and other common types. To supplement or override the default converters with your own custom converter(s), set the converters property. Property values may implement either of the Converter, ConverterFactory, or GenericConverter interfaces.

<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<set>
<bean class="example.IntegerToAdazone"/>
<bean class="example.AdazoneToInteger"/>
</set>
</property>
</bean>

根据需要更改上面的包(示例)。希望这有帮助

关于java - 提交表单时出错-客户端发送的请求在语法上不正确-Spring DAO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39528402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com