说明:
1.用了maven去搞这个demo,懒得去导jar包。。。
2.这个demo用了spring去做Ioc,事务的aop;用了struts2去做“MVC”(没有用到任何UI技术,有点对不起这个MVC,就单单一个后台。。。);用了hibernate和Mybatis两个持久层,用上MyBatis是用来应对复杂的SQL查询逻辑,结果集映射也灵活。
3.此demo目的只是一个笔记,搞java web 开发的都懂的,要学的太多,很多东西过段时间不接触就忘了,也用作一个测试环境。同时也是给自己的一个小礼物以自勉之,下周鄙人就要跳去搞外包了。。。只因实力不济啊,吾为索道不辞路远,希望未来有所长进。。。
4.很遗憾,Junit test 用不了。。。
demo的整合大致示意图:
demo的目录结构:
给出demo用到的依赖包,pom.xml:
Xml代码
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.oham.ssh</groupId>
- <artifactId>ssh</artifactId>
- <packaging>war</packaging>
- <version>0.0.1-SNAPSHOT</version>
- <name>ssh Maven Webapp</name>
- <url>http://maven.apache.org</url>
- <!-- 设置一些全局常量 -->
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <spring.version>3.1.0.RELEASE</spring.version>
- <struts2.version>2.3.15.1</struts2.version>
- <hibernate.version>4.1.4.Final</hibernate.version>
- <mybatis.version>3.1.1</mybatis.version>
- </properties>
- <!-- 设置maven build的时候环境 -->
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>2.3.2</version>
- <configuration>
- <source>1.6</source>
- <target>1.6</target>
- </configuration>
- </plugin>
- </plugins>
- </build>
- <!-- 项目用到的jar包依赖 -->
- <dependencies>
- <!-- spring start -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>3.1.1.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>3.1.1.RELEASE</version>
- </dependency>
- <!-- spring end -->
- <!-- struts2 start -->
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-core</artifactId>
- <version>${struts2.version}</version>
- </dependency>
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-spring-plugin</artifactId>
- <version>${struts2.version}</version>
- </dependency>
- <!-- struts2中对于注解的支持 -->
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-convention-plugin</artifactId>
- <version>${struts2.version}</version>
- </dependency>
- <!-- 用于查看struts2中注册的action, 可不用 -->
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-config-browser-plugin</artifactId>
- <version>${struts2.version}</version>
- </dependency>
- <!-- 用于struts2的JSON支持, 可不用 -->
- <dependency>
- <groupId>org.apache.struts</groupId>
- <artifactId>struts2-json-plugin</artifactId>
- <version>${struts2.version}</version>
- </dependency>
- <!-- struts2 end -->
- <!-- c3p0数据源 start -->
- <dependency>
- <groupId>com.mchange.c3p0</groupId>
- <artifactId>com.springsource.com.mchange.v2.c3p0</artifactId>
- <version>0.9.1.2</version>
- </dependency>
- <!-- c3p0数据源 end -->
- <!-- MySQL 驱动 start -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.15</version>
- </dependency>
- <!-- MySQL 驱动 end -->
- <!-- Hibernate start -->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-core</artifactId>
- <version>${hibernate.version}</version>
- </dependency>
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-ehcache</artifactId>
- <version>${hibernate.version}</version>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.6.1</version>
- </dependency>
- <!-- Hibernate end -->
- <!-- MyBatis start-->
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis</artifactId>
- <version>${mybatis.version}</version>
- </dependency>
- <dependency>
- <groupId>org.mybatis</groupId>
- <artifactId>mybatis-spring</artifactId>
- <version>1.1.1</version>
- </dependency>
- <!-- End of MyBatis -->
- <!-- Junit test start -->
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
- <!-- Junit test end -->
- <!-- J2EE start -->
- <dependency>
- <groupId>org.apache.openejb</groupId>
- <artifactId>javaee-api</artifactId>
- <version>5.0-1</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.faces</groupId>
- <artifactId>jsf-api</artifactId>
- <version>1.2_04</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>javax.servlet.jsp</groupId>
- <artifactId>jsp-api</artifactId>
- <version>2.1</version>
- <scope>provided</scope>
- </dependency>
- <!-- J2EE end -->
- </dependencies>
- </project>
demo是web项目,web.xml:
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:spring/applicationContext.xml,classpath:spring/**/applicationContext*.xml</param-value>
- </context-param>
- <!-- 设置启用log4j -->
- <listener>
- <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
- </listener>
- <!-- 设置struts的filter,给了个param,读取指定的struts配置文件 -->
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>platform-web.xml,struts-default.xml,struts-plugin.xml,struts/struts.xml</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <!-- 设置spring的listener -->
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- </web-app>
先看spring的配置,有两个,
applicationContext.xml:
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:util="http://www.springframework.org/schema/util"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-3.0.xsd">
- <!-- 读入配置文件 -->
- <context:property-placeholder location="classpath:config/*.properties" />
- <!-- 设置配置文件引用变量 -->
- <util:properties id="dbProp" location="classpath:config/db.properties" />
- <!-- 启动 spring 的注解功能 -->
- <context:annotation-config />
- <!-- register annotated class with @Service, @Repository as Spring beans -->
- <!-- 指定 spring 注解的扫描路径 -->
- <context:component-scan base-package="org.oham.learn.ssh.controllers;org.oham.learn.ssh.services" />
- </beans>
applicationContext-persistence.xml:
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: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/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
- >
- <bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass" value="#{dbProp.driverClass_MySql}" />
- <property name="jdbcUrl" value="#{dbProp.jdbcUrl_MySql}" />
- <property name="user" value="#{dbProp.username_MySql}" />
- <property name="password" value="#{dbProp.password_MySql}" />
- <property name="initialPoolSize" value="#{dbProp.initialPoolSize_MySql}" />
- <property name="maxPoolSize" value="#{dbProp.maxPoolSize_MySql}" />
- <property name="minPoolSize" value="#{dbProp.minPoolSize_MySql}" />
- <property name="acquireIncrement" value="#{dbProp.acquireIncrement_MySql}" />
- <property name="autoCommitOnClose" value="#{dbProp.autoCommitOnClose_MySql}" />
- </bean>
- <!-- Hibernate 的 session factory 配置 -->
- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
- <property name="dataSource" ref="c3p0DataSource"/>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">#{dbProp.hibernate_dialect}</prop>
- <prop key="hibernate.show_sql">#{dbProp.hibernate_showSql}</prop>
- <prop key="hibernate.format_sql">#{dbProp.hibernate_formatSql}</prop>
- <prop key="hibernate.use_sql_comments">#{dbProp.hibernate_useSqlComments}</prop>
- <prop key="hibernate.hbm2ddl.auto">#{dbProp.hibernate_hbm2ddlAuto}</prop>
- <!-- 配置hibernate的缓存机制的,但demo里没有具体的体现 -->
- <prop key="hibernate.cache.provider_class">#{dbProp.hibernate_cacheProviderClass}</prop>
- <prop key="hibernate.cache.use_query_cache">#{dbProp.hibernate_cacheUseQueryCache}</prop>
- <prop key="hibernate.cache.use_second_level_cache">#{dbProp.hibernate_cacheUseSecondLevelCache}</prop>
- </props>
- </property>
- <!-- demo用了hibernate的两种映射配置方式:xml与annotation -->
- <property name="packagesToScan" value="org.oham.learn.ssh.beans" />
- <property name="mappingResources">
- <list>
- <value>org/oham/learn/ssh/beans/TPerson.hbm.xml</value>
- </list>
- </property>
- </bean>
- <!-- Mybatis 的 session factory 配置 -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="c3p0DataSource" />
- <property name="configLocation" value="classpath:mybatis/sqlmap-config.xml" />
- </bean>
- <!-- 扫描 mapper 接口 -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="org.oham.learn.ssh.sqlmapper" />
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
- </bean>
- <!-- 这个是配置AOP形式的transaction管理,有兴趣的可以放开注释自己玩玩 -->
- <!--
- handle default transaction management for service - start
- <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="update*" read-only="false" propagation="REQUIRED"/>
- <tx:method name="save*" read-only="false" propagation="REQUIRED"/>
- <tx:method name="insert*" read-only="false" propagation="REQUIRED"/>
- <tx:method name="delete*" read-only="false" propagation="REQUIRED"/>
- <tx:method name="handleLink*" read-only="false" propagation="REQUIRED"/>
- <tx:method name="*" read-only="true" propagation="NOT_SUPPORTED"/>
- </tx:attributes>
- </tx:advice>
- <aop:config>
- <aop:pointcut id="txOperationForService"
- expression="execution(* com.hkt.engineering.iris.ws.service.impl..*.*(..))" />
- <aop:advisor advice-ref="transactionAdvice" pointcut-ref="txOperationForService" />
- </aop:config>
- -->
- <!-- handle default transaction management for service - end -->
- <!-- Transaction Manager -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <!-- 用于Mybatis 的事务配置 -->
- <property name="dataSource" ref="c3p0DataSource"/>
- <!-- 用于Hibernate 的事务配置 -->
- <property name="sessionFactory" ref="sessionFactory"/>
- </bean>
- <!-- 注解形式的transaction管理 -->
- <!-- Transaction Manager Annotation set active-->
- <tx:annotation-driven transaction-manager="transactionManager"/>
- </beans>
struts.xml:
Xml代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <!-- 开启使用开发模式,详细错误提示 -->
- <constant name="struts.devMode" value="true" />
- <!-- 指定每次配置文件更改后,自动重新加载 -->
- <constant name="struts.configuration.xml.reload" value="true" />
- <!-- 将对象交给spring管理 -->
- <!-- <constant name="struts.objectFactory" value="spring" />-->
- <!-- 指定资源编码类型 -->
- <constant name="struts.i18n.encoding" value="UTF-8" />
- <!-- 指定struts-convention插件扫描的本demo里action所在的路径,若没有被扫描,注解了的action类是不会被注册为action的,此处我尝试使用
- struts的注解形式的action配置
- -->
- <constant name="struts.convention.action.packages" value="org.oham.learn.ssh.controllers" />
- <!-- 配置注解action默认的result视图文件路径 -->
- <!-- <constant name="struts.convention.result.path" value="/WEB-INF/jsp" />-->
- <!-- 默认的package,此处只搞了个json的 -->
- <package name="ssh-default" namespace="/" extends="struts-default">
- <result-types>
- <result-type name="json" class="org.apache.struts2.json.JSONResult"/>
- </result-types>
- <interceptors>
- <interceptor name="json" class="org.apache.struts2.json.JSONInterceptor"/>
- </interceptors>
- </package>
- <!-- 传统的以xml的形式注册了action,注意action的class,是spring bean的id -->
- <package name="testStruts" namespace="/struts" extends="ssh-default">
- <action name="testCall" method="testCall" class="testStrutsAction">
- <result name="success">/WEB-INF/jsp/Main.jsp</result>
- <result name="json" type="json"/>
- </action>
- <action name="testAnnotationCall" method="testAnnotationCall" class="testStrutsAction">
- <result name="success">/WEB-INF/jsp/Main.jsp</result>
- </action>
- </package>
- </struts>
基本配置写完了,现在挑选一个action试试,TestStrutsAction.java:
Java代码
- package org.oham.learn.ssh.controllers;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.annotation.Resource;
- import org.oham.learn.ssh.beans.TGroup;
- import org.oham.learn.ssh.beans.TPerson;
- import org.oham.learn.ssh.beans.TUser;
- import org.oham.learn.ssh.services.TestHibernateService;
- import org.oham.learn.ssh.sqlmapper.TUserMapper;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Service;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * 此处的action由spring去负责实例化,默认是单例的,
- * 为迎合struts的action,现设置器scope为protoype
- */
- @Service(value="testStrutsAction")
- @Scope("prototype")
- public class TestStrutsAction extends ActionSupport {
- private static final long serialVersionUID = 3362122496775495186L;
- @Resource(name="TestHibernate")
- private TestHibernateService testHibernateService;
- @Resource
- private TUserMapper tUserMapper;
- private Map<String, Object> jsonMap;
- /**
- * 该action为xml方式配置的,此处测试其方法的调用,
- * 用Hibernate执行了查询,此处的映射配置基于xml
- */
- public String testCall() {
- List<TPerson> list = testHibernateService.getTpersonList();
- for(TPerson p : list) {
- System.out.println(p.getName());
- }
- return "success";
- }
- /**
- * 用Hibernate执行了查询,此处的映射配置基于annotation
- */
- public String testAnnotationCall() {
- List<TGroup> list = testHibernateService.getTGroupList();
- for(TGroup g : list) {
- System.out.println(g.getName());
- }
- return "success";
- }
- /**
- * 测试json interceptor是否起作用
- */
- public String testJsonCall() {
- jsonMap = new HashMap<String, Object>();
- jsonMap.put("a", "aa");
- jsonMap.put("b", "b");
- return "json";
- }
- /**
- * 测试MyBatis 整合
- */
- public String testMybatisCall() {
- TUser user = new TUser();
- user.setId(1);
- user.setName("oham");
- List<TUser> list = tUserMapper.selectTUserByExample(user);
- for(TUser t : list) {
- System.out.println(t.getName());
- }
- return SUCCESS;
- }
- public Map<String, Object> getJsonMap() {
- return jsonMap;
- }
- }
看其testCall方法, 用到一个注入的testHibernateService,进入看看,TestHibernateServiceImpl.java:
Java代码
- package org.oham.learn.ssh.services.impl;
- import java.util.List;
- import javax.annotation.Resource;
- import org.hibernate.Criteria;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.oham.learn.ssh.beans.TGroup;
- import org.oham.learn.ssh.beans.TPerson;
- import org.oham.learn.ssh.services.TestHibernateService;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- @Service(value="TestHibernate")
- public class TestHibernateServiceImpl implements TestHibernateService {
- @Resource(name="sessionFactory")
- private SessionFactory factory;
- @Transactional(readOnly=true, propagation=Propagation.REQUIRED)
- public List<TPerson> getTpersonList() {
- Session session = factory.getCurrentSession();
- Criteria criteria = session.createCriteria(TPerson.class);
- List<TPerson> list = criteria.list();
- return list;
- }
- @Transactional(readOnly=true, propagation=Propagation.REQUIRED)
- public List<TGroup> getTGroupList() {
- Session session = factory.getCurrentSession();
- Criteria criteria = session.createCriteria(TGroup.class);
- List<TGroup> list = criteria.list();
- return list;
- }
- }
factory.getCurrentSession();这句,若当前没有事务,抛session closed异常的,可以注释@Transactional试试,方法调用结束,随着事务关闭,session也关闭。可以自己写个方法测试,形如:
Java代码
- @Transactional(readOnly=true, propagation=Propagation.REQUIRED)
- public Session testSession() {
- Session session = factory.getCurrentSession();
- return session;
- }
然后action里如此调用:
Java代码
- Session session = testHibernateService.testSession();
- Criteria criteria = session.createCriteria(TPerson.class);
- List<TPerson> list = criteria.list();
给出hibernate的映射相关,
TPerson.java:
Java代码
- package org.oham.learn.ssh.beans;
- import java.io.Serializable;
- public class TPerson implements Serializable{
- private static final long serialVersionUID = 4135349486988951197L;
- private int id;
- private String name;
- private String address;
- private String tel;
- private String zipCode;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public String getTel() {
- return tel;
- }
- public void setTel(String tel) {
- this.tel = tel;
- }
- public String getZipCode() {
- return zipCode;
- }
- public void setZipCode(String zipCode) {
- this.zipCode = zipCode;
- }
- }
TPerson.hbm.xml:
Xml代码
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="org.oham.learn.ssh.beans">
- <class name="TPerson" table="t_person">
- <id name="id" column="id" type="java.lang.Integer">
- <generator class="native"/>
- </id>
- <property name="name" column="name" type="java.lang.String"/>
- <property name="address" column="address" type="java.lang.String"/>
- <property name="tel" column="tel" type="java.lang.String"/>
- <property name="zipCode" column="zipcode" type="java.lang.String"/>
- </class>
- </hibernate-mapping>
TGroup.java,这个是基于hibernate的注解来做映射的:
Java代码
- package org.oham.learn.ssh.beans;
- import java.io.Serializable;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name="t_group")
- public class TGroup implements Serializable {
- private static final long serialVersionUID = 2177231731375781687L;
- @Id
- @Column(name="id")
- private int id;
- @Column(name="name", nullable= true)
- private String name;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- }
然后试着调用:http://你的IP加端口/ssh/struts/testCall.action,看看可否。也可调用本action的其他方法,诸如:http://你的IP加端口/ssh/struts/testCall!testJsonCall.action,再看看。
再给出基于struts注解形式注册的另一个action,TestStrutsAnnotationAction.java:
Java代码
- package org.oham.learn.ssh.controllers;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import javax.annotation.Resource;
- import org.apache.struts2.convention.annotation.Action;
- import org.apache.struts2.convention.annotation.Namespace;
- import org.apache.struts2.convention.annotation.ParentPackage;
- import org.apache.struts2.convention.annotation.Result;
- import org.oham.learn.ssh.beans.TGroup;
- import org.oham.learn.ssh.beans.TPerson;
- import org.oham.learn.ssh.beans.TUser;
- import org.oham.learn.ssh.services.TestHibernateService;
- import org.oham.learn.ssh.sqlmapper.TUserMapper;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Component;
- import com.opensymphony.xwork2.ActionSupport;
- @ParentPackage("ssh-default")
- @Namespace("/sa")
- @Scope(value="prototype")
- @Component("saAction")
- public class TestStrutsAnnotationAction extends ActionSupport{
- private static final long serialVersionUID = -2793512785203051741L;
- private Map<String, Object> jsonMap;
- @Resource(name="TestHibernate")
- private TestHibernateService testHibernateService;
- @Resource
- private TUserMapper tUserMapper;
- @Action(value="testSACall",
- results={
- @Result(name="success", location="/WEB-INF/jsp/Main.jsp")
- })
- public String testAnnotaionCall() {
- List<TPerson> list = testHibernateService.getTpersonList();
- for(TPerson p : list) {
- System.out.println(p.getName());
- }
- List<TGroup> list2 = testHibernateService.getTGroupList();
- for(TGroup g : list2) {
- System.out.println(g.getName());
- }
- return "success";
- }
- @Action(value="testSAMyBatis",
- results={
- @Result(name="success", location="/WEB-INF/jsp/Main.jsp")
- })
- public String testMyBatisCall() {
- List<TUser> list = tUserMapper.selectTUserByExample(null);
- for(TUser t : list) {
- System.out.println(t.getName());
- }
- return SUCCESS;
- }
- @Action(value="testJ",
- results={@Result(name="json", type="json")})
- public String testJ() {
- jsonMap = new HashMap<String, Object>();
- jsonMap.put("a", "aa");
- jsonMap.put("b", "b");
- return "json";
- }
- public Map<String, Object> getJsonMap() {
- return jsonMap;
- }
- }
调用:诸如,http://你的IP加端口/ssh/sa/testSACall.action试试可否。
本demo用到了struts2-config-browser-plugin这个插件,主要用来看看注册的action有哪些,因为鄙人为了搞基于注解的struts action搞半天没成,起初不知原因为何,后来struts.xml中加上:<constant name="struts.convention.action.packages" value="org.oham.learn.ssh.controllers" />就行了,但看别人的一些demo他们没加,所以至今也不太敢确定是否就是这个原因了。
调用http://你的IP加端口/ssh/config-browser/actionNames.action就可以看到成功注册的action。
完了,希望对诸君有所助。
附上项目源码ssh.zip,解压——>导入Existing Maven Project。
- ssh.zip (62.8 KB)
- 下载次数: 176