struts入门中的几个问题的解决方法

来源:互联网 发布:自动化模拟仿真软件 编辑:程序博客网 时间:2024/06/06 09:37

1、如何对FORM命名,以便在JAVASCRIPT中调用? 

<html:javascript formName="logonForm"
        dynamicJavascript="true"
         staticJavascript="false"/>

2、如何在一个Action.java文件中处理多个Action事件?

第一步,在struts-config.xml中的Action中设置parameter,如:

   <!-- user login action -->
    <action    path="/logon"
               type="lh.jv.system.action.UserAction"
               name="logonForm"
               scope="session"
               parameter="LOGIN"
              >
      <forward name="0"              path="/main.jsp"/>
      <forward name="error"          path="/index.jsp"/>
    </action>

第二步,在Action.java中进行的execute中进行处理,如:

 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  ActionForward myforward = null;
  String methodName = mapping.getParameter();  //获取参数

  //根据参数不同调用不同的

  if ("ADD".equalsIgnoreCase(methodName)) {  
   myforward = ADD(mapping, form, request, response);
  }
  if ("LOGIN".equalsIgnoreCase(methodName)) {
   
   myforward = LOGIN(mapping, form, request, response);
  }  
  return myforward;   
 }

即可;

3、如何在Action.java中调用资源文件?

A、调用默认资源文件

  MessageResources messageresources = getResources(request);
  String topmenutitle=messageresources.getMessage("mac.error.macisnull");

  //前面与下面括号中为资源文件中的关键字(这都是取默认的资源文件)

 errors.add(ActionErrors.GLOBAL_ERROR,new  ActionError("application.error1")); 

B、调用非默认资源文件(待完善)

原创粉丝点击