Struts---Action篇

DispatchAction:
1. Create an action handler class that subclasses DispatchAction:

Examples:
 public class UserRegistrationMultiAction extends DispatchAction {
  ...
  }

2. Create a method to represent each logical related action:

Examples:
 public ActionForward processPage1(
 ActionMapping mapping,
 ActionForm form,
 HttpServletRequest request,
 HttpServletResponse response)throws Exception {
  ...
  }
 public ActionForward processPage2(
 ActionMapping mapping,
 ActionForm form,
 HttpServletRequest request,
 HttpServletResponse response)throws Exception {
  ...
  }

3. Create an action mapping for this action handler using the parameter attribute to specify the request
parameter that carries the name of the method you want to invoke:

Examples:
 <action path="/userRegistrationMultiPage1"
  type="strutsTutorial.UserRegistrationMultiAction"
  name="userRegistrationForm"
  attribute="user"
  parameter="action"
  input="/userRegistrationPage1.jsp">
  ...
 </action>

4. Pass the action a request parameter that refers to the method you want to invoke.
 The userRegistrationPage1.jsp contains this hidden parameter:
 <html:hidden property="action" value="processPage1"/>

LookupDispatchAction:
1. Implement the method you'll use later....

Examples:
 public class UserRegistrationAction extends LookupDispatchAction {
 //methods:
 public ActionForward remove(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response) throws Exception {
   .......................
   }
 public ActionForward save(ActionMapping mapping,ActionForm form,
   HttpServletRequest request,
   HttpServletResponse response) throws Exception {
   .......................
   }
 } 
 

2. Implement the getKeyMethodMap() method to map the resource keys to method names:
 
Examples:
 protected Map getKeyMethodMap() {
  Map map = new HashMap();
  map.put("userRegistration.removeButton", "remove");
  map.put("userRegistration.saveButton", "save");
  return map;
  }

3. Create an action mapping for this action handler using the parameter attribute to specify the request parameter that

carries the name of the method you want invoke...

Examples:
 <action path="/userRegistration"
  type="strutsTutorial.UserRegistrationAction"
  name="userRegistrationForm"
  attribute="user"
  input="/userRegistration.jsp"
  parameter="action">
  ...
  <forward name="success" path="/regSuccess.jsp" />
  <forward name="failure" path="/regFailure.jsp" />
 </action>
4. Set up the message in the resource bundle for values and the labels of the buttons…
Examples:
 userRegistration.removeButton=Remove
 userRegistration.saveButton=Save

5. Use the bean message to display the labels of the buttons and associate the buttons with the name action…
Examples:
    <%@ taglib uri="/tags/struts-html" prefix="html"%>
  <%@ taglib uri="/tags/struts-bean" prefix="bean"%>
  ...
  <html: submit property="action">
  <bean: message key="userRegistration.removeButton"/>
  </html: submit>
  ...
  <html: submit property="action">
  <bean: message key="userRegistration.saveButton"/>
  </html: submit>
  ...

The final result is that when the user clicks the Remove button in the HTML form, the remove () method is called in the

action. When the user clicks the Save button, the save () method is called in the action.

Summary:
 The difference between DispatchAction and LookupDispatchAcion is DispatchAction always request-orientated,it is used

in multipage proceed,but LookupDispatchAction always event-orientated,it is used when action handler depends on different

buttons...

 

 Uploading files with action:
To enable the user to upload files…set the encoding type of html:form to multipart/form-data and use the html:file as

follows:
 <html: form action="/UserUpdate"
  method="post"
  enctype="multipart/form-data">
  <html: file property="userImage"/>
  <br />
  ...

The property userImage in the form is a type of FormFile…FormFile represents the file type sent by client…FormFile as a

method called getInputStream ()…which return an InputStream… 
Examples:
  public ActionForward execute (ActionMapping mapping,
  ActionForm form,
  HttpServletRequest request,
  HttpServletResponse response) throws Exception {
  UserForm userForm = (UserForm) form;
  InputStream inputStream =userForm.getUserImage ().getInputStream ();
  // Do something with the inputStream, it is the file data.
  ...
  }

To set up the Struts application to restrict the size and the directory location, you could get a controller element…

<controller maxFileSize="200K", tempDir="/temp/struts/user/uploads/"/>
The file will locate web container/temp/struts/user/uploads/…

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值