2010年8月14日土曜日

Struts Apache2.2 Tomcat5.5 連携基礎

Strutsを利用してURLが*.doで次ページに戻るアプリです。
とりあえず動くものをアップしておきます。

-----------------------------------------------------------------------------------------
■JSPページ作成
page4.jsp
-----------------------------------------------------------------------------------------
<%--
Document : page4
Created on : 2010/08/14
Author : springjoe2
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- タグライブラリ設定 -->
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test4JSP</title>
</head>
<body>
<h1>テスト4JSP</h1>
<html:link action="/page4action">自分に戻る</html:link>
</body>
</html>
---------------------------------------------------------------------------------------
■アクションクラス作成
Page4Action.java
---------------------------------------------------------------------------------------
/*
* 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 Page4Action 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);
}
}

---------------------------------------------------------------------------------------
■アクションフォームクラス作成
Page4ActionForm.java
---------------------------------------------------------------------------------------
/*
* 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 Page4ActionForm 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 Page4ActionForm() {
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;
}
}
-----------------------------------------------------------------------------------------
■いつものように最後にstruts-config.xmlを変更
<action-mappings>の<action path="/Welcome" forward="/page4.jsp"/>ウェルカムページ
1、forward4のkeyでpage4.jspへ飛ぶ
<global-forwards>の<forward name="forward4" path="/page4.jsp"/>
2、アクションマッピングpage4.jspへ飛ぶというアクション
<action input="/page4.jsp" name="Page4ActionForm" path="/page4action" scope="session" type="action.Page4Action"/>

0 件のコメント:

コメントを投稿