JAVA 提交form表單 后台獲取不到參數及亂碼問題


        <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>

這是我的表單提交代碼,當然在提交的手利用js來改變acction,重點是提交進入控制器之后居然得不到參數,我后台是利用對象直接獲取的參數

    @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";
    }

解決辦法:前台頁面代碼中多了個

enctype="multipart/form-data"這個屬性 去掉了就可以了
然后就是傳過來的參數亂碼了

 

 

 

 我在控制器添加

@RequestMapping(value="/AddorEdit",produces = {"application/text;charset=UTF-8"}),頁面也添加了編碼
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
還有許多網上提到的
request.setCharacterEncoding("UTF-8");說是加段這個就可以了,但在我的項目中沒用
最后再網站上看到了在web.xml中加入這樣一段代碼(推薦放在最前面,我的就是這樣好的)完美解決了
    <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>

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM