eclipse中开发struts项目 struts原理解析

来源:互联网 发布:松下gr7软件下载 编辑:程序博客网 时间:2024/06/09 16:30

1、新建一个web项目



2、为web项目添加struts支持

新建web项目后,web项目本身还没有struts支持。

添加完成后可以看到struts的库和struts-config.xml文件




3、选择视图


此时可以看到新建中出现了struts的文件





4、新建ActionForm、Action

 




5、编写ActionForm

主要是编写validate()验证函数

public ActionErrors validate(ActionMapping mapping,  HttpServletRequest request)

此函数返回的是ActionErrors对象

ActionError对象使用的是property错误资源文件中的key值构造的,要先配置错误资源文件。

注意资源文件中不能保存中文,要先转成unicode编码

在myeclipse中可以在图形化界面中直接输入中文,工具会自动进行转换。

 

public class HellpForm extends ActionForm {

    private Stringinfo;

    public ActionErrorsvalidate(ActionMapping mapping,HttpServletRequest request) {

        ActionErrors errors = new ActionErrors();

        if(info==null &&"".equals(info))//没有info或输入为空

            errors.add("info",new ActionMessage("error.info"));//第一个参数是与表单元素名相同

        return errors;

    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {}

    public String getInfo() {

        returninfo;

    }

    public void setInfo(String info) {

        this.info = info;

    }

}

 

6、定义Action

 

public class HellpAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,

            HttpServletRequest request, HttpServletResponse response) {

        HellpForm hellpForm = (HellpForm) form;

        String info = hellpForm.getInfo();//ActionFormget取得提交的参数(javabean,封装数据)

        request.setAttribute("msg", info);//设置要在页面上显示的属性msg

        return mapping.findForward("show");//跳转,到struts-config中按name查找forward标签

    }

}

 

7、配置web.xml和struts-config

actionform和action类编写完后,需要在web.xml和struts-config中进行配置才能发挥作用。

一般通过工具创建actionform和action后会自动配置完成。但还需要配置一些元素或属性,例如跳转路径。

(1)在web.xml中

<servlet>

    <servlet-name>action</servlet-name>

    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>

</servlet>

<servlet-mapping>

  <servlet-name>action</servlet-name>

  <url-pattern>*.do</url-pattern>

</servlet-mapping>

 

所有提交路径为*.do的都会被name为action 的servlet接收,对应的类为org.apache.struts.action.ActionServlet

(2)struts-config的配置主要包括:ActionForm类,Action类,跳转路径

(a)<form-beans>:ActionForm的设定,每个ActionForm对应一个子标签<form-bean>,并给出名称name和自定义的ActionForm类名type属性。

(b)<action-mappings>:Action的设定。每个Action对应一个子标签   <action>,并给出绑定的ActionForm名称name,访问本Action的提交路径path(不加.do),本Action对应的类type以及本Action可用的跳转路径属性<forwardname="跳转名" path="跳转目标路径"/>。

当Action中执行mapping.findForward("show"),查找name为show的forward标签,然后实现到path页面的跳转。

 

<struts-config>

  <form-beans>

    <form-beanname="hellpForm"type="com.tony.struts.form.HellpForm"/>

  </form-beans>

  <global-exceptions/>

  <global-forwards/>

  <action-mappings>

  <actionname="hellpForm"path="/hellp"type="com.tony.struts.action.HellpAction"input="/MyJsp.jsp">

<forward name="show"path="/MyJsp.jsp"/>

   </action>

  </action-mappings>

  <message-resourcesparameter="com.tony.struts.ApplicationResources"/>

</struts-config>

 

8、新建带有struts标签库的jsp页面文件



可以看到此时页面已经配置了struts的标签库的前缀和uri映射。

 

<%@ taglib uri="http://struts.apache.org/tags-bean"prefix="bean"%>

<%@ taglib uri="http://struts.apache.org/tags-html"prefix="html"%>

<%@ taglib uri="http://struts.apache.org/tags-logic"prefix="logic"%>

<%@ tagliburi="http://struts.apache.org/tags-tiles"prefix="tiles"%>

 

9、编辑jsp文件代码,提交参数,输出提示信息(错误提示信息或输入的正确信息)

  <body>

    <html:errors/><!--输出ActionErrors中的信息 -->

    <logic:presentname="msg"scope="request">

        ${msg}

   </logic:present>

    <html:formaction="hellp.do"method="post">

    <html:textproperty="info"/>

    <html:submitvalue="显示"/>

    </html:form>

  </body>

 

10、总结:

ActionForm:一种JavaBean。封装表单提交的数据,传给Action。此外,可以提供验证功能,validate,并将根据验证信息设置错误信息ActionErrors或ActionMessages。

 

Action:一种Servlet,通过web.xml和struts-config的配置映射,表单提交到次Action的路径上,Action进行具体的业务操作,并根据操作结果

设置错误信息ActionErrors或ActionMessages

设置跳转目标ActionMapping对象mapping.findForward(Stringname)。跳转到<forward>中name对应的path。

 

struts-config:

配置使用的ActionForm名称name和类名type

配置Action的监听路径path和类名type

绑定的ActionForm Action名称name

错误页面input

可用的跳转路径<forward>。所有的跳转路径(各Action的特有跳转和全局跳转都是在struts-config中配置)

 

Jsp页面:使用struts标签库完成表单提交、信息显示。

 

原理图:


原创粉丝点击