Struts1.x的入门实例

1、定义一个PersonForm

package com.sunrex.demo06.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public final class PersonForm extends ActionForm {
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.username = null;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if(null == this.username || this.username.trim().length()<1) {
			errors.add("username", new ActionMessage("person.form.username.no.exist"));
		}
		return errors;
	}
}

3、定义资源文件applicationResources.properties, 内容如下:

person.jsp.title=Struts\u7684\u7b2c\u4e00\u4e2a\u5165\u95e8\u4f8b\u5b50
person.jsp.username=\u7528\u6237\u540d
person.jsp.submit=\u63d0\u4ea4

person.form.username.no.exist=\u7528\u6237\u540d\u4e0d\u5b58\u5728
person.action.username.login.error=\u7528\u6237\u540d\u3010jhl\u3011\u88ab\u7981\u7528

 

4、配置资源文件

<?xml version="1.0" encoding="ISO-8859-1" ?>

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

<struts-config>
	<form-beans>
		<form-bean name="personForm" type="com.sunrex.demo06.form.PersonForm"/>
	</form-beans>
	
	<action-mappings>
		<action path="/person"
			type="com.sunrex.demo06.action.PersonAction"
			scope="request"
			name="personForm"
			parameter="method"
			validate="true"
			input="/demo06/person.jsp"
		>
			<forward name="person" path="/demo06/person.jsp"/>
		</action>
	</action-mappings>
	
	<message-resources key="demo06" parameter="com.sunrex.demo06.applicationMessages"/>
</struts-config>

  

 

5、定义PersonAction

package com.sunrex.demo06.form;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public final class PersonForm extends ActionForm {
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	@Override
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.username = null;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		if(null == this.username || this.username.trim().length()<1) {
			errors.add("username", new ActionMessage("person.form.username.no.exist"));
		}
		return errors;
	}
}

 

6、person.jsp页面

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
    
<%@ taglib prefix="bean" uri="http://struts.apache.org/tags-bean" %>    
<%@ taglib prefix="html" uri="http://struts.apache.org/tags-html" %>    
<%@ taglib prefix="logic" uri="http://struts.apache.org/tags-logic" %>    
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title><bean:message bundle="demo06" key="person.jsp.title"/></title>
</head>
<body>
<html:errors bundle="demo06"/>
<logic:present name="personLoginSuccess">
	<bean:write name="personLoginSuccess" scope="request"/>
</logic:present>
<html:form action="/person.do" method="post">
	<bean:message bundle="demo06" key="person.jsp.username"/>
	<html:text property="username" size="6" maxlength="16"/><br>
	<html:submit value="submit-->"/>
</html:form>
</body>
</html:html>

 

 

 

你可能感兴趣的:(apache,html,jsp,bean,struts)