Struts2 + 스프링 + 아이바티스 기본 설정을 가장 잘 설명해놓은 페이지
1. lib /WEB-INF/lib/ 에 아래 jar 추가하기
asm-1.5.3.jar | mybatis에서사용 |
cglib-2.1_3.jar | |
commons-collections.jar | 아파치커먼스 |
commons-dbcp-1.4.jar | |
commons-fileupload-1.2.1.jar | |
commons-io-1.3.2.jar | |
commons-lang.jar | |
commons-logging-1.1.1.jar | |
commons-pool.jar | |
freemarker-2.3.15.jar | |
log4j-1.2.15.jar | 로깅 |
mybatis-3.0.1.jar | mybatis3 |
ognl-2.7.3.jar | struts2에서사용 |
slf4j-api-1.5.8.jar | 로깅 |
slf4j-log4j12-1.5.8.jar | 로깅 |
spring-aop.jar | 스프링관련 |
spring-beans.jar | |
spring-core.jar | |
spring-jdbc.jar | |
spring-orm.jar | |
spring-tx.jar | |
spring-web.jar | |
spring-webmvc-struts.jar | |
spring-webmvc.jar | |
spring.jar | |
sqljdbc4.jar | 디비컨넥션 |
struts2-core-2.1.8.1.jar | 스트러츠2 |
struts2-spring-plugin-2.1.8.1.jar | 스트러츠2와 스프링연계하기 |
xwork-core-2.1.6.jar | 스트러츠2 |
2. web.xml
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
스프링 관련
<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>
3. applicationContext.xml (/WEB-INF/applicationContext.xml)
mybatis3는 아직 스프링2.x에서는 지원하지 않는다. (스프링 3에서는 지원 근데 나는 스프링2.5를 쓴다는...)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="test" class="com.arario.mixi.anipang.TestAction" />
</beans>
4. struts.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.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.devMode" value="false" />
<constant name="struts.objectFactory" value="spring" /> <!-- 요부분이 스프링 연동 -->
<constant name="struts.action.extension" value="htmls" />
<include file="example.xml"/>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<action name="index">
<result type="redirectAction">
<param name="actionName">HelloWorld</param>
<param name="namespace">/example</param>
</result>
</action>
<action name="test" class="test">
<result name="success">/WEB-INF/pages/test.jsp</result>
</action>
</package>
<!-- Add packages here -->
</struts>
- TestAction.java : 컨트롤러 역활을 하는 클래스
private static final SqlSessionFactory sqlSessionFactory;
static {
try{
String resource = "SqlSessionConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public SqlSessionFactoryManager(){}
public static SqlSessionFactory getSqlMapper(){
return sqlSessionFactory;
}
}
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"hjdbc:sqlserver://127.0.0.1;DatabaseName=dbttp://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="mssqlserver-con.properties" />
<typeAliases>
<typeAlias alias="testVO" type="test.TestVO" />
</typeAliases>
<environments default="beta">
<environment id="beta">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="jdbc:sqlserver://127.0.0.1;DatabaseName=db" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
<environment id="real">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="jdbc:sqlserver://127.0.0.1;DatabaseName=db" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper.xml" />
</mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="Mixi">
<select id="test" parameterType="String" resultType="testVO">
select top 3 name charName, userId userId
from dbo.userDB
where MEMBERID = #{user_id}
</select>
</mapper>
private final Logger log = LoggerFactory.getLogger(this.getClass());
private List<TestVO> chars;
public String execute() {
log.debug(this.getClass().getName() + " StarT!");
TestDAO testDAO = new TestDAO();
chars = testDAO.getChars("wapj2000");
for(int i=0;i<3;i++){
System.out.println(chars.get(i));
}
return SUCCESS;
}
public List<TestVO> getChars() {
return chars;
}
public void setChars(List<TestVO> chars) {
this.chars = chars;
}
}
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryManager.getSqlMapper();
public List<TestVO> getChars(String user_id){
SqlSession session = sqlSessionFactory.openSession();
try{
List<TestVO> charNames = session.selectList("Mixi.test", user_id);
return charNames;
} finally{
session.close();
}
}
}
private String usn;
private String userId;
private String charName;
public String getUsn() {
return usn;
}
public void setUsn(String usn) {
this.usn = usn;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getCharName() {
return charName;
}
public void setCharName(String charName) {
this.charName = charName;
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>TEST!!</title>
</head>
<body>
<h2>TTTTEST</h2>
<s:iterator value="chars">
<div>
<s:property value="userId" /> || <s:property value="charName" />
</div>
</s:iterator>
</body>
</html>
7. log4j.properties xml 파일로 하는 방법도 있던데 이게 젤 간편한듯.
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p ({%t} %F[%M]:%L) [%d] - %m%n
log4j.appender.dailyfile.Threshold = INFO
log4j.appender.dailyfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern=%5p ({%t} %F[%M]:%L) [%d] - %m%n
log4j.logger.com.opensymphony=WARN
log4j.logger.org.apache=WARN
log4j.logger.org.springframework=WARN
log4j.logger.com.ibatis=WARN
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=INFO
log4j.logger.java.sql.PreparedStatement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
아래와 같은 화면이 나오면 성공~~
'개발관련 > 웹개발' 카테고리의 다른 글
windows 2003 64bit에 톰캣(tomcat)설치하기 (0) | 2010.09.15 |
---|---|
윈도우즈(windows)에서 허드슨 설치 (0) | 2010.09.09 |
Java 오픈 소스 라이브러리/어플리케이션 목록 정리 (0) | 2009.07.30 |
[자바] 정규표현식 예제1 (1) | 2009.07.10 |
[자바] NumberFormat 클래스를 이용한 숫자를 금액으로 표시하기 (0) | 2009.06.02 |