【发布时间】:2016-11-17 11:11:20
【问题描述】:
我正在尝试获取下拉列表中选定元素的值以将其保存到我的数据库中,但数据被传递为“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>
我的控制器(顺便说一句,你们都可能看到它具有属性“required = false”,是的,因为它可以为空,但该逻辑我稍后会应用它,一旦我从所选选项中获得值):
@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 - ****************************
这是我通过查看“查看页面源代码”在网络上获得的部分内容,我想获取该值并仅以纯文本形式对其进行测试。
<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>
【问题讨论】:
-
nup... 因为在该列表中,我的下拉列表中显示了银行
-
您可以在控制器方法中添加一个记录器以显示所选项目的值..
-
FuSsa,对不起,我不明白.. 你是什么意思?我正在登录这个“logger.error(”BANCO PADREEEEEE“+bancoPadre”这是形式:选择我从我的观点得到什么
-
'resultList' 是什么列表?意思是类型?因为描述是你班级中的字符串
-
这是一个列表
,而 Banco 是模型。我在上面粘贴但不完整的那个
标签: spring spring-mvc