struts2学习之一: http://arthur2014.iteye.com/blog/2162974
struts2学习之二-json插件: http://arthur2014.iteye.com/blog/2162989
struts2学习之四-struts2注解: http://arthur2014.iteye.com/admin/blogs/2163348
在build.gradle需要添加"org.apache.struts:struts2-spring-plugin:2.3.15.1",引入一系列spring相关jar包。
1、在类路径下新建applicationContext.xml,在dao、service层使用spring注解实现,则applicationContext.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <!-- <context:annotation-config /> --> <context:component-scan base-package="com.haochen.*" /> </beans>
<context:component-scan/>表示使用注解来注册bean定义,属性 base-package表示扫描com.haochen包下的带注解标签的类。在spring中一般使用@Repository注解dao层,使用@Service注解业务层
2、修改web.xml添加一下内容:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext.xml </param-value> </context-param>
3、定义dao,dao接口IUserDao.java:
package com.haochen.dao; import com.haochen.vo.User; public interface IUserDao { public User getUser(String name); }
实现接口的具体dao类UserDaoImpl.java,使用@Repository注解注册:
package com.haochen.dao; import org.springframework.stereotype.Repository; import com.haochen.vo.User; /** * * @title * @Description dao类,使用spring注解@Repository注册 * @author chenhao * @date 2014年12月4日 上午9:11:28 * @version 1.0 */ @Repository("userDao") public class UserDaoImpl implements IUserDao { @Override public User getUser(String name) { User user = new User(); user.setName("tom"); user.setAge(18); user.setSex('M'); return user; } }
4、定义service,接口IUserService.java:
package com.haochen.service; import com.haochen.vo.User; public interface IUserService { public User getUser(String name); }
实现类UserService.java,使用@Service注解注册定义,使用@Autowired注解注入dao:
package com.haochen.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import com.haochen.dao.IUserDao; import com.haochen.vo.User; /** * * @title * @Description 业务类,使用spring注解@Service注册 * @author chenhao * @date 2014年12月4日 上午9:12:39 * @version 1.0 */ @Service("userService") public class UserService implements IUserService { /** * 通过spring注解获取dao实例 */ @Qualifier("userDao") @Autowired private IUserDao userDao; @Override public User getUser(String name) { return this.userDao.getUser(name); } }
5、在控制层action中使用@Autowired注解注入service:
package com.haochen.action; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import com.haochen.service.IUserService; public class UserJsonAction extends BaseJsonAction { /** * */ private static final long serialVersionUID = -5818584581746655517L; private String name; // 指定bean名 @Qualifier("userService") @Autowired private IUserService userService; @Override public String execute() throws Exception { String n = name; this.success(userService.getUser(n)); /** * 1、返回值为JSON,配置文件struts.xml中的<action></action>不用设置<result * type="json"></result>。 * 2、返回值为SUCCESS,配置文件struts.xml中的<action></action>需要设置<result * type="json"></result>。 **/ return JSON; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
6、启动tomcat,访问http://localhost:8080/struts2demo/a/helloworld.action,即可获取user值。