我正在使用spring mvc和mysql,我的问题是,当我使用spring的form标签保存数据时,编码包括像“à,á,é,ã”这样的字母,当我将这些数据从数据库返回到输入字段时,但是当我合并新数据时,编码后的字符无法通过UTF-8到达控制器,控制器会重新使用“Combustão”而不是“Combustão”
这是我的表格:
<form action="${pageContext.request.contextPath}/catalog/carro/saveedit"
method="post">
<div class="form-group">
<label class="col-md-4" for="nome">Carro:</label>
<input class="form-control" type="text" name="nome"
id="nome" value="${carroEditable.nome}"/>
</div>
<div class="form-group">
<label for="categoria" class="col-md-4">Categoria:</label>
<select name="categoria" id="categoria"
class="form-control">
<option selected value="${carroEditable.categoria}">
${carroEditable.categoria}
</option>
<option value="Combustão">Combustão</option>
<option value="Elétrico">Elétrico</option>
</select>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default btn-block">
Salvar
</button>
</div>
<input type="hidden" name="idCarro"
value="${carroEditable.idCarro}"/>
</form>
和我的控制器
@RequestMapping(value = "/catalog/carro/saveedit",
method = RequestMethod.POST)
public String merge(ServletRequest request)
throws ServletRequestBindingException, UnsupportedEncodingException {
Integer id = ServletRequestUtils.getIntParameter(request, "idCarro");
String categoria = ServletRequestUtils.getRequiredStringParameter
(request,
"categoria");
String nome = ServletRequestUtils.getRequiredStringParameter(request,
"nome");
Carros carros = carrosRepository.findOne(id);
carros.setNome(nome);
carros.setCategoria(categoria);
carrosRepository.saveAndFlush(carros);
return "redirect:/catalog/carro/gerencia";
我也试过
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
表格标签
content="text/html; charset=UTF-8"
答案 0 :(得分:1)
尝试将spring CharacterEncodingFilter添加到您的web.xml。
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>