struts 2 학습 의 3-spring 주해
                                            
 5764 단어  봄 주해
                    
struts 2 학습 중 하나:
http://arthur2014.iteye.com/blog/2162974
struts 2 학습 의 2-json 플러그 인:
http://arthur2014.iteye.com/blog/2162989
struts 2 학습 의 4-struts 2 주해:
http://arthur2014.iteye.com/admin/blogs/2163348
build.gradle 에"org.apache.struts:struts 2-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>
  2.웹.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.서비스 정의,인터페이스 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.actionuser 값 을 가 져 올 수 있 습 니 다.