관리 메뉴

nkdk의 세상

1장 스트럭쳐 예제 1번 본문

My Programing/Framework

1장 스트럭쳐 예제 1번

nkdk 2008. 3. 9. 22:05
index.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<html><body>
<a href="index.do">로그온</a>
</body></html>

IndexAction.java

package example;
import javax.servlet.http.*;
import org.apache.struts.action.*;

public class IndexAction extends Action{
// execute 메서드를 만든다.(오버 라이딩 됨)
public ActionForward execute(
ActionMapping mapping, // 맵핑을 상속 받는다.
ActionForm form, // 폼을 상속 받는다.
HttpServletRequest request,
HttpServletResponse response) throws Exception{

request.setAttribute("title", "스트럿츠 만세");
return mapping.findForward("success");
}
}


// MyrequestProcess.java

package example;
import java.io.*;

import javax.servlet.http.*;
import org.apache.struts.action.*;

public class MyrequestProcess extends RequestProcessor{
protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("EUC-KR");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 이곳에서 전체적 영향을 받는 콘트롤러를 만든다.
System.out.println("Action 실행 보다 먼저 수행");
return true;
}
}


--------------------
struts-config.xml

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_3.dtd">
<struts-config>
<action-mappings>
<action path="/index" type="example.IndexAction" >
<forward name="success" path="/success.jsp" />
</action>
</action-mappings>
<controller>
<set-property property="processorClass" value="example.MyrequestProcess" />
</controller>
</struts-config>

-------------------
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>str</display-name>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

-------
success.jsp

<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<html><body>
결과는
<%=request.getAttribute("title") %>
<br>
${requestScope.title}
</body></html>