1.
struts-config.xml4
<?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>
/WEB-INF/applicationContext-*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置得到上下文路径的listener -->
<listener>
<listener-class>
tool.ContextPathListener
</listener-class>
</listener>
<!-- <servlet-class>tool.CityActionServlet</servlet-class> 使其能与ajax结合 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>tool.CityActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 利用spring监听 编码设置-->
<filter>
<filter-name>Spring character encoding filter</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>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2.
<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>
/WEB-INF/applicationContext-*.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 配置得到上下文路径的listener -->
<listener>
<listener-class>
tool.ContextPathListener
</listener-class>
</listener>
<!-- <servlet-class>tool.CityActionServlet</servlet-class> 使其能与ajax结合 -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>tool.CityActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 利用spring监听 编码设置-->
<filter>
<filter-name>Spring character encoding filter</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>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://192.168.0.3:3309/game"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<!-- 指定缓存产品提供商 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- 查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/meitian/domain/hbm/Area.hbm.xml</value>
<value>com/meitian/domain/hbm/Map.hbm.xml</value>
<value>com/meitian/domain/hbm/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 对底层数据库操作的的的dao -->
<bean id="cityDao" class="com.meitian.dao.CityImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
3.
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://192.168.0.3:3309/game"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 开启二级缓存 -->
<prop key="hibernate.cache.use_second_level_cache">false</prop>
<!-- 指定缓存产品提供商 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- 查询缓存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>com/meitian/domain/hbm/Area.hbm.xml</value>
<value>com/meitian/domain/hbm/Map.hbm.xml</value>
<value>com/meitian/domain/hbm/User.hbm.xml</value>
</list>
</property>
</bean>
<!-- 对底层数据库操作的的的dao -->
<bean id="cityDao" class="com.meitian.dao.CityImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
</beans>
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
package tool;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionServlet;
/**
* 继承struts中的actionServlet,重写service方法,使其与ajax结合
* @author Administrator
*/
public class CityActionServlet extends ActionServlet {
private static final long serialVersionUID = 4386722652784007932L;
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.process(request, response);
//从request中得到要返回给浏览器的值
Object result = request.getAttribute("ajaxResult");
if (result != null) {
response.setCharacterEncoding("utf-8");
response.getWriter().print(result);
}
}
}
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionServlet;
/**
* 继承struts中的actionServlet,重写service方法,使其与ajax结合
* @author Administrator
*/
public class CityActionServlet extends ActionServlet {
private static final long serialVersionUID = 4386722652784007932L;
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.process(request, response);
//从request中得到要返回给浏览器的值
Object result = request.getAttribute("ajaxResult");
if (result != null) {
response.setCharacterEncoding("utf-8");
response.getWriter().print(result);
}
}
}