解答例 - 実習課題3 - 9.Logicタグライブラリ2
(実習課題3)
実習課題2のアプリケーションを改良しなさい。
- エラーがある場合とない場合とで、入力フォームに表示するメッセージを変えるようにしなさい。
解答例
▼ディレクトリ構成は以下の通り
. ├─com │ └─techscore │ └─struts │ └─chapter9 │ └─exercise3 login.jsp └─WEB-INF web.xml(実習課題1と同じ),struts-config.xml ├─classes │ └─com │ └─techscore │ └─struts EncodingFilter.class(2章 実習課題2と同じ), │ │ MessageResources.properties(実習課題2と同じ) │ └─chapter9 │ └─exercise3 LoginForm.class,RegisterAction.class ├─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
/** * LoginForm.java * TECHSCORE Java JakartaStruts 9章 実習課題3 * * Copyright (c) 2004 Four-Dimensional Data, Inc. * */ package com.techscore.struts.chapter9.exercise3; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionErrors; import org.apache.struts.action.ActionError; import javax.servlet.http.HttpServletRequest; public class LoginForm extends ActionForm{ private String userName = ""; public void setUserName(String userName){ this.userName = userName; } public String getUserName(){ return userName; } public ActionErrors validate(ActionMapping mapping, HttpServletRequest request){ ActionErrors errors = new ActionErrors(); if (userName.equals("")) { errors.add("userName", new ActionError("invalid.userName")); } return errors; } }
/** * RegisterAction.java * TECHSCORE Java JakartaStruts 9章 実習課題3 * * Copyright (c) 2004 Four-Dimensional Data, Inc. * */ package com.techscore.struts.chapter9.exercise3; 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; import javax.servlet.http.Cookie; import java.net.URLEncoder; public class RegisterAction extends Action{ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{ LoginForm loginForm = (LoginForm)form; Cookie cookie = new Cookie("userName", URLEncoder.encode(loginForm.getUserName(), "EUC-JP")); cookie.setMaxAge(60); response.addCookie(cookie); return mapping.findForward("login"); } }
<!-- login.jsp --> <!-- TECHSCORE Java JakartaStruts 9章 実習課題3 --> <!-- Copyright (c) 2004 Four-Dimensional Data, Inc. --> <%@ page contentType="text/html; charset=Windows-31J" session="false" pageEncoding="Windows-31J" import="java.net.URLDecoder" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <html:html> <head><title>TECHSCORE Java JakartaStruts 9章 実習課題3</title></head> <body> <logic:notPresent cookie="userName"> <logic:messagesNotPresent> <p>ユーザ名を入力してください。</p> </logic:messagesNotPresent> <logic:messagesPresent> <p><html:errors property="userName" /></p> </logic:messagesPresent> <html:form action="/com/techscore/struts/chapter9/exercise3/Login.do" method="post"> <p>ユーザ名 : <html:text property="userName" /></p> <p><html:submit value="登録" /></p> </html:form> </logic:notPresent> <logic:present cookie="userName"> <bean:cookie id="user" name="userName"/> <%=URLDecoder.decode(user.getValue(), "EUC-JP") %>さんようこそ </logic:present> </body> </html: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="loginForm93" type="com.techscore.struts.chapter9.exercise3.LoginForm" /> </form-beans> <action-mappings> <action path="/com/techscore/struts/chapter9/exercise3/Login" type="com.techscore.struts.chapter9.exercise3.RegisterAction" name="loginForm93" scope="request" validate="true" input="/com/techscore/struts/chapter9/exercise3/login.jsp"> <forward name="login" path="/com/techscore/struts/chapter9/exercise3/login.jsp" redirect="true" /> </action> </action-mappings> <message-resources parameter="com.techscore.struts.MessageResources" /> </struts-config>
▼起動URLは以下の通り
WEB_ROOT/com/techscore/struts/chapter9/exercise3/login.jsp
EncodingFilter.javaの参照(2章の実習課題2と同じ)
MessageResources.propertiesの参照(実習課題2と同じ)