目次へ

解答例 - 実習課題2 - 6.HTMLタグライブラリ2

(実習課題2)

以下のWebアプリケーションを、Strutsを用いて作成しなさい。

  • imageタグを用いて画像ボタンを作成する事。
  • 押された座標を、beanタグライブラリのwriteタグを用いて表示する事。

解答例

▼ディレクトリ構成は以下の通り

.
├─com
│  └─techscore
│      └─struts
│          └─chapter6
│              └─exercise2     useImageTag.jsp,displayClickPoint.jsp
│                  └─img      submit.gif(どんなものでもいいのでイメージファイルを準備)
└─WEB-INF              web.xml(実習課題1と同じ),struts-config.xml
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ),
    │              │           MessageResources.properties(中身は空)
    │              └─chapter6
    │                  └─exercise2 ImageTagAction.class,ImageTagForm.class
    ├─lib              strutsライブラリjarファイル
    └─tld              struts-bean.tld,struts-html.tld

※strutsライブラリjarファイル
  struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar
/**
 * ImageTagAction.java
 * TECHSCORE Java JakartaStruts 6章 実習課題2
 *
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 *
 */

package com.techscore.struts.chapter6.exercise2;

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

public class ImageTagAction extends Action{

    public ActionForward execute(ActionMapping mapping, ActionForm form,
                               HttpServletRequest request, HttpServletResponse response)
            throws Exception{

        return mapping.findForward("display");

    }

}
/**
 * ImageTagForm.java
 * TECHSCORE Java JakartaStruts 6章 実習課題2
 *
 * Copyright (c) 2004 Four-Dimensional Data, Inc.
 *
 */

package com.techscore.struts.chapter6.exercise2;

import org.apache.struts.action.ActionForm;

public class ImageTagForm extends ActionForm{

    public class Button{
        private int x = 0;
        private int y = 0;

        public void setX(int x) {
            this.x = x;
        }
        
        public int getX() {
            return x;
        }
        
        public void setY(int y) {
            this.y = y;
        }

        public int getY() {
            return y;
        }
    }

    private Button button = new Button();

    public Button getButton(){
        return button;
    }

    public void setButton(Button button){
        this.button = button;
    }

}
<!-- displayClickPoint.jsp -->
<!-- TECHSCORE Java JakartaStruts 6章 実習課題2 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@ page contentType="text/html; charset=Windows-31J"
         session="false" 
         pageEncoding="Windows-31J" %>

<%@ 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>TECHSCORE Java JakartaStruts 6章 実習課題2</title></head>
<body>

<p>ボタン押下位置の座標</p>

<p>x : <bean:write name="imageTagForm" scope="request" property="button.x" /></p>
<p>y : <bean:write name="imageTagForm" scope="request" property="button.y" /></p>

</body>
</html>
<!-- useImage.jsp -->
<!-- TECHSCORE Java JakartaStruts 6章 実習課題2 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

<%@ page contentType="text/html; charset=Windows-31J"
         session="false" 
         pageEncoding="Windows-31J" %>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html"
           prefix="html" %>

<html>
<head><title>TECHSCORE Java JakartaStruts 6章 実習課題2</title></head>
<body>

<p>ボタンを押してください。座標が表示されます。</p>

<html:form action="/com/techscore/struts/chapter6/exercise2/ImageTag.do" method="post">
    <html:image page="/com/techscore/struts/chapter6/exercise2/img/submit.gif"
                property="button" />
</html:form>

</body>
</html>

▼struts-config.xml

<?xml version="1.0" encoding="Windows-31J" ?>
<!DOCTYPE struts-config PUBLIC
  "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
  "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

    <form-beans>
        <form-bean name="imageTagForm"
                   type="com.techscore.struts.chapter6.exercise2.ImageTagForm" />
    </form-beans>

    <action-mappings>
        <action path="/com/techscore/struts/chapter6/exercise2/ImageTag"
                type="com.techscore.struts.chapter6.exercise2.ImageTagAction"
                name="imageTagForm"
                scope="request">
            <forward name="display"
                     path="/com/techscore/struts/chapter6/exercise2/displayClickPoint.jsp" />
        </action>
    </action-mappings>

    <message-resources parameter="com.techscore.struts.MessageResources" />

</struts-config>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter6/exercise2/useImageTag.jsp

web.xmlの参照(実習課題1と同じ)

EncodingFilter.javaの参照(2章の実習課題2と同じ)


↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp