第一个struts的web应用程序helloapp
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文件,在上面放上提示,文本框,按钮还有错误提示。 [code="html"] <%@ 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" %> <bean:message key="hello.jsp.title" />
[/code] 在上面的代码中,我们使用了许多struts自己的标签。 我们来分析下: [table] |||创建HTML表单,能够将表单字段和ActionForm Bean的属性关联起来。| |||输出本地化文本,key属性对应文本。| |||创建文本框。| |||输出JavaBean的属性值,property对应属性。| |||显示错误信息。| |||判断JavaBean在特定的范围那是否存在,存在才会去执行主体内容。| |||提交按钮。| |||清空按钮。| [/table] 由于我们使用了许多消息文本,我们需要将这些文本本地化,因此 我们需要创建资源文件。 applicatoin.properties: [code="java"] #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 UserName to say hello to! [/code] 接下来,我们需要创建ActionForm Bean。 HelloForm.java: [code="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; } } [/code] [color=red][b]注:ActionErrors类是ActionMessage的子类。 从struts1.2开始废弃ActionErrors,统一使用ActionMessage来表示正常或 错误消息。[/b][/color] 接下来,我们需要创建常量类和业务类。 Constants.java: [code="java"] package mytrain.struts.helloapp; public final class Constants { public static final String PERSON_KEY = "personbean"; } [/code] PersonBean.java: [code="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; } } [/code] 接着,我们便可以创建重点类 - Action类了。 HelloAction.java: [code="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")); } } [/code] 最后,我们需要在我们的核心配置文件struts-config.xml中配置Action映射, 全局转向(forward),FormBean定义,资源文件定义。 struts-config.xml: [code="xml"] [/code]