<form class="form-horizontal m-t" id="commentForm" method="post" enctype="multipart/form-data"> <div class="form-group"> <label class="col-sm-2 control-label" style="float:left;"><font>标题:</font></label> <div class="col-sm-6"> <input id="newsTitle" name="newsTitle" minlength="2" type="text" class="form-control" required="" aria-required="true" value=""> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" style="float:left;"><font>关键词:</font></label> <div class="col-sm-6"> <input id="keywords" name="keywords" minlength="2" type="text" class="form-control" required="" aria-required="true" value=""> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" style="float:left;"><font>描述:</font></label> <div class="col-sm-6"> <input id="description" name="description" minlength="2" type="text" class="form-control" required="" aria-required="true" value=""> </div> </div> <div class="form-group"> <label class="col-sm-2 control-label" style="float:left;"><font>详细:</font></label> <div class="col-sm-6"> <textarea id="newsContent" name="newsContent" minlength="2" rows="4" type="text" class="form-control"></textarea> <script type="text/javascript">CKEDITOR.replace('newsContent')</script> </div> </div> <div class="form-group"> <div class="col-sm-6 col-sm-offset-2"> <button class="btn btn-primary" type="submit" onclick="return btnsubmit()">提交</button> <button class="btn btn-danger" type="button" id="cancel">取消</button> </div> </div> </form>
This is my form submission code. Of course, in the submitting hand, I use js to change the function. The key point is that I ca n’t get the parameters after I enter the controller. I use the parameters directly obtained by the object in the background.
@RequestMapping(value="/AddorEdit",produces = {"application/text;charset=UTF-8"}) public String AddorEdit(NewsManager model,HttpServletRequest request)throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); String userName = request.getParameter("newsTitle"); var user = ManageProvider.getProvider().Current(request); if (user==null||user.getId()==0) { return "login/login"; } SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); java.util.Date time=null; try { time= df.parse(df.format(new Date())); } catch (ParseException e) { e.printStackTrace(); } model.setCreateDate(time); model.setCreateName(user.getUserName()); model.setState(1); ApplicationContext ctx = new ClassPathXmlApplicationContext( "file:D:/Program Files/JetBrains/javaproject08/web/WEB-INF/applicationContext.xml"); NewsManagerServiceImpl newsmanagerserviceimpl=(NewsManagerServiceImpl)ctx.getBean("newsManagerservice"); newsmanagerserviceimpl.insertNewsManager(model); return "news/newsindex"; }
Solution: There is one more in the front page code
enctype = "multipart / form-data" This attribute can be removed,
then the parameters passed are garbled
I add in controller
@RequestMapping (value = "/ AddorEdit", produces = {"application / text; charset = UTF-8"}), the page also added encoding
<% @ page contentType = "text / html; charset = UTF-8" language = "java"%>
<meta http-equiv = "Content-Type" content = "text / html; charset = utf-8" />
There are many others mentioned online
request.setCharacterEncoding ("UTF-8"); It is ok to add a paragraph, but it is useless in my project.
Finally, I saw on the website that such a code was added to web.xml (recommended at the front, Mine is so good) perfectly solved
<filter> <filter-name>characterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>