Struts 의 페이지 제어 및 설정

파일
1. 페이지 입력
input1. jsp 페이지

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html> 
	<head>
		<title>JSP for HelloWorldForm form</title>
	</head>
	<body>
		<html:form action="/helloWorld2">
			msg : <html:text property="msg"/><html:errors property="msg"/><br/>
			<html:submit/><html:cancel/>
		</html:form>
	</body>
</html>

threeInput. jsp 페이지

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'input3.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="<%=basePath%>threeInput.do" method="post">&nbsp; 
       :<input type="text" name="msg" value="" />
    <input type="submit" name="method" value="insert" />
    <input type="submit" name="method" value="update" />
    <input type="submit" name="method" value="delete" />
    </form>
  </body>
</html>

2. 출력 페이지
show. jsp 페이지

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'show.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  <%
  String str = (String)request.getAttribute("helloWorld");
  %>
  <body>
          1 :${helloWorld}<br>
          2 :<%=str%>
  </body>
</html>

3. ActionForm 클래스

public class HelloWorldForm extends ActionForm {
	/*
	 * Generated fields
	 */

	/** msg property */
	private String msg;

	/*
	 * Generated Methods
	 */

	/** 
	 * Method reset
	 * @param mapping
	 * @param request
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		// TODO Auto-generated method stub
	}

	/** 
	 * Returns the msg.
	 * @return String
	 */
	public String getMsg() {
		return msg;
	}

	/** 
	 * Set the msg.
	 * @param msg The msg to set
	 */
	public void setMsg(String msg) {
		this.msg = msg;
	}
}

4. action 클래스
[1] HelloWorld1Action 류

public class HelloWorld1Action extends Action{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		String str = "hello zhao ";
		request.setAttribute("helloWorld", str);
		return mapping.findForward("show");
	}
}

[2] HelloWorld 2Action 류

public class HelloWorld2Action extends Action {
			
public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HelloWordForm helloWordForm = (HelloWordForm) form;// TODO Auto-generated method stub
		String msg = helloWordForm.getMsg();
		request.setAttribute("helloWorld", msg);
		//response.setCharacterEncoding("utf-8");
		//response.setContentType("text/html;charset=utf-8");
		return mapping.findForward("show");
	}
}

[3] ThreeInputAction 클래스

public class ThreeInputAction extends DispatchAction {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		HelloWordForm helloWordForm = (HelloWordForm) form;// TODO Auto-generated method stub
		return null;
	}
}

2. struts - config. xml 설정
1. 입력 페이지 가 없고 action 과 출력 만 있 습 니 다.

<action-mappings >
    <action path="/helloWorld1" type="com.zhao.struts.action.HelloWorld1Action">
      <forward name="show" path="/WEB-INF/jsp/show.jsp" />
    </action>
</struts-config>

2. 입력 페이지 가 있 고 bean 이 없 으 면 action 은 request. getParameter ("msg") 를 사용 해 야 합 니 다.매개 변수
3. 비 안 이 있다

<form-beans >
    <form-bean name="helloWorldForm" type="com.zhao.struts.form.HelloWorldForm" />
</form-beans>

<action-mappings >
 <action
      attribute="helloWorldForm"
      name="helloWorldForm"
      path="/helloWorld2"
      scope="request"
      type="com.zhao.struts.action.HelloWorld2Action"
      validate="false">
      <forward name="show" path="/WEB-INF/jsp/show.jsp" />
    </action>
</action-mappings >

4. 페이지 설정 입력
[1] 직접 / form / input1. jsp 탐색 은 설정 하지 않 아 도 됩 니 다.
[2]

<!--struts   -->
 <action forward="/WEB-INF/jsp/input1.jsp" path="/input2" />
 <action include="/WEB-INF/jsp/input1.jsp" path="/input3" />
 <!--struts    -->   
     <action 
    path="/input4" 
    type="org.apache.struts.actions.ForwardAction"
    parameter="/WEB-INF/jsp/input1.jsp" />

5. 다 중 단추 설정

<action
      attribute="helloWorldForm"
      name="helloWorldForm"
      parameter="method"
      path="/threeInput"
      scope="request"
      type="com.zhao.struts.action.ThreeInputAction">
      <forward name="show" path="/WEB-INF/jsp/show.jsp" />
    </action>

좋은 웹페이지 즐겨찾기