从Eclipse里建个web project,然后加入Struts 标签.这样就可以做我的东西了.
1.建个hello.jsp页面,这个页面也就是所说的视图页面,使用的是Struts标签.
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html locale="true">
<head>
<title><bean:message key="hello.jsp.title"/></title>
<html:base/>
</head>
<body bgcolor="white"><p>
<h2><bean:message key="hello.jsp.page.heading"/></h2><p>
<html:errors/><p>
<logic:present name="personbean" scope="request">
<h2>
<bean:message key="hello.jsp.page.hello"/>
<bean:write name="personbean" property="userName" />!<p>
</h2>
</logic:present>
<html:form action="/HelloWorld.do" focus="userName" >
<bean:message key="hello.jsp.prompt.person"/>
<html:text property="userName" size="16" maxlength="16"/><br>
<html:submit property="submit" value="Submit"/>
<html:reset/>
</html:form><br>
<html:img page="/struts-power.gif" alt="Powered by Struts"/>
</body>
</html:html>
2.创建消息资源文件application.properties ,这个文件是用来定义一些常量的文本信息.把信息放到资源文件中,使用起来相对方便.
#Application Resources for the "Hello" sample application
#Application Resources that are specific to the hello.jsp file
hello.jsp.title=Hello - A first Struts program
hello.jsp.page.heading=Hello World! A first Struts application
hello.jsp.prompt.person=Please enter a UserName to say hello to :
hello.jsp.page.hello=Hello
#Validation and error messages for HelloForm.java and HelloAction.java
hello.dont.talk.to.monster=We don't want to say hello to Monster!!!
hello.no.username.error=Please enter a <i>UserName</i> to say hello to!
3.再来创建一个表单文件HelloForm.java,这个表单是与hello.jsp中表单字段一一对应的,主要是set,get方法.
package com.boyang.co;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class HelloForm extends ActionForm {
private String userName = null;
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void reset(ActionMapping mapping,
HttpServletRequest request) {
this.userName = null;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionError("hello.no.username.error"));
return errors;
}
}
其中,包含了一个validate()方法,用来进行表单验证的,如果验证错误,Struts框架会把ActionErrors对象保存到request范围中,然后把请求转发到hello.jsp视图文件中,视图文件通过<html:error>标签把request范围内的ActionErrors对象中包含的错误信息显示出来.
4.接下来,我们创建控制器组件HelloAction.java. 组件:ActionServlet类和Action类.ActionServlet是Struts框架的控制枢纽,Action是用来处理特定的HTTP请求.
package com.boyang.co;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
public final class HelloAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
// These "messages" come from the ApplicationResources.properties file
// 用来读取资源文件.
MessageResources messages = getResources(request);
ActionErrors errors = new ActionErrors();
String userName = (String)((HelloForm) form).getUserName();
String badUserName = "Monster";
// 用<html:errors/>标签来显示错误消息.
if (userName.equalsIgnoreCase(badUserName)) {
errors.add("username", new ActionError("hello.dont.talk.to.monster", badUserName));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
PersonBean pb = new PersonBean();
pb.setUserName(userName);
// 持久化的消息处理.
pb.saveToPersistentStore();
request.setAttribute( Constants.PERSON_KEY, pb);
// Remove the Form Bean - don't need to carry values forward
request.removeAttribute(mapping.getAttribute());
// Forward control to the specified success URI
return (mapping.findForward("SayHello"));
}
}
5.模型组件PersonBean.java
模型组件把业务逻辑的实现和其它的应用分开,提高了灵活性,重用性,可扩展性.可用来保存持久化的数据系统中.
package com.boyang.co;
public class PersonBean {
private String userName = null;
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
// 持久化的消息处理.
public void saveToPersistentStore() {
//
}
}
6.创建常量文件Constants.java
package com.boyang.co;
public final class Constants {
public static final String PERSON_KEY = "personbean";
}
7.创建WEB应用配置文件web.xml,应该对ActionServlet类进行配置,此外还应该包含标签库:Struts Bean,Struts HTML,Struts Logic标签库.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>HelloApp Struts Application</display-name>
<!-- Standard Action Servlet Configuration -->
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- Standard Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>hello.jsp</welcome-file>
</welcome-file-list>
<!-- Struts Tag Library Descriptors -->
<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>
8.创建Struts框架的配置文件struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<!--
This is the Struts configuration file for the "Hello!" sample application
-->
<struts-config>
<!-- ======== Form Bean Definitions =================================== -->
<form-beans>
<form-bean name="HelloForm" type="com.boyang.co.HelloForm"/>
</form-beans>
<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<!-- Say Hello! -->
<action path = "/HelloWorld"
type = "com.boyang.co.HelloAction"
name = "HelloForm"
scope = "request"
validate = "true"
input = "/hello.jsp"
>
<forward name="SayHello" path="/hello.jsp" />
</action>
</action-mappings>
<!-- ========== Message Resources Definitions =========================== -->
<message-resources parameter="com.boyang.co.application"/>
</struts-config>
9.好了,我们就可以编译发布应用.
(环境配置:Eclipse3.2, apache-tomcat-5.5.16, jdk1.5.0_08, IE7.0, Windows XP)