struts作为非常成功的经典开源MVC模式,它是如下运作的:
[color=blue][b]
[size=large]
* Controller(控制器)由ActionServlet和Action类来实现。
* ActionServlet负责读取struts-config.xml配置文件,使用
ActionMapping来查找对应的Action,并把请求转发给Action类。
* 对于大型应用,Action类充当用户请求和业务逻辑处理之间的Adaptot(适配器)。
* 在Model(模型)端,业务逻辑可以采用Java Bean或者EJB来完成。
* ActionForm Bean负责传递系统状态
* 视图端主要是JSP,struts通过其内部的标签库进行了扩展。
[/size]
[/b][/color]
下面我们来举个简单的helloapp的例子。
[b]
场景: 页面端有个文本,根据用户输入的名字,返回Hello 输入名。
如果用户输入空,则提示不能为空;如果用户输入的是monstor,
则提示非法。
[/b]
我们需要建立一个jsp文件,在上面放上提示,文本框,按钮还有错误提示。
在上面的代码中,我们使用了许多struts自己的标签。
我们来分析下:
[table]
|<html:form>||创建HTML表单,能够将表单字段和ActionForm Bean的属性关联起来。|
|<bean:message>||输出本地化文本,key属性对应文本。|
|<html:text>||创建文本框。|
|<bean:write>||输出JavaBean的属性值,property对应属性。|
|<html:errors>||显示错误信息。|
|<logic:present>||判断JavaBean在特定的范围那是否存在,存在才会去执行主体内容。|
|<html:submit>||提交按钮。|
|<html:reset>||清空按钮。|
[/table]
由于我们使用了许多消息文本,我们需要将这些文本本地化,因此
我们需要创建资源文件。
applicatoin.properties:
接下来,我们需要创建ActionForm Bean。
HelloForm.java:
[color=red][b]注:ActionErrors类是ActionMessage的子类。
从struts1.2开始废弃ActionErrors,统一使用ActionMessage来表示正常或
错误消息。[/b][/color]
接下来,我们需要创建常量类和业务类。
Constants.java:
PersonBean.java:
接着,我们便可以创建重点类 - Action类了。
HelloAction.java:
最后,我们需要在我们的核心配置文件struts-config.xml中配置Action映射,
全局转向(forward),FormBean定义,资源文件定义。
struts-config.xml:
[color=blue][b]
[size=large]
* Controller(控制器)由ActionServlet和Action类来实现。
* ActionServlet负责读取struts-config.xml配置文件,使用
ActionMapping来查找对应的Action,并把请求转发给Action类。
* 对于大型应用,Action类充当用户请求和业务逻辑处理之间的Adaptot(适配器)。
* 在Model(模型)端,业务逻辑可以采用Java Bean或者EJB来完成。
* ActionForm Bean负责传递系统状态
* 视图端主要是JSP,struts通过其内部的标签库进行了扩展。
[/size]
[/b][/color]
下面我们来举个简单的helloapp的例子。
[b]
场景: 页面端有个文本,根据用户输入的名字,返回Hello 输入名。
如果用户输入空,则提示不能为空;如果用户输入的是monstor,
则提示非法。
[/b]
我们需要建立一个jsp文件,在上面放上提示,文本框,按钮还有错误提示。
<%@ 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" />
</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>
在上面的代码中,我们使用了许多struts自己的标签。
我们来分析下:
[table]
|<html:form>||创建HTML表单,能够将表单字段和ActionForm Bean的属性关联起来。|
|<bean:message>||输出本地化文本,key属性对应文本。|
|<html:text>||创建文本框。|
|<bean:write>||输出JavaBean的属性值,property对应属性。|
|<html:errors>||显示错误信息。|
|<logic:present>||判断JavaBean在特定的范围那是否存在,存在才会去执行主体内容。|
|<html:submit>||提交按钮。|
|<html:reset>||清空按钮。|
[/table]
由于我们使用了许多消息文本,我们需要将这些文本本地化,因此
我们需要创建资源文件。
applicatoin.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!
接下来,我们需要创建ActionForm Bean。
HelloForm.java:
package mytrain.struts.helloapp;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionMessage;
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;
}
/**
* 验证属性非空,返回ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if ((userName == null) || (userName.length() < 1))
errors.add("username", new ActionMessage("hello.no.username.error"));
return errors;
}
}
[color=red][b]注:ActionErrors类是ActionMessage的子类。
从struts1.2开始废弃ActionErrors,统一使用ActionMessage来表示正常或
错误消息。[/b][/color]
接下来,我们需要创建常量类和业务类。
Constants.java:
package mytrain.struts.helloapp;
public final class Constants {
public static final String PERSON_KEY = "personbean";
}
PersonBean.java:
package mytrain.struts.helloapp;
public class PersonBean {
private String userName = null;
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
接着,我们便可以创建重点类 - Action类了。
HelloAction.java:
package mytrain.struts.helloapp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
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 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);
/*
* 基本验证在ActionForm Bean中做,
* 业务逻辑验证在Action中做.
*/
ActionMessages errors = new ActionMessages();
String userName = (String)((HelloForm) form).getUserName();
String badUserName = "Monster";
if (userName.equalsIgnoreCase(badUserName)) {
errors.add("username", new ActionMessage("hello.dont.talk.to.monster", badUserName ));
saveErrors(request, errors);
return (new ActionForward(mapping.getInput()));
}
/*
* Having received and validated the data submitted
* from the View, we now update the model
*/
PersonBean pb = new PersonBean();
pb.setUserName(userName);
pb.saveToPersistentStore();
/*
* 将数据 - JavaBean通过page, request, session, servlet context这些范围
* 传递到视图组件.
*/
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"));
}
}
最后,我们需要在我们的核心配置文件struts-config.xml中配置Action映射,
全局转向(forward),FormBean定义,资源文件定义。
struts-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<!-- Form Bean Definitions -->
<form-beans>
<form-bean name="HelloForm" type="mytrain.struts.helloapp.HelloForm"/>
</form-beans>
<global-exceptions />
<global-forwards />
<!-- Action Mapping Definitions -->
<action-mappings>
<action path = "/HelloWorld"
type = "mytrain.struts.helloapp.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="mytrain.struts.helloapp.application"/>
</struts-config>