Struts入門レベルのログインフォームを作ってみた。

20090603223617
今回はログインフォームを作ってみました。
うんちくより先に、吟じます。

package section1;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action{

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginForm param = (LoginForm)form;
		if(isCheck(param)) {
			return mapping.findForward("success");
		} else {
			return mapping.findForward("error");
		}
	}

	private boolean isCheck(LoginForm param) {
		return "admin".equals(param.getName()) && "admin".equals(param.getPass());
	}

	
}
package section1;

import org.apache.struts.action.ActionForm;

public class LoginForm extends ActionForm{

	private String name;
	
	private String pass;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}
}
<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html:html>
	<html:form action="login">
		<table>
			<tr>
				<td>UserId:</td>
				<td><html:text property="name" /></td>
			</tr>
			<tr>
				<td>PassWord:</td>
				<td><html:password property="pass" /></td>
			</tr>
			<tr>
				<td><html:submit value="execute" /></td>
				<td><html:cancel value="clear"></html:cancel><br></td>
			</tr>
		</table>
	</html:form>
</html:html>
<%@ page language="java" contentType="text/html; charset=windows-31j"
    pageEncoding="windows-31j"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-31j">
<title>OK画面</title>
</head>
<body>
<!-- 認証失敗なら「<h2>失敗してるぜベイベー</h2>」 -->
	<h2>認証成功だぜベイベー</h2>
</body>
</html>
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    (the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at
   
         http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
          "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<!--
     This is a blank Struts configuration file with an example
     welcome action/page and other commented sample elements.

     Struts Validator is configured using the factory defaults
     and is ready-to-use.

     NOTE: If you have a generator tool to create the corresponding Java classes
     for you, you could include the details in the "form-bean" declarations.
     Otherwise, you would only define the "form-bean" element itself, with the
     corresponding "name" and "type" attributes, as shown here.
-->


<struts-config>

<form-beans>
	<form-bean name="section1" type="section1.LoginForm" />
</form-beans>

<action-mappings>
	<action name="section1" type="section1.LoginAction" path="/login" scope="session">
		<forward name="success" path="/section1/result.jsp" />
		<forward name="error" path="/section1/ng.jsp" />
	</action>
</action-mappings>

</struts-config>

動くかどうか確かめます。
このログインは以下で動きます

  • UserId・・・admin
  • PassWord・・・admin

まず違う値をいれてみます。
20090603223618

これだと結果は以下になります。
20090603223619

では正常値を入れて見ます。
20090603223620

すると結果は以下になります
20090603223616


う〜ん
あると思います。

早速やりたいことが増えてきたdamePGでした。