스프링 라이브러리 다운 받기

http://www.springsource.org/download






action/UserRegAction4.java
package action;

import model.User;
import dao.UserDao;
import com.opensymphony.xwork2.ActionSupport;

public class UserRegAction4 extends ActionSupport {
	private User user=new User();	
	private UserDao userDao;
	
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}		
	
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	@Override
	public String execute() throws Exception {
		//userDao=new UserDao();
		// 객체 생성을 spring 이 해주었다. 
		userDao.create(user);
		return SUCCESS;
	}
}


dao/UserDao.java
package dao;

import model.User;

public class UserDao {
	public void create(User user){
		System.out.println("사용자를 추가했습니다.");
		System.out.println("추가된 사용자의 정보");
		System.out.println(user);
	}
}

model/User.java
package model;

public class User {
	private String userId;  
	private String userPW;
	private String userName;
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserPW() {
		return userPW;
	}
	public void setUserPW(String userPW) {
		this.userPW = userPW;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	@Override
	public String toString() {
		String result;
		result=String.format("[User:ID=%s,Password=%s,Name=%s]", 
				userId, userPW, userName);
		return result;
	}
}

struts.properties
struts.i18n.reload=true
struts.devMode=true
struts.configuration.xml.reload=true
struts.objectFactory=org.apache.struts2.spring.StrutsSpringObjectFactory
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.i18n.encoding" value="UTF-8" />
  <constant name="struts.devMode" value="true" />
  
  <package name="ch12"  extends="struts-default">

    <action name="userRegForm">
      <result>/jsp/userRegForm4.jsp</result>
    </action>  
    
    <!--  spring 적용된 userRegAction4  -->
    <action name="userRegAction" class="userRegAction4"> 
      <result name="input">/jsp/userRegForm4.jsp</result>  
      <result name="success">/jsp/userRegSuccess4.jsp</result>
    </action>
   </package>    
</struts>

jsp/useRegForm4.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>회원 가입</title>
</head>
<body>
<center>
<h2>회원 가입</h2><p>
<s:form action="userRegAction"  validate="true">
    <s:textfield label="아이디" name="user.userId" value="%{user.userId}" />  
    보는는 쪽 : 객체.setUser().setUserId()  /  받는쪽 : 객체.getUser().getUserId();  
    <s:password label="비밀번호" name="user.userPW"  value="%{user.userPW}" />
    <s:textfield label="이름" name="user.userName"  value="%{user.userName}" />
    <s:submit/>
</s:form>
</center>
</body>
</html>
jsp/useRegSuccess4.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>회원 가입 완료</title>
</head>
<center>
<body>
<b><font color="red">회원 가입이 완료되었다.</font></b><p>
 아 이 디 : <s:property value="user.userId"/>  <p>
 비밀번호 : <s:property value="user.userPW"/>  <p>
 이 름  : <s:property value="user.userName"/> <p>
</body>
</center>
</html>
appliactionContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans default-autowire="byName">
<bean id="userDao" class="dao.UserDao" />
<!--  userDao = new UserDao(); 객체를 생성해 둡니다. 
	   db와 연동시켜주는 객체는 싱글톤을 사용하거나 
	   userDao.getInstance(); - 싱글톤 객체 얻는 명령.. 
	   userDao.insert  userDao.selectAll.. 등등
	   
 -->
<bean id="userRegAction4" class="action.UserRegAction4" />
<!--  action.UserRegAction4 클래스를 userRegAction4로
	   불러 사용할 수 있습니다. 
	   + action.UserRegAction4 클래스의 객체를 생성해둡니다.
	   new UserRegAction4(); 
  -->

</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Chapter14</display-name>
    <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>
    
  <filter>
  <filter-name>struts2</filter-name>
  <filter-class>
  org.apache.struts2.dispatcher.FilterDispatcher
  </filter-class>
   <init-param> 
     <param-name>struts.i18n.encoding</param-name>
     <param-value>UTF-8</param-value>
  </init-param> 
  </filter>
  <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>



+ Recent posts