2010年8月15日日曜日

JSP/サーブレットで値の受け渡し【Struts】

今回はStrutsを利用してJSP/Servletによる値の受け渡し方法を説明します。
page5.jspからpage6.jspへの遷移をStrutsを用いて実施しています。
いつものように最後にstruts-config.xmlの追記を忘れないようにしましょう!!

1、page5.jspの作成
以前作成したpage5.jspに以下を追記
<html:form action="/page6" method="POST">
<html:text property="value1" name="value1" value=""></html:text>
<html:submit value="送信"></html:submit>
</html:form>

2、page6.jspの作成
ただ値を受け取るだけのページです。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%= request.getParameter("value1") %>
<h1>page5.jspから送信された値</h1>
</body>
</html>

3、ActionFormクラスの作成
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


package actionform;


import javax.servlet.http.HttpServletRequest;


import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;


/**
*
* @author x300
*/
public class Page6ActionForm extends org.apache.struts.action.ActionForm {


private String name;


private int number;


/**
* @return
*/
public String getName() {
return name;
}


/**
* @param string
*/
public void setName(String string) {
name = string;
}


/**
* @return
*/
public int getNumber() {
return number;
}


/**
* @param i
*/
public void setNumber(int i) {
number = i;
}


/**
*
*/
public Page6ActionForm() {
super();
// TODO Auto-generated constructor stub
}


/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param request The HTTP Request we are processing.
* @return
*/
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (getName() == null || getName().length() < 1) {
errors.add("name", new ActionMessage("error.name.required"));
// TODO: add 'error.name.required' key to your resources
}
return errors;
}
}

4、Actionクラスの作成
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


package action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;


/**
*
* @author x300
*/
public class Page6Action extends org.apache.struts.action.Action {


/* forward name="success" path="" */
private static final String SUCCESS = "success";


/**
* This is the action called from the Struts framework.
* @param mapping The ActionMapping used to select this instance.
* @param form The optional ActionForm bean for this request.
* @param request The HTTP Request we are processing.
* @param response The HTTP Response we are processing.
* @throws java.lang.Exception
* @return
*/
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {


return mapping.findForward(SUCCESS);
}
}

5、suturuts-config.xmlに登録
ACTION-MAPPINGタグに追加するもの
<action input="/page6.jsp" name="Page6ActionForm" path="/page6" scope="session" type="action.Page6Action"/>
FORM-BEANSタグに追加するもの
<form-bean name="Page6ActionForm" type="actionform.Page6ActionForm"/>

0 件のコメント:

コメントを投稿