zoukankan      html  css  js  c++  java
  • struts2中form表单提交到action乱码

    java1234官网!!!!

    今天遇到一个乱码问题。jsp页面代码如下,页面编码为UTF-8:

     1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
     2 <%@taglib uri="/struts-tags" prefix="s" %>
     3 <%
     4 String path = request.getContextPath();
     5 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
     6 %>
     7 
     8 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
     9 <html>
    10   <head>
    11     <base href="<%=basePath%>">
    12     
    13     <title>My JSP 'Category_input.jsp' starting page</title>
    14     
    15     <meta http-equiv="pragma" content="no-cache">
    16     <meta http-equiv="cache-control" content="no-cache">
    17     <meta http-equiv="expires" content="0">    
    18     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    19     <meta http-equiv="description" content="This is my page">
    20     <!--
    21     <link rel="stylesheet" type="text/css" href="styles.css">
    22     -->
    23 
    24   </head>
    25   
    26   <body>
    27   <form action="admin/Category-update" method="post">
    28   <input type="hidden" name="category.id" value='<s:property value="category.id"/>'>
    29       name:<input name="category.name" value='<s:property value="category.name"/>'/>
    30       description:<textarea name="category.description"><s:property value="category.description"/></textarea>
    31       <input type="submit" value="update" /> 
    32   </form>
    33   </body>
    34 </html>

    web.xml配置如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
     3   <display-name>bbs2</display-name>
     4  <welcome-file-list>
     5     <welcome-file>index.jsp</welcome-file>
     6   </welcome-file-list>
     7   
     8   <filter>
     9         <filter-name>struts2</filter-name>
    10         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    11     </filter>
    12 
    13     <filter-mapping>
    14         <filter-name>struts2</filter-name>
    15         <url-pattern>/*</url-pattern>
    16     </filter-mapping>
    17   
    18 </web-app>

    struts.xml配置如下:

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
     4     "http://struts.apache.org/dtds/struts-2.0.dtd">
     5 
     6 <struts>
     7 
     8     <!-- form表单提交中文乱码 -->
     9     <constant name="struts.i18n.encoding" value="UTF-8" />
    10     <!--<constant name="struts.enable.DynamicMethodInvocation" value="false" />-->
    11     <constant name="struts.devMode" value="true" />
    12     <!--<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>-->
    13     
    14     <package name="front" namespace="/" extends="struts-default">
    15         <!--  <default-action-ref name="Category-list"></default-action-ref>  不能与*-*一起用-->
    16         <action name="Category-list" class="com.lch.bbs2009.action.CategoryAction">
    17             <result>/admin/Category-list.jsp</result>
    18         </action>
    19     </package>
    20     
    21     <package name="admin" namespace="/admin" extends="struts-default">
    22         
    23         <action name="index">
    24             <result>/admin/index.html</result>
    25         </action>
    26         
    27         <action name="*-*" class="com.lch.bbs2009.action.{1}Action" method="{2}">
    28             <result name="success">/admin/{1}-{2}.jsp</result>
    29             <result name="input">/admin/{1}-{2}.jsp</result>
    30         </action>
    31 
    32     </package>
    33 
    34 </struts>

    action代码如下:

     1 package com.lch.bbs2009.action;
     2 
     3 import java.util.List;
     4 
     5 import com.lch.bbs2009.model.Category;
     6 import com.lch.bbs2009.service.CategoryService;
     7 import com.opensymphony.xwork2.ActionSupport;
     8 
     9 /**
    10  * @author licheng
    11  * 
    12  */
    13 public class CategoryAction extends ActionSupport {
    14     /**
    15      * 
    16      */
    17     private List<Category> categories;
    18     private CategoryService categoryService = new CategoryService();
    19     private Category category;
    20     private int id;
    21 
    22     public String list() {
    23         categories = categoryService.queryAll();
    24         return SUCCESS;
    25     }
    26 
    27     public String add() {
    28         categoryService.add(category);
    29         return SUCCESS;
    30     }
    31 
    32     public String update() {
    33         categoryService.update(category);
    34         return SUCCESS;
    35     }
    36 
    37     public String delete() {
    38         categoryService.deleteById(id);
    39         return SUCCESS;
    40     }
    41 
    42     public String addInput() {
    43         return INPUT;
    44     }
    45 
    46     public String updateInput() {
    47         this.category = this.categoryService.loadById(id);
    48         return INPUT;
    49     }
    50     
    51     public List<Category> getCategories() {
    52         return categories;
    53     }
    54 
    55     public void setCategories(List<Category> categories) {
    56         this.categories = categories;
    57     }
    58 
    59     public CategoryService getCategoryService() {
    60         return categoryService;
    61     }
    62 
    63     public void setCategoryService(CategoryService categoryService) {
    64         this.categoryService = categoryService;
    65     }
    66 
    67     public Category getCategory() {
    68         return category;
    69     }
    70 
    71     public void setCategory(Category category) {
    72         this.category = category;
    73     }
    74 
    75     public int getId() {
    76         return id;
    77     }
    78 
    79     public void setId(int id) {
    80         this.id = id;
    81     }
    82 
    83 }

    通过debug模式调试,发现action中接收到的值就是乱码的。

    在网上找解决办法

    1.检查页面编码,服务器编码,数据库编码是否一致。我的均为UTF-8;

    2.在你配置文件的前面写上<constant name="struts.i18n.encoding" value="UTF-8" />。我写了,无效;

    3.在struts.properties中添加

    struts.locale=zh_CN
    struts.i18n.encoding=UTF-8 以此设定locale、encoding字符集。

    补充:struts.properties是可以不要的!!struts2的默认配置文件为default.properties,位于struts2-core-2.x.x.jar的org.apache.struts2包下面。如果一个项目中一个属性同时都在两个文件中配置了,它有一个加载顺序:先加载struts.xml,再加载struts.properties。也就是说struts.properties可以覆盖struts.xml里面的配置。具体是否要用struts.properties得视具体情况而定。

    我添加了此文件并修改了,结果无效。

    4.在web.xml中加入

     1 <!-- zh-cn encoding -->
     2     <filter>
     3         <filter-name>struts-cleanup</filter-name>
     4         <filter-class>
     5             org.apache.struts2.dispatcher.ActionContextCleanUp
     6         </filter-class>
     7     </filter>
     8     <filter-mapping>
     9         <filter-name>struts-cleanup</filter-name>
    10         <url-pattern>/*</url-pattern>
    11     </filter-mapping>

    补充:web.xml中Encodingfilter的位置应该在Struts2的FilterDispatcher之前,因为要先调整字符集,然后进入Action.按照Struts2的API,filter的顺序是 :

    struts-cleanup filter
    SiteMesh filter
    FilterDispatcher

    结果无效

    5.表单提交都是通过servlet调post()或get()方法处理的,再把结果返回到客户端。写个过滤类,表单提交都先到servlet请求,如果在请求前加个过滤器,将字符都转为中文再进行处理

    public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException {

    HttpServletRequest request_1 = (HttpServletRequest) request;
    HttpServletResponse response_1 = (HttpServletResponse) response;

    // 转换编码,并且加上去缓存
    request_1.setCharacterEncoding("GBK");
    response_1.setCharacterEncoding("GBK");
    chain.doFilter(request, response);

    }我像这样建一个servlet之后,上面都是红叉叉!!

    谁能教教我怎么解决,谢了!!

    我坑爹的搞了一下午,最后问题解决了版本问题!!!!!

     问题解决了,struts版本问题
    Struts2.1.8版本之前有个Bug ,中文乱码。貌似搞成GBK可以,不想纠结了,直接换jar包吧!!记得马士兵老师之前讲过的,可惜我忘了。虽然弄了一下午,但还是学到了不少。呼,在此感谢小锋老师远程帮我解决了问题!!!!!
  • 相关阅读:
    go案例:客户管理系统流程 mvc模式 分层设计
    珠峰2016,第9期 vue.js 笔记部份
    前后端分离电商,业务逻辑部份
    'Specifying a namespace in include() without providing an app_name '报错解决
    vue2.0 前端框架
    vue项目实战
    电商网前后端分离数据表设计部份
    djang2.1教育平台02
    django框架开发流程
    测试的艺术:测试用例的设计
  • 原文地址:https://www.cnblogs.com/ligui989/p/3219773.html
Copyright © 2011-2022 走看看