我正在使用Spring MVC框架,postgresSQL数据库和休眠ORM。
我遇到了错误。
由于某些原因,服务器无法或不会处理请求 被视为客户端错误(例如,格式错误的请求) 语法,无效的请求消息框架或欺骗性请求 路由)。
这是我的控制者:::
@RequestMapping(value = "/createdistrict")
public ModelAndView createdistrict(Locale locale, Model model) {
List<Division> allDivisionList = new ArrayList<Division>();
allDivisionList = this.districtService.listdivisions() ;
Map<Integer,String> allDivision = new LinkedHashMap<Integer,String>();
for( int i=0 ; i < allDivisionList.size() ; i++) {
//System.out.println(" division id ::::::::::" + allDivisionList.get(i).getId() + " division name:::::::::" + allDivisionList.get(i).getName());
allDivision.put(allDivisionList.get(i).getId() , allDivisionList.get(i).getName());
}
return new ModelAndView("createdistrict" , "allDivision" , allDivision);
}
@PostMapping("/adddistrict")
public String addDistrict(
@ModelAttribute("district")District district,
Model model,
@RequestParam("division") int division) {
System.out.println("id:::::::::::::::::::" + division);
this.districtService.adddistrict(district, division);
return "redirect:districtlist";
}
这是我的模型:::
@Entity
@Table(name = "district")
public class District implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private Integer id;
@ManyToOne
@JoinColumn(name = "division_id", referencedColumnName = "id")
private Division division;
@Column(name="name")
private String name;
}
这是jsp页面::::::
<form method="POST" action="adddistrict" modelAttribute="district">
<table class="create-table table table-hover">
<tr>
<td>
Division
</td>
<td>
<select id="division" name="division" path="division.id">
<c:forEach items="${allDivision}" var="allDivision">
<option class="dropdivision" value="${allDivision.key}">${allDivision.value }</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>
Name
</td>
<td>
<input type="text" id="name" name="name" path="name"></input>
</td>
</tr>
<!-- End of single tr -->
</table>
<!-- End of table -->
<div class="button-set text-right">
<button type="submit" class="btn site-btn filled-btn" id="savebutton">save</button>
<a href="" class="btn site-btn filled-btn">cancel</a>
<a href="" class="btn site-btn filled-btn">reset</a>
</div>
<!-- End of button-set -->
</form>
我该如何解决问题?
答案 0 :(得分:0)
在您的jsp页面中,您应该使用taglibs:
首先包括相应的标签库(例如,春天的标签库):
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
您的JSP中至少需要一个:
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
然后在您的jsp中使用它们:
<form ...></form>
-Tag代替<form:form></form:form>
<input>
代替<form:input>
,依此类推。这是一个(不完整的)示例,应该为您工作:
<form:form action="addDistrict" method="post" cssClass="" modelAttribute="district">
<form:label cssClass="control-label" path="name">Name of District</form:label>
<div class="controls">
<t:input cssClass="someCssClass" path="name" id="titelTypeahead" placeholder="(e. g. Ottakring)" />
<form:errors element="div" path="name" cssClass="alert alert-error" />
</div>
</form:form>