目次へ

解答例 - 実習課題3 - 17.その他の Action クラス

(実習課題3)

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

  • 入力フォームにはテキストボックスが 1つだけ含まれる。
  • submit ボタンは [表示] と [検索] の 2つ
  • 表示ボタンが押されたときは、入力された値を表示する。
  • 検索ボタンが押されたときは、入力された値を Google で検索した結果ページを表示する。

解答例

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

.
└─WEB-INF              web.xml,struts-config.xml
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class(2章 実習課題2と同じ),
    │              │           MessageResources.properties
    │              └─chapter17
    │                  └─exercise3 InputForm.class,LookupAction.class
    ├─JSP
    │  └─com
    │      └─techscore
    │          └─struts
    │              └─chapter17
    │                  └─exercise3 display.jsp,input.jsp,search.jsp
    ├─lib              strutsライブラリ jarファイル
    └─tld              struts-bean.tld,struts-html.tld,struts-logic.tld


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

package com.techscore.struts.chapter17.exercise3;

import org.apache.struts.action.ActionForm;

public class InputForm extends ActionForm {
    private String inputText = "";

    public void setInputText(String inputText) {
        this.inputText = inputText;
    }

    public String getInputText() {
        return inputText;
    }

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

package com.techscore.struts.chapter17.exercise3;

import org.apache.struts.actions.LookupDispatchAction;
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;
import java.util.Map;
import java.util.HashMap;

public class LookupAction extends LookupDispatchAction {

    protected Map getKeyMethodMap() {
        Map map = new HashMap();
        map.put("button.display", "display");
        map.put("button.search", "search");
        return map;
    }

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

        return mapping.findForward("display");
    }

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

        return mapping.findForward("search");
    }

}
<!-- input.jsp -->
<!-- TECHSCORE Java JakartaStruts 17章 実習課題3 -->
<!-- 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" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean"
           prefix="bean" %>

<html:html>
<head><title>TECHSCORE Java JakartaStruts 17章 実習課題3</title></head>
<body>

<p>何か文字を入力して、どちらかのボタンを押してください。<br>
   表示ボタンを押すと、入力された文字を表示します。<br>
   検索ボタンを押すと、入力文字をGoogleで検索した結果が表示されます。</p>

<html:form action="/com/techscore/struts/chapter17/exercise3/Input.do" method="post">

    <p><html:text property="inputText" /></p>

    <p><html:submit property="method">
           <bean:message key="button.display" />
       </html:submit>
       <html:submit property="method">
           <bean:message key="button.search" />
       </html:submit></p>

</html:form>

</body>
</html:html>
<!-- display.jsp -->
<!-- TECHSCORE Java JakartaStruts 17章 実習課題3 -->
<!-- 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 17章 実習課題3</title></head>
<body>

<p>入力された文字 : 
   <bean:write name="inputForm173" scope="request" property="inputText" /></p>

<p><html:link page="/com/techscore/struts/chapter17/exercise3/ShowInputForm.do">入力に戻る</html:link></p>

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

<%@ page import="java.net.URLEncoder" %>

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

<logic:redirect href="<%="http://www.google.co.jp/search?q=" + URLEncoder.encode(request.getParameter("inputText"), "UTF-8") %>" />

▼MessageResources.properties

button.display=display
button.search=search

▼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="inputForm173"
                   type="com.techscore.struts.chapter17.exercise3.InputForm" />

    </form-beans>

    <action-mappings>
    
        <action path="/com/techscore/struts/chapter17/exercise3/ShowInputForm"
                type="org.apache.struts.actions.ForwardAction"
                parameter="/WEB-INF/JSP/com/techscore/struts/chapter17/exercise3/input.jsp">
        </action>

        <action path="/com/techscore/struts/chapter17/exercise3/Input"
                type="com.techscore.struts.chapter17.exercise3.LookupAction"
                parameter="method"
                name="inputForm173"
                scope="request">

            <forward name="display"
                     path="/WEB-INF/JSP/com/techscore/struts/chapter17/exercise3/display.jsp" />
            <forward name="search"
                     path="/WEB-INF/JSP/com/techscore/struts/chapter17/exercise3/search.jsp" />
        </action>

    </action-mappings>

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


</struts-config>

▼web.xml

<?xml version="1.0" encoding="Windows-31J" ?>
<!DOCTYPE web-app PUBLIC
  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/j2ee/dtds/web-app_2_3.dtd">

<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<web-app>

    <filter>
        <filter-name>Encoding</filter-name>
        <filter-class>com.techscore.struts.EncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>Windows-31J</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <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>
        <init-param>
            <param-name>debug</param-name>
            <param-value>6</param-value>
        </init-param>
        <init-param>
            <param-name>config/admin</param-name>
            <param-value>/WEB-INF/struts-config-admin.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/struts/tags-bean</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-bean.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/struts/tags-html</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-html.tld</taglib-location>
    </taglib>

    <taglib>
        <taglib-uri>http://jakarta.apache.org/struts/tags-logic</taglib-uri>
        <taglib-location>/WEB-INF/tld/struts-logic.tld</taglib-location>
    </taglib>

</web-app>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter17/exercise3/ShowInputForm.do

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


↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp