目次へ

解答例 - 実習課題2 - 2.ActionForm

(実習課題2)

実習課題1のActionFormを用いて以下のWebアプリケーションを作成しなさい。

  • 入力ページと出力ページを持ち、入力ページで社員データを入力する。出力ページでは社員データの表示を行う。
  • 社員データの受け渡しには、必ずActionFormを使用すること。
  • 日本語の入力値にも対応する事。

解答例

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

.
├─com
│  └─techscore
│      └─struts
│          └─chapter2
│              └─exercise2     inputEmployee.jsp,outputEmployee.jsp
└─WEB-INF              web.xml,struts-config.xml
    ├─classes
    │  └─com
    │      └─techscore
    │          └─struts       EncodingFilter.class
    │              └─chapter2
    │                  └─exercise2 InputEmployeeForm.class,OutputEmployeeAction.class
    └─lib              strutsライブラリjarファイル

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

package com.techscore.struts.chapter2.exercise2;

import org.apache.struts.action.ActionForm;

public class InputEmployeeForm extends ActionForm{
    private String name;
    private String address;
  
    public void setName(String name){
        this.name = name;
    }
  
    public String getName(){
        return name;
    }

    public void setAddress(String address){
        this.address = address;
    }
  
    public String getAddress(){
        return address;
    }

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

package com.techscore.struts.chapter2.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 OutputEmployeeAction extends Action{

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

        return mapping.findForward("output");

    }

}

▼EncodingFilter.java

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

package com.techscore.struts;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import java.io.IOException;

public final class EncodingFilter implements Filter {
    private String encoding = null;

    public void init(FilterConfig config) throws ServletException {

        encoding = config.getInitParameter("encoding");

    }

    public void doFilter(ServletRequest request, ServletResponse response,
                       FilterChain chain)
            throws ServletException, IOException {

        request.setCharacterEncoding(encoding);
        chain.doFilter(request, response);

    }

    public void destroy() {

        encoding = null;

    }

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

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

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

<h2>社員データ入力</h2>
<p>社員データを入力し、登録ボタンを押してください。</p>

<form action="<%=request.getContextPath() %>/com/techscore/struts/chapter2/exercise2/InputEmployee.do" method="post">
    <table>
        <tr><td>名前</td><td><input type="text" name="name"></td></tr>
        <tr><td>住所</td><td><input type="text" name="address"></td></tr>
    </table>
    <p><input type="submit" value="登録"></p>
</form>

※未入力チェックをしていないため、名前や住所が空でもそのまま登録されます。<br>
</body></html>
<!-- outputEmployee.jsp -->
<!-- TECHSCORE Java JakartaStruts 2章 実習課題2 -->
<!-- Copyright (c) 2004 Four-Dimensional Data, Inc. -->

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

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

<h2>社員データ入力</h2>
<p>登録された社員データは以下の通りです</p>

<jsp:useBean id="inputEmployeeForm22"
             class="com.techscore.struts.chapter2.exercise2.InputEmployeeForm"
             scope="request" />

<table border="2">
    <tr><th>氏名</th><td><%=inputEmployeeForm22.getName() %></td></tr>
    <tr><th>住所</th><td><%=inputEmployeeForm22.getAddress() %></td></tr>
</table>

<p><a href="<%=request.getContextPath() %>/com/techscore/struts/chapter2/exercise2/inputEmployee.jsp">入力に戻る</p>

</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="inputEmployeeForm22"
                   type="com.techscore.struts.chapter2.exercise2.InputEmployeeForm" />
    </form-beans>

    <action-mappings>
        <action path="/com/techscore/struts/chapter2/exercise2/InputEmployee"
                type="com.techscore.struts.chapter2.exercise2.OutputEmployeeAction"
                name="inputEmployeeForm22"
                scope="request">
            <forward name="output"
                     path="/com/techscore/struts/chapter2/exercise2/outputEmployee.jsp" />
        </action>
    </action-mappings>

</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">

<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>
    </servlet>

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

</web-app>

▼起動URLは以下の通り

WEB_ROOT/com/techscore/struts/chapter2/exercise2/inputEmployee.jsp

↑このページの先頭へ

こちらもチェック!

PR
  • XMLDB.jp