SSH Framework Online Mall Project The Struts2 of the 24th War on how to handle multiple model requests _java

Source: Internet
Author: User

1. The question was raised

If you implement the Modeldriven<model> interface in Struts2, you can inject the parameters into the model, you can use the model in the action. But what if there are two model now that needs to be used in the same action? For example, in the last section we completed the online payment function, but the payment is not finished, we need to receive feedback from the third side of the information, such as the successful payment, we need to send the payer email and text messages. So we also need to get the parameters from the third party in the payaction, because the parameters passed from the third party are different from the parameters that we passed, so we have to write a model (Backdata) to receive those parameters, so the problem is, Our payaction has been written in this way: public class Payaction extends Baseaction<senddata>, which is already implemented in Baseaction modeldriven< Senddata> interface, then how to receive a model in an action, and still have to handle them differently?
There is a solution (in fact, it cannot be called a solution ...). Because there is no solution ... is to write a model, and then let SendData and backdata inherit it, but the problem is that these two model does not matter, why to inherit the same model, so this solution is actually in the escape from the above problem.
In Springmvc (Springmvc has not really begun to learn, if there is a mistake, please correct me!) To solve this problem well, because each method in SPRINGMVC corresponds to a model, instead of each action corresponding to a model, which is convenient, I write two methods in the same action, different methods to deal with different model.

2. Resolution of the problem

For this issue, STRUTS2 also provides a workaround:
Struts2 Stores a number of maps in Actioncontext, such as the previous request, session, application, etc. There is also a parametermap, which stores all request parameters in the map, so long as our action implements the Parameteraware interface, we can get this parametermap, This is the same as Modeldriven, if we implement the Modeldriven<model> interface, then we can get the model in action, that is, define a model and implement the Set method.
OK, so now the problem is well done, the parameters of the payment and return parameters are different, that is, two times into the payacition of the parameters are different, that is, two times parametermap loaded data is not the same, So long as we select a parameter in the action (this parameter can be divided two times is different request can be) as a judge, we know which model is currently used to receive parameters (SendData or backdata). Now let's rewrite the code in Payaction:

@Controller ("Payaction") @Scope ("prototype") public class Payaction extends baseaction<object> implements Parameteraware {//Note that the above inherited baseaction cannot be written in SendData, write object, and so we then judge the specific use of the//define a MAP to receive request parameters private map<
 String, string[]> parameters;

 @Override public void Setparameters (map<string, string[]> parameters) {this.parameters = parameters; }/* In Struts-default.xml, the ServletConfig interceptor executes before the Modeldriven, so we have the request parameter when we inject model, so that we can be in Getmodel ()
  Method through the parameter to determine which request/@Override public Object Getmodel () {//payment of the time there is a payment channel encoded parameters (PD_FRPID), returned without//so that we can judge by this parameter is paid or returned
  if (Parameters.get ("pd_frpid")!= null) {model = new SendData ();
  else {model = new backdata ();
 return model;
  }//Changyibao send data method public String GoBank () {//correspond to sent model:senddata senddata SendData = (senddata) model;

 The logic to handle sending data has been implemented in the previous section ...}
  The method that receives the returned data is public void Backbank () {//corresponds to the received model:backdata backdata backdata = (backdata) model; Handling the logic of returning data ...
To be implemented later,  First speak STRUTS2 handle multiple model requests this knowledge point}}

 

3. STRUTS2 Process

Let's analyze the implementation process of STRUTS2, which is more conducive to understanding the above principles. Struts processing process:

1, get the request, first create an action agent, in the creation of the agent by the way to create the action;
2), execute 18 Interceptor, the interceptor execution succeeds then calls the action method;
3, the action method completes, then calls 18 interceptors
So according to this process, we know: Create Action–> and Execute interceptors first (execute ServletConfig, then execute Modeldriven, because ServletConfig interceptors are in front of Modeldriven). So in the code above, we can use the Getmodel () method to get the data in the Parametermap to judge.
Use the following simple sequence diagram to visually express the above processing flow bar:

This is very intuitive to see Struts2 processing process, so for the above processing multiple model request also very good understanding. Here, Struts2 processing more than one model request part of the method has been analyzed, the following for the project in a small logic, to do a little better.

4. Improve the way to receive data

The above left a logical implementation, that is, processing the returned data, here the logic is mainly: Update order status (has been paid, shipped, etc.), send mail, send SMS and so on. We will first update the status of the order completed, the subject of the mail and send text message function, we write later.
First Perfect Backbank () method:

public void Backbank () {
 backdata backdata = (backdata) model;
 SYSTEM.OUT.PRINTLN (model);
 Boolean IsOK = Payservice.checkbackdata (backdata);
 if (IsOK) {
  //1. Update order status, parameters are passed in according to the situation in the database, used to test the
  Forderservice.updatestatusbyid (integer.valueof (201605006) , 2);
  2. Send a message//3 according to the user's mailbox address
  . Send SMS
  System.out.println ("----success!! ----");
 } else {
  System.out.println ("----false!!! ----");
 }
}

We then complete the Checkbackdata (backdata) method in Payservice (Logic and basic in section 21):

@Service ("Payservice") public class Payserviceimpl implements Payservice {//omit unrelated code/******************************
   The face is the method that sends the request **************************************///completes the appended private String Joinbackdataparam (Backdata backdata) of the returned data {
   Append string, prepare for cryptographic verification stringbuffer InfoBuffer = new StringBuffer ();
   Infobuffer.append (Backdata.getp1_merid ());
   Infobuffer.append (Backdata.getr0_cmd ());
   Infobuffer.append (Backdata.getr1_code ());
   Infobuffer.append (Backdata.getr2_trxid ());
   Infobuffer.append (Backdata.getr3_amt ());
   Infobuffer.append (Backdata.getr4_cur ());
   Infobuffer.append (Backdata.getr5_pid ());
   Infobuffer.append (Backdata.getr6_order ());
   Infobuffer.append (Backdata.getr7_uid ());
   Infobuffer.append (BACKDATA.GETR8_MP ());
   Infobuffer.append (Backdata.getr9_btype ());
  return infobuffer.tostring (); //The returned data is encrypted and compared to the transmitted ciphertext, if OK indicates that the data has not been tampered with public boolean checkbackdata (Backdata backdata) {String joinparam=th
   Is.joinbackdataparam (Backdata); AddAfter the secret to get their own ciphertext String MD5 = digestutil.hmacsign (joinparam.tostring (), key);
  Cipher text and pass over ciphertext comparison return md5.equals (Backdata.gethmac ());

 } 
}

Finally we complete the Updatestatusbyid method in Forderservice:

Forderservice interface Public
interface Forderservice extends baseservice<forder> {
 //omit other extraneous code
 ... Update order status public
 void Updatestatusbyid (int id, int sid) based on the order number;
}

Forderserviceimpl Implementation class
@Service ("Forderservice") public
class Forderserviceimpl extends Baseserviceimpl <Forder> implements Forderservice {

 //omit other extraneous code

 @Override public
 void Updatestatusbyid (int id, int SID) {
  String hql = "Update Forder f set f.status.id=:sid where F.id=:id";
  GetSession (). CreateQuery (HQL).
   Setinteger ("Sid", Sid)
   . Setinteger ("id", id)
   . executeupdate ();
 } 


This will make it possible to update the order status after the customer has paid.

Original link: http://blog.csdn.net/eson_15/article/details/51465067

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.