S2SH使用Action封装json
因为今天刚学习了在Struts2-Action类中完成json格式数据封装,所以简单记录一下。
1. 把json相关的jar放到项目的lib下面
2. 编写Action类 命名为JsonAction
2.1第一种实现方式
public class JsonAction {
private UserService userService = null;
private List<User> userList = null;
public String execute(){
userList = userService.getAllUsers();
return "success";
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
}
2.2第二种实现方式:
public class JsonAction {
private UserService userService = null;
public String execute(){
List<User> userList = userService.getAllUsers();
ActionContext.getContext().put("userList", userList);
return "success";
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
}
2.3第三种实现方式
3. 编写structs.xml配置文件
<struts>
<package name="cn.edu.hezeu.jsj.json" extends="struts-default,json-default">
<action name ="jsonAction" class="jsonAction" method="execute">
<result name = "success" type="json">
<param name="root">userList</param>
</result>
</action>
</package>
</struts>
4. 编写spring.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:p="http://www.springframework.org/schema/p"
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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 数据源的配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/s2sh"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="initialSize" value="2"/>
<property name="maxActive" value="10"/>
<property name="maxIdle" value="3"/>
<property name="maxWait" value="1000"/>
</bean>
<!--定义Hibernate的sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 依赖注入已配置好的数据源 -->
<property name="dataSource" ref="dataSource"/>
<!- 指定Hibernate的映射文件 -->
<property name="mappingResources" >
<list>
<value>cn/edu/hezeu/jsj/json/action/s2sh.hbm.xml</value>
</list>
</property>
<!-- 设置Hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
</bean>
<!-- 事务管理器的配置 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="userDao" class="cn.edu.hezeu.jsj.ch09.spring.hibernate.struts.UserDaoImpl">
<property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
<bean id="userService" class="cn.edu.hezeu.jsj.ch09.spring.hibernate.struts.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<!-- json -->
<bean id="jsonAction" class="cn.edu.hezeu.jsj.json.action.JsonAction">
<property name="userService" ref="userService"></property>
</bean>
</beans>
5. 编写hibernate.hbm.xml
这里是根据数据库的表和User类来配置的 ,配置文件命名一般是类名.hbm.xml,User类在这里省略,
<hibernate-mapping package="cn.edu.hezeu.jsj.ch09.spring.hibernate.struts">
<class name="User" table="tab_user">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username" column="username" type="string" not-null="true"/>
<property name="password" column="password" type="string" not-null="true"/>
<property name="realName" column="realName" type="string" not-null="true"/>
<property name="age" column="age" type="int" not-null="false"/>
<property name="regDate" column="regDate" type="date" not-null="false"/>
</class>
</hibernate-mapping>