在spring-mvc应用程序中,出现以下错误:
HTTP Status 400 - Required String parameter 'ownerID' is not present
当我尝试加载以下网址时:
http://localhost:8080/petclinic/owners
期待着
http://localhost:8080/petclinic/owners?ownerID=1 or some other valid number.
如何更改代码,以便在指定ownerID时加载预期的内容,但在未指定ownerID或ownerID无效时也加载其他指定内容?我需要在jsp或控制器类中的jstl中进行更改吗?
这是OwnerController.java中处理/ owners url模式的部分:
@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(@RequestParam("ownerID") String ownerId, Owner owner, BindingResult result, Map<String, Object> model) {
Collection<Owner> results = this.clinicService.findOwnerByLastName("");
model.put("selections", results);
int ownrId = Integer.parseInt(ownerId);
Owner sel_owner = this.clinicService.findOwnerById(ownrId);//jim added this
model.put("sel_owner",sel_owner);
return "owners/ownersList";
}
这是ownerList.jsp的代码。请注意,它不接受我尝试用来解决此问题的select ... when块:
<body>
<div class="container">
<table>
<tr>
<td></td>
<td>
<spring:url value="/owners/new" var="owner_newUrl"></spring:url>
<a href="${fn:escapeXml(owner_newUrl)}" class="btn btn-info" >Add New Owner</a>
</td>
</tr>
<tr>
<td width=160 valign="top">
<datatables:table id="owners" data="${selections}" cdn="true" row="owner" theme="bootstrap2"
cssClass="table table-striped" paginate="true" info="false"
cssStyle="width: 150px;" align="left" >
<datatables:column title="Name" cssStyle="width: 150px;" display="html">
<spring:url value="/owners?ownerID={ownerId}" var="ownerUrl">
<spring:param name="ownerId" value="${owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(ownerUrl)}"><c:out value="${owner.firstName} ${owner.lastName}"/></a>
</datatables:column>
</datatables:table>
</td>
<c:choose>
<c:when test="${!empty sel_owner}">
<td valign="top">
<table class="table table-striped" style="width:600px;">
<tr>
<td><b><c:out value="${sel_owner.firstName} ${sel_owner.lastName}"/></b></td>
<td></td>
</tr>
<tr>
<td><c:out value="${sel_owner.address}"/></td>
<td><c:out value="${sel_owner.city}"/></td>
</tr>
<tr>
<td><c:out value="${sel_owner.telephone}"/></td>
<td></td>
</tr>
<tr>
<td colspan=2>
<spring:url value="/owners/{ownerId}/edit" var="owner_editUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_editUrl)}" class="btn btn-info" >Edit This Owner</a>
<spring:url value="/owners/{ownerId}/pets/new" var="owner_newpetUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_newpetUrl)}" class="btn btn-info" >Add New Pet</a>
<spring:url value="/owners?ownerID={ownerId}&type=cats" var="owner_catsUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_catsUrl)}" class="btn btn-info" >Show Cats</a>
<spring:url value="/owners?ownerID={ownerId}&type=dogs" var="owner_dogsUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_dogsUrl)}" class="btn btn-info" >Show Dogs</a>
<spring:url value="/owners?ownerID={ownerId}&type=all" var="owner_allUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_allUrl)}" class="btn btn-info" >Show All Pets</a>
</td>
</tr>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'all')}">
<tr>
<td colspan=2>
<p>Pets</p>
<datatables:table id="pets" data="${sel_owner.pets}" cdn="true" row="pet" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${pet.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${pet.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${pet.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'cats')}">
<tr>
<td colspan=2>
<p>Cats</p>
<datatables:table id="cats" data="${sel_owner.cats}" cdn="true" row="cat" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${cat.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${cat.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${cat.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'dogs')}">
<tr>
<td colspan=2>
<p>Dogs</p>
<datatables:table id="dogs" data="${sel_owner.dogs}" cdn="true" row="dog" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${dog.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${dog.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${dog.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
</table>
</td>
</tr>
</c:when>
<c:otherwise>
Print this content if there is no ownerID.
</c:otherwise>
编辑:
根据Sotirios和Loc的建议,我将代码更改为以下代码,但仍然抛出相同的错误。有人可以明确地告诉我如何解决此问题,以便/ owners网址模式可以提供“一些文本”以及所有者列表,而不是抛出错误或提供默认所有者的详细信息吗?
这是OwnerController.java中的相关方法:
public String processFindForm(@RequestParam(value="ownerID", required=false) String ownerId, Owner owner, BindingResult result, Map<String, Object> model) {
Collection<Owner> results = this.clinicService.findOwnerByLastName("");
model.put("selections", results);
boolean ownerIDValid;
if(ownerId==null){ownerIDValid = false;}
else{//I added this if test
ownerIDValid=true;
Integer ownrId = null;
try {
ownrId = Integer.parseInt(ownerId);
Owner sel_owner = this.clinicService.findOwnerById(ownrId);//jim added this
if (sel_owner == null) ownerIDValid = false;
model.put("sel_owner",sel_owner);
catch( Exception ex) {
ownerIDValid = false;
}
}
model.put("ownerIDValid", ownerIDValid);
return "owners/ownersList";
}
这是ownerList.jsp:
<html><head>tags and stuff</head>
<body>
<div class="container">
<table>
<tr>
<td></td>
<td>
<spring:url value="/owners/new" var="owner_newUrl"></spring:url>
<a href="${fn:escapeXml(owner_newUrl)}" class="btn btn-info" >Add New Owner</a>
</td>
</tr>
<tr>
<td width=160 valign="top">
<datatables:table id="owners" data="${selections}" cdn="true" row="owner" theme="bootstrap2"
cssClass="table table-striped" paginate="true" info="false"
cssStyle="width: 150px;" align="left" >
<datatables:column title="Name" cssStyle="width: 150px;" display="html">
<spring:url value="/owners?ownerID={ownerId}" var="ownerUrl">
<spring:param name="ownerId" value="${owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(ownerUrl)}"><c:out value="${owner.firstName} ${owner.lastName}"/></a>
</datatables:column>
</datatables:table>
</td>
<c:if test="${ownerIDValid}">
<td valign="top">
<table class="table table-striped" style="width:600px;">
<tr>
<td><b><c:out value="${sel_owner.firstName} ${sel_owner.lastName}"/></b></td>
<td></td>
</tr>
<tr>
<td><c:out value="${sel_owner.address}"/></td>
<td><c:out value="${sel_owner.city}"/></td>
</tr>
<tr>
<td><c:out value="${sel_owner.telephone}"/></td>
<td></td>
</tr>
<tr>
<td colspan=2>
<spring:url value="/owners/{ownerId}/edit" var="owner_editUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_editUrl)}" class="btn btn-info" >Edit This Owner</a>
<spring:url value="/owners/{ownerId}/pets/new" var="owner_newpetUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_newpetUrl)}" class="btn btn-info" >Add New Pet</a>
<spring:url value="/owners?ownerID={ownerId}&type=cats" var="owner_catsUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_catsUrl)}" class="btn btn-info" >Show Cats</a>
<spring:url value="/owners?ownerID={ownerId}&type=dogs" var="owner_dogsUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_dogsUrl)}" class="btn btn-info" >Show Dogs</a>
<spring:url value="/owners?ownerID={ownerId}&type=all" var="owner_allUrl">
<spring:param name="ownerId" value="${sel_owner.id}"/>
</spring:url>
<a href="${fn:escapeXml(owner_allUrl)}" class="btn btn-info" >Show All Pets</a>
</td>
</tr>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'all')}">
<tr>
<td colspan=2>
<p>Pets</p>
<datatables:table id="pets" data="${sel_owner.pets}" cdn="true" row="pet" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${pet.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${pet.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${pet.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'cats')}">
<tr>
<td colspan=2>
<p>Cats</p>
<datatables:table id="cats" data="${sel_owner.cats}" cdn="true" row="cat" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${cat.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${cat.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${cat.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
<c:if test="${fn:endsWith(requestScope['javax.servlet.forward.query_string'], 'dogs')}">
<tr>
<td colspan=2>
<p>Dogs</p>
<datatables:table id="dogs" data="${sel_owner.dogs}" cdn="true" row="dog" theme="bootstrap2"
cssClass="table table-striped" paginate="false" info="false" filter="false"
cssStyle="width: 350px;" align="left" >
<datatables:column title="Name" cssStyle="width: 200px;" display="html">
<c:out value="${dog.name}"/>
</datatables:column>
<datatables:column title="BirthDate" cssStyle="width: 300px;" display="html">
<joda:format value="${dog.birthDate}" pattern="yyyy-MM-dd"/>
</datatables:column>
<datatables:column title="Type" cssStyle="width: 200px;" display="html">
<c:out value="${dog.type.name}"/>
</datatables:column>
</datatables:table>
</td>
</tr>
</c:if>
</table>
</td>
</tr>
</c:if>
<c:if test="${!ownerIDValid}">
Print this content if there is no ownerID.
</c:if>
</table>
</div>
</body>
</html>
最佳答案
编写2种方法。每种方法提供不同的映射。
@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(Owner owner, BindingResult result, Map<String, Object> model)
{
String ownerId = DEFAULT value;
}
@RequestMapping(value = "/owners/{ownerID}", method = RequestMethod.GET)
public String processFindForm(@RequestParam("ownerID") String ownerId, Owner owner, BindingResult result, Map<String, Object> model)
{
}
第一个服务:“ / petclinic / owners”
第二个则是'/ petclinic / owners / 100'; // 100是ownerID
或1这样的方法:
@RequestMapping(value = { "/owners", "/owners/{ownerID}" }, method = RequestMethod.GET)
public String processFindForm(@RequestParam(value="ownerID", required=false) String ownerId, Owner owner, BindingResult result, Map<String, Object> model)
{
}