解答例 - 実習課題2 - 17.その他の Action クラス
(実習課題2)
1章の実習課題 2 の Web アプリケーションを改良しなさい。
- DispatchAction を使用すること。
- リクエストパラメータ「op」の値が指定されなかった場合には任意のエラーページを表示すること
解答例
▼ディレクトリ構成は以下の通り
. └─WEB-INF web.xml,struts-config.xml ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ) │ └─chapter17 │ └─exercise2 ParameterCheckAction.class ├─JSP │ └─com │ └─techscore │ └─struts │ └─chapter17 │ └─exercise2 ip.jsp,none.jsp,time.jsp └─lib strutsライブラリjarファイル ※strutsライブラリjarファイル struts.jar,commons-beanutils.jar,commons-collections.jar,commons-digester.jar,commons-logging.jar,jakarta-oro.jar
/** * ParameterCheckAction.java * TECHSCORE Java JakartaStruts 17章 実習課題2 * * Copyright (c) 2004 Four-Dimensional Data, Inc. */ package com.techscore.struts.chapter17.exercise2; import org.apache.struts.actions.DispatchAction; 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 ParameterCheckAction extends DispatchAction { public ActionForward time(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("time"); } public ActionForward ip(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("ip"); } protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { return mapping.findForward("none"); } }
<!-- ip.jsp --> <!-- TECHSCORE Java JakartaStruts 17章 実習課題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 17章 実習課題2</title></head> <body> <%= java.net.InetAddress.getLocalHost().toString()%> </body></html>
<!-- none.jsp --> <!-- TECHSCORE Java JakartaStruts 17章 実習課題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 17章 実習課題2</title></head> <body> <p>opパラメータ指定なし</p> </body></html>
<!-- time.jsp --> <!-- TECHSCORE Java JakartaStruts 17章 実習課題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 17章 実習課題2</title></head> <body> <%=new java.util.Date() %> </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> <action-mappings> <action path="/com/techscore/struts/chapter17/exercise2/ParameterCheck" type="com.techscore.struts.chapter17.exercise2.ParameterCheckAction" parameter="op"> <forward name="time" path="/WEB-INF/JSP/com/techscore/struts/chapter17/exercise2/time.jsp" /> <forward name="ip" path="/WEB-INF/JSP/com/techscore/struts/chapter17/exercise2/ip.jsp" /> <forward name="none" path="/WEB-INF/JSP/com/techscore/struts/chapter17/exercise2/none.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"> <!-- 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> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter17/exercise2/ParameterCheck.do
EncodingFilter.javaの参照(2章の実習課題2と同じ)