SSH框架集成统一编码格式UTF-8,从jsp传到后台action中的中文还是正确的,但是插入mysql数据库后,就是??了,这是为什么呢

时间:2022-07-10 23:48:28
list.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>岗位列表</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
    <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
    <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/DataShowManager.js" charset="utf-8"></script>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
    <script type="text/javascript">
    </script>
</head>
<body>
 
<div id="Title_bar">
    <div id="Title_bar_Head">
        <div id="Title_Head"></div>
        <div id="Title"><!--页面标题-->
            <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位管理
        </div>
        <div id="Title_End"></div>
    </div>
</div>

<div id="MainArea">
    <table cellspacing="0" cellpadding="0" class="TableStyle">
       
        <!-- 表头-->
        <thead>
            <tr align="CENTER" valign="MIDDLE" id="TableTitle">
             <td width="200px">岗位名称</td>
                <td width="300px">岗位说明</td>
                <td>相关操作</td>
            </tr>
        </thead>

<!--显示数据列表-->
         <tbody id="TableData" class="dataContainer" datakey="roleList">
        <s:iterator value="list">
<tr class="TableDetail1 template">
<td>${name}&nbsp;</td>
<td>${description}&nbsp;</td>
<td><s:a onclick="return window.confirm('确定删除当前记录吗?')" action="role_delete?id=%{    id    }" namespace="">删除</s:a>
<s:a action="role_editUI?id=%{    id    }" namespace="">修改</s:a>
<s:a href="setPrivilegeUI.html">设置权限</s:a>
</td>
</tr>
        </s:iterator>
        </tbody>
    </table>
    
    <!-- 其他功能超链接 -->
    <div id="TableTail">
        <div id="TableTail_inside">
            <a href="saveUI.html"><img src="${pageContext.request.contextPath}/style/images/createNew.png" /></a>
        </div>
    </div>
</div>
</body>
</html>


edit.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>岗位设置</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript" src="${pageContext.request.contextPath}/script/jquery.js"></script>
    <script language="javascript" src="${pageContext.request.contextPath}/script/pageCommon.js" charset="utf-8"></script>
    <script language="javascript" src="${pageContext.request.contextPath}/script/PageUtils.js" charset="utf-8"></script>
<script language="javascript" src="${pageContext.request.contextPath}/script/DataShowManager.js" charset="utf-8"></script>
    <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/pageCommon.css" />
    <script type="text/javascript"> 
    </script>
</head>
<body> 

<!-- 标题显示 -->
<div id="Title_bar">
    <div id="Title_bar_Head">
        <div id="Title_Head"></div>
        <div id="Title"><!--页面标题-->
            <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 岗位设置
        </div>
        <div id="Title_End"></div>
    </div>
</div>

<!--显示表单内容-->
<div id="MainArea">
    <s:form action="role_edit" namespace="/"  method="post">
     <s:hidden name="id"></s:hidden>
        <div class="ItemBlock_Title1"><!-- 信息说明<DIV CLASS="ItemBlock_Title1">
         <IMG BORDER="0" WIDTH="4" HEIGHT="7" SRC="${pageContext.request.contextPath}/style/blue/images/item_point.gif" /> 岗位信息 </DIV>  -->
        </div>
        
        <!-- 表单内容显示 -->
        <div class="ItemBlockBorder">
            <div class="ItemBlock">
                <table cellpadding="0" cellspacing="0" class="mainForm">
                    <tr>
                        <td width="100">岗位名称</td>
                        <td>
                        <s:textfield name="name" cssClass="InputStyle"></s:textfield>
                        *
                        </td>
                    </tr>
                    <tr>
                        <td>岗位说明</td>
                        <td>
                         <s:textarea name="description" cssClass="TextareaStyle"></s:textarea>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        
        <!-- 表单操作 -->
        <div id="InputDetailBar">
            <input type="image" src="${pageContext.request.contextPath}/style/images/save.png"/>
            <a href="javascript:history.go(-1);"><img src="${pageContext.request.contextPath}/style/images/goBack.png"/></a>
        </div>
    </s:form>
</div>

</body>
</html>



web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 通过上下文参数指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>

<!-- 配置spring的上下文载入器监听器 ,项目启动时加载spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置struts2的前端控制器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!-- 读取属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>

</bean>

<!-- 本地会话工厂bean -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 注入hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<!-- hibernate映射文件的位置 -->
<property name="mappingDirectoryLocations">
<list>
<value>classpath:cn/itcast/oa/domain</value>
</list>
</property>
</bean>

<!-- Hibernate事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<!-- 对注解提供支持 -->
<context:annotation-config />
<!-- 组件扫描 -->
<context:component-scan base-package="cn.itcast.oa" />

<!-- 注解驱动 -->
<tx:annotation-driven transaction-manager="txManager" />

</beans>


struts.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<constant name="struts.devMode" value="true" />
<!-- 将对象工厂指定为spring -->
<constant name="struts.objectFactory" value="spring" />
<!-- struts的Action访问后缀 -->
<constant name="struts.action.extension" value="do" />
<!-- 制定表单主题 -->
<constant name="struts.ui.theme" value="simple"></constant>

<package name="default" namespace="/" extends="struts-default">
<action name="bookAction" class="bookAction"></action>

<!-- 岗位管理Action -->
<action name="role_*" class="roleAction" method="{1}">
<result name="list">/WEB-INF/jsp/role/list.jsp</result>
<result name="toList" type="redirectAction">
<param name="actionName">role_list</param>
<param name="namespace">/</param>
</result>
<result name="editUI">/WEB-INF/jsp/role/edit.jsp</result>
</action>
</package>
</struts>

5 个解决方案

#1


数据库编码的问题吧

#2


mysql默认不支持中文。

#3


设置mysql数据库的默认编码为UTF-8

配置数据库URL中加入编码设置,设置为UTF-8  

#4


首先你先确定你的数据库是不是用utf-8,其次在连接的URL后面比如这么写jdbc:mysql://localhost/activiti?useUnicode=true&amp;characterEncoding=UTF-8
url后面加这东西
?useUnicode=true&amp;characterEncoding=UTF-8

#5


查看数据库编码格式,
  插入 资料时,转换成对于的编码格式。

#1


数据库编码的问题吧

#2


mysql默认不支持中文。

#3


设置mysql数据库的默认编码为UTF-8

配置数据库URL中加入编码设置,设置为UTF-8  

#4


首先你先确定你的数据库是不是用utf-8,其次在连接的URL后面比如这么写jdbc:mysql://localhost/activiti?useUnicode=true&amp;characterEncoding=UTF-8
url后面加这东西
?useUnicode=true&amp;characterEncoding=UTF-8

#5


查看数据库编码格式,
  插入 资料时,转换成对于的编码格式。