eclipse中使用struts框架练习

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

test1.jsp

<%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix = "bean" uri = "struts-bean" %>
<%@ taglib prefix = "html" uri = "struts-html" %>
<%@ taglib prefix = "logic" uri = "struts-logic" %>
<%@ taglib prefix = "template" uri = "struts-template" %>
<!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=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="js/sample.js"></script>
</head>
<body>
<!--
<div class="button-text">buttonタグ</div>
<button type="button" οnclick="change()" disabled>テキスト変更</button>
 <input type="text" disabled> 
-->
<div>メールアドレス</div>
<input type = "text" id = "mailAddress" disabled><br>
<input type = "checkbox" id = "checkBox1" value="" onchange= "change2()"><span>広告を受け取る</span><br>
<input type="image" src="<html:rewrite page="/img/good.png" />" id="send" alt="送信">
</body>
</html>

js/sample.js

/**
 * 
 */
function change() {
	var elements = document.getElementsByClassName("button-text");
	elements[0].innerHTML = "テキストが変更されました。"
}

function change2() {
	var element;
	if (document.getElementById("checkBox1").checked) {
		element = document.getElementById("mailAddress");
		element.disabled = false;
	} else {
		element = document.getElementById("mailAddress");
		element.disabled = true;
	}
}

user_register.jsp

<%@ page language="java" contentType="text/html; UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="bean" uri="struts-bean"%>
<%@ taglib prefix="html" uri="struts-html"%>
<%@ taglib prefix="logic" uri="struts-logic"%>
<%@ taglib prefix="template" uri="struts-template"%>

<!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=UTF-8">
<title>ユーザー登録画面</title>
</head>
<body>
<h1>この画面は<%= request.getAttribute("register") %></h1>
	<html:form action="/tagSample/RegisterAction">
		<!-- 名前:<input type = "text" name="username"/>  -->
		<!--パスワード:<input type = "text" name= "password"/><br> -->
名前:<html:text property="username" /><br>
パスワード: <html:text property="password" /><br>
		<!--<input type = "submit" value = "登録"> -->
		<html:submit property="submit" value="登録"/>
		<html:reset value="リセット" />
		<html:reset value="戻る" />
		<logic:present name="registerForm" property="username">
			<bean:define id="username" name="registerForm" property="username"
				type="java.lang.String" />
		</logic:present>
	</html:form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%>   
<%@ taglib prefix = "bean" uri = "struts-bean" %>
<%@ taglib prefix = "html" uri = "struts-html" %>
<%@ taglib prefix = "logic" uri = "struts-logic" %>
<%@ taglib prefix = "template" uri = "struts-template" %>

<!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=UTF-8">
<title>登録成功</title>
</head>
<body>
register success!!!
</body>
</html>

failure.jsp

<%@ page language="java" contentType="text/html; UTF-8" pageEncoding="UTF-8"%>   
<%@ taglib prefix = "bean" uri = "struts-bean" %>
<%@ taglib prefix = "html" uri = "struts-html" %>
<%@ taglib prefix = "logic" uri = "struts-logic" %>
<%@ taglib prefix = "template" uri = "struts-template" %>

<!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=UTF-8">
<title>登録失敗</title>
</head>
<body>
register failed!!!
</body>
</html>

RegisterAction.java

package tagSample;

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 RegisterAction extends Action {
	public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest req,
			HttpServletResponse res) {
		RegisterForm registerForm = (RegisterForm) form;
		// String userName = request.getParameter("username");
		req.setAttribute("register", "ユーザー登録");
		String username = registerForm.getUsername();
		String password = registerForm.getPassword();
		System.out.println(username);
		if ((username == null || username.length() == 0) && (password == null || password.equals(""))) {
			return mapping.findForward("REGISTER_FAILED");
		}
		return mapping.findForward("REGISTER_SUCCESS");

	}
}

RegisterForm .java

package tagSample;

import org.apache.struts.action.ActionForm;

public class RegisterForm extends ActionForm{
	private String username;
	private String password;
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

WEB-INF/struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>

	<form-beans>
		<!--beanにはデータを格納するActionFormクラスを設定します-->
		<!--nameは略称、typeはpathを含むファイル名です-->
		<form-bean name="tagForm"
			type="tagSample.TagForm">
		</form-bean>
		
		<!-- RegisterForm -->
		<form-bean name = "registerForm"
		type = "tagSample.RegisterForm">
		</form-bean>
	</form-beans>

	<global-forwards>
		<forward name="welcome" path="/tagSample.jsp"/>
	</global-forwards>

	<action-mappings>
		<!--actionは実際に処理を行うActionクラスを設定します-->
		<!--pathはjspで指定するときの略称です-->
		<!--nameは使用するbean(ActionForm)の略称を入れます-->
		<!--typeはpathを含むファイル名です-->
		<!--scopeはデータ転送形式です-->
		<!--inputは実行されるjspです-->

		<!-- Equalロジック確認用 -->
		<action path = "/tagSample/RegisterAction" type = "tagSample.RegisterAction" 
		name = "registerForm" scope = "request">
			<forward name = "REGISTER_SUCCESS" path = "/user_register.jsp"/>
			<forward name = "REGISTER_FAILED" path = "/failure.jsp"/>		
		</action>
		<action path="/tagSample/TagEqualAction" type="tagSample.TagEqualAction"
			name="tagForm" scope="request">
			<forward name="SUCCESS" path="/tagSample.jsp"/>
		</action>
		<!-- Presentロジック確認用 -->
		<action path="/tagSample/TagPresentAction" type="tagSample.TagPresentAction"
			name="tagForm" scope="request">
			<forward name="SUCCESS" path="/tagSample.jsp"/>
		</action>
		<!-- Iterateロジック確認用 -->
		<action path="/tagSample/TagIterateAction" type="tagSample.TagIterateAction"
			name="tagForm" scope="request">
			<forward name="SUCCESS" path="/tagSample.jsp"/>
		</action>
		<!-- greaterロジック確認用 -->
		<action path="/tagSample/TagGreaterAction" type="tagSample.TagGreaterAction"
			name="tagForm" scope="request">
			<forward name="SUCCESS" path="/tagSample.jsp"/>
		</action>
		<!-- lessロジック確認用 -->
		<action path="/tagSample/TagLessAction" type="tagSample.TagLessAction"
			name="tagForm" scope="request">
			<forward name="SUCCESS" path="/tagSample.jsp"/>
		</action>
		<!-- matchロジック確認用 -->
		<action path="/tagSample/TagMatchAction" type="tagSample.TagMatchAction"
			name="tagForm" scope="request">
			<forward name="SUCCESS" path="/tagSample.jsp"/>
		</action>
	</action-mappings>
</struts-config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值