我试图在dropdownList中获取所选元素的值,以将其保存到我的数据库中,但数据正在传递' null'。
我已在这些帖子和网络上搜索过但我无法找到答案:How to pass data from <form:select> Spring MVC和How to Pass <Select> and <input> data from View to a Controller (Spring MVC)。
以下是我的观点:
<form:form action="${urlcallBankAddAction}" method="POST" onsubmit="return validar(this)" modelAttribute="banco">
<input type="text" name="bancoNombre" />
<input type="text" name="bancoCodigoVisa" maxlength="8" onkeypress="return isNumber(event);" />
<form:select path="descripcion" name="bancoPadre" id="banks-description" onchange="selectValue()">
<form:options items="${resultList}" itemValue="id" itemLabel="descripcion"/>
</form:select>
</form:form>
我的控制器(顺便说一下,你们都可能会看到它有属性&#39; required = false&#39;,是的因为它可以为null但是这个逻辑我稍后会应用它,一旦我从所选选项中获取值:)
@RequestMapping(value = "/addAction", method = RequestMethod.POST)
public String agregarBanco(HttpServletRequest request,
@RequestParam("bancoNombre") String bancoNombre,
@RequestParam("bancoCodigoVisa") int bancoCodigoVisa,
@RequestParam("bancoCodigoSudeban") String bancoCodigoSudeban,
@RequestParam(value = "bancoPadre", required = false) String bancoPadre,
ModelMap model,
HttpSession session){
try{
String valorSesion = (String) session.getAttribute("acceso");//Se valida el inicio de sesion
if(valorSesion.equals("1")){//Se valida el inicio de sesion
Banco banco = new Banco();
logger.error("****************************");
logger.error("BANCO PADREEEEEE " + bancoPadre + " " + bancoNombre + " " + bancoCodigoVisa);
logger.error("****************************");
banco.setDescripcion(bancoNombre);
banco.setCodigoVisa(bancoCodigoVisa);
banco.setCodigoSudeban(bancoCodigoSudeban);
bancoService.agregarBanco(banco);
model.addAttribute("status", "Banco guardado con exito.");
return "redirect:/banks/details";
} else{
//MANDAR A PAGINA DE ERROR
return "redirect:/unauthorized";
}
} catch(NullPointerException n){//Se valida el inicio de sesion
//MANDAR A PAGINA DE ERROR
return "redirect:/unauthorized";
}
和我的模型以防万一:
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "cod_visa")
private int codigoVisa;
@Column(name = "CodSud")
private String codigoSudeban;
@Column(name = "descripcion")
private String descripcion;
@Column(name = "id_padre")
private Integer idPadre;
当我将数据发送到我的控制器时,您可以看到我并记录我应该从我的视图中获取的那些参数但这就是我得到的
2016-07-14 09:14:21 ERROR BancosController:96 - ****************************
2016-07-14 09:14:21 ERROR BancosController:97 - BANCO PADREEEEEE null test 32423
2016-07-14 09:14:21 ERROR BancosController:98 - ****************************
以下是我通过查看网页来源&#39;在网络上获得的部分内容。我想取值,只是用纯文本来测试它。
<select id="banks-description" name="descripcion" name="bancoPadre" onchange="selectValue()">
<option value="27">100% BANCO, BANCO UNIVERSAL, C.A. DE OSMAN</option>
</select>
希望你们能够伸出援助之手。我不知道我错过了什么。
修改
我添加了整个代码视图
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<spring:url value="/banks/deleteAction" var="urlcallBankDeleteAction"/>
<spring:url value="/banks/addAction" var="urlcallBankAddAction"/>
<form:form action="${urlcallBankAddAction}" method="POST" onsubmit="return validar(this)" modelAttribute="banco">
<div style="color:RGB(0,32,159); font-weight:bold;"><fmt:message key="banks.addTitle" /></div>
<div class="field mandatory">
<spring:message text="${status}" /><br />
<label><fmt:message key="banks.nombre" /></label>
<input type="text" name="bancoNombre" />
</div>
<div class="field mandatory" style="display:inline-block;">
<label><fmt:message key="banks.visaCode" /></label>
<input type="text" name="bancoCodigoVisa" maxlength="8" onkeypress="return isNumber(event);" />
<label><fmt:message key="banks.sudebanCode" /></label>
<input type="text" name="bancoCodigoSudeban" maxlength="4" onkeypress="return isNumber(event);" />
</div>
<br/>
<div class="field mandatory" style="display: inline; width: 150px;">
<label>Entidad Interna: <input type="radio" name="padre" id="entidad-interna" value="interna" onclick="disabledList()" disabled=true checked/></label>
<label>Entidad Externa: <input type="radio" name="padre" id="entidad-externa" value="externa" onclick="enableList()" disabled=true/></label>
</div>
<br><br>
<div class="field mandatory" style="display: inline-block; width: 150px;">
<input type="checkbox" name="tiene-padre" id="tiene-padre" value="tiene-padre" onchange="checkList()" /> Tiene padre
<form:select path="descripcion" name="bancoPadre" id="banks-description" onchange="selectValue()">
<form:options items="${resultList}" itemValue="id" itemLabel="descripcion"/>
</form:select>
</div>
<br><br>
<%-- <form:select path="descripcion" name="bancoPadre" id="banks-description" onchange="selectValue()">
<form:options items="${resultList}" />
</form:select>--%>
<t:button-submit />
</form:form>
<table class="smart" >
<caption><fmt:message key="banks.listTitle" /></caption>
<thead>
<tr>
<th ><fmt:message key="banks.visaCode" /></th>
<th ><fmt:message key="banks.sudebanCode" /></th>
<th ><fmt:message key="banks.nombre" /></th>
<th ><fmt:message key="banks.operation" /></th>
</tr>
</thead>
<c:forEach items="${resultList}" var="c" varStatus="iteracion">
<tbody>
<tr>
<td>${c.codigoVisa}</td>
<td>${c.codigoSudeban}</td>
<td>${c.descripcion}</td>
<td><a href="${urlcallBankDeleteAction}/${c.codigoSudeban}" onclick="return confirm('¿Está seguro de eliminar este banco?');"><fmt:message key="banks.operation1" /></td>
</tr>
</c:forEach>
</tbody>
</table>
答案 0 :(得分:1)
尝试这样的例子:
-webkit-
AppController的:
<form:form method="POST" modelAttribute="user" class="form-horizontal">
<form:input type="hidden" path="id" id="id"/>
<div class="row">
<div class="form-group col-md-12">
<label class="col-md-3 control-lable" for="lastName">Last Name</label>
<div class="col-md-7">
<form:input type="text" path="lastName" id="lastName" class="form-control input-sm" />
<div class="has-error">
<form:errors path="lastName" class="help-inline"/>
</div>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="col-md-3 control-lable" for="email">Email</label>
<div class="col-md-7">
<form:input type="text" path="email" id="email" class="form-control input-sm" />
<div class="has-error">
<form:errors path="email" class="help-inline"/>
</div>
</div>
</div>
</div>
<div class="row">
<div class="form-group col-md-12">
<label class="col-md-3 control-lable" for="userProfiles">Roles</label>
<div class="col-md-7">
<form:select path="userProfiles" items="${roles}" multiple="true" itemValue="id" itemLabel="type" class="form-control input-sm" />
<div class="has-error">
<form:errors path="userProfiles" class="help-inline"/>
</div>
</div>
</div>
</div>
<div class="row">
<div class="form-actions floatRight">
<c:choose>
<c:when test="${edit}">
<input type="submit" value="Update" class="btn btn-primary btn-sm"/> or <a href="<c:url value='/list' />">Cancel</a>
</c:when>
<c:otherwise>
<input type="submit" value="Register" class="btn btn-primary btn-sm"/> or <a href="<c:url value='/list' />">Cancel</a>
</c:otherwise>
</c:choose>
</div>
</div>
</form:form>
答案 1 :(得分:0)
我使用与Mozilla集成的开发人员工具提出了一个解决方案。
问题:我试图从请求中获取一个不存在的值。
@RequestParam(value = "bancoPadre", required = false) String bancoPadre
因此,通过将上面的代码更改为下面的代码,我可以从dropdownList中的所选项中获取值
@RequestParam(value = "descripcion", required = false) String bancoPadre
这是记录值
的日志记录2016-07-14 13:45:35 ERROR BancosController:96 - ****************************
2016-07-14 13:45:35 ERROR BancosController:97 - BANCO PADREEEEEE 30 cleider 2342
2016-07-14 13:45:35 ERROR BancosController:98 - ****************************
这是我意识到我得到了错误参数的图像。