在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吗?
以下是处理/ owner url模式的OwnerController.java的一部分:
@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的代码。请注意,它不接受选择...当我尝试用来解决此问题的块时:
<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的建议,我将我的代码更改为以下内容,但它仍然会抛出相同的错误。有人可以明确告诉我如何解决这个问题,以便/所有者网址模式可以提供“一些文本”以及所有者列表,而不是抛出错误或提供默认所有者的详细信息?
以下是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>
答案 0 :(得分:1)
WRITE 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)
{
}
OR 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)
{
}