そうだ、今年、やりのこしたことがあった。
Dukeマウス、getしたのに、ここで紹介してなかったので、
いま、紹介します。こんなかんじ
![](https://mokano.main.jp/blogimg/sun061215.jpg)
今年中に、まにあった。。かな?
package pac1; public class Shori1 { public String rituKeisan(String nyuryoku) { //==============================// // データの取得 // //==============================// if ( nyuryoku == null ) { return "NO DATA!"; } else if ( nyuryoku.length() == 0 ) { return "NO DATA!"; } // 処理 int i,atari; atari = 0; String chk = "This is a pen"; for(i = 0 ; i < nyuryoku.length() ; i ++ ) { if ( i >= chk.length() ) { break; } else if ( chk.charAt(i) == nyuryoku.charAt(i) ) { atari++; } } //==============================// // 結果を返す // //==============================// return Integer.toString(atari*100/chk.length()); } } |
Tomcatのフォルダ |-webapps |-アプリを置くフォルダ |-jsp | *-JSPファイル |-WEB-INF |-struts-config.xml |-web.xml |-lib | |-(中身は省略) | |-classes |-パッケージ名 |-Javaクラスファイル |
Tomcatのフォルダ |-webapps |-stest |-jsp | |-index.jsp | |-kekka.jsp | *-error.jsp | |-WEB-INF |-struts-config.xml |-web.xml |-lib | |-(中身は省略) | |-classes |-pac1 |-inputForm.class |-errorForm.class |-kekkaForm.class |-ShoriCtrl.class *-Shori1.class |
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" "http://struts.apache.org/dtds/struts-config_1_3.dtd"> <struts-config> <form-beans> <form-bean name="inputForm" type="pac1.inputForm" /> <form-bean name="kekkaForm" type="pac1.kekkaForm" /> <form-bean name="errorForm" type="pac1.errorForm" /> </form-beans> <action-mappings> <action path="/shori" type="pac1.ShoriCtrl" name="inputForm" scope="request" validate="false"> <forward name="success" path="/jsp/kekka.jsp" /> <forward name="error" path="/jsp/error.jsp" /> </action> </action-mappings> </struts-config> |
inputForm inputform = (inputForm)form; |
String nyuryoku = inputform.getNyuryoku(); |
// モデル呼び出し Shori1 shori1 = new Shori1(); String kekka = shori1.rituKeisan(nyuryoku); |
kekkaForm kekkaform = new kekkaForm(); |
kekkaform.setRitu(kekka); |
request.setAttribute("kekkaForm",kekkaform); |
return map.findForward("success"); |
package pac1; import javax.servlet.http.*; import org.apache.struts.action.*; public final class ShoriCtrl extends Action { public ActionForward execute (ActionMapping map, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // ここに上記処理を書く } } |
package pac1; import javax.servlet.http.*; import org.apache.struts.action.*; public final class ShoriCtrl extends Action { public ActionForward execute (ActionMapping map, ActionForm form, HttpServletRequest request, HttpServletResponse response) { // データの入力 inputForm inputform = (inputForm)form; String nyuryoku = inputform.getNyuryoku(); // モデル呼び出し Shori1 shori1 = new Shori1(); String kekka = shori1.rituKeisan(nyuryoku); // エラー時の出力 if ( kekka == null ) { errorForm errorform = new errorForm(); errorform.setMsg("NO DATA!"); request.setAttribute("errorForm",errorform); return map.findForward("error"); } else if ( kekka.equals("NO DATA!") == true) { errorForm errorform = new errorForm(); errorform.setMsg("NO DATA!"); request.setAttribute("errorForm",errorform); return map.findForward("error"); } // 正常時の処理 kekkaForm kekkaform = new kekkaForm(); kekkaform.setRitu(kekka); request.setAttribute("kekkaForm",kekkaform); return map.findForward("success"); } } |
package パッケージ名; import org.apache.struts.action.*; public final class クラス名 extends ActionForm { } |
public void setNyuryoku(String nyuryoku) { this.nyuryoku=nyuryoku; } public String getNyuryoku() { return nyuryoku; } |
package pac1; import org.apache.struts.action.*; public final class inputForm extends ActionForm { private String nyuryoku; public void setNyuryoku(String nyuryoku) { this.nyuryoku=nyuryoku; } public String getNyuryoku() { return nyuryoku; } } |
package pac1; import org.apache.struts.action.*; public final class errorForm extends ActionForm { private String nyuryoku; private String msg; public void setNyuryoku(String nyuryoku) { this.nyuryoku=nyuryoku; } public String getNyuryoku() { return nyuryoku; } public void setMsg(String msg) { this.msg=msg; } public String getMsg() { return msg; } } |
package pac1; import org.apache.struts.action.*; public final class kekkaForm extends ActionForm { private String ritu; public void setRitu(String ritu) { this.ritu=ritu; } public String getRitu() { return ritu; } } |
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html:html> <head> <title>早打ちの練習</title> </head> <body> <H1>早うちの練習</H1> 以下の文を打とう!<BR> <B>This is a pen</B><BR> <html:form action="/shori.do"> <html:text property="nyuryoku" size="20" /><P> <html:submit property="do" value="打ち終わった!" /><P> </html:form> </html:html> |
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html:html> <head> <title>早打ちの練習</title> </head> <body> <h1><font color=red><bean:write name="errorForm" property="msg" scope="request"/>!</font></h1> <H1>早うちの練習</H1> 以下の文を打とう!<BR> <B>This is a pen</B><BR> <html:form action="/shori.do"> <html:text property="nyuryoku" size="20" /><P> <html:submit property="do" value="打ち終わった!" /><P> </html:form> </html:html> |
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> <html:html> <head> <title>早打ちの練習</title> </head> <body> <H1>結果</H1> 正解率 = <bean:write name="kekkaForm" property="ritu" scope="request"/>%<BR> </html:html> |