In-depth struts2 (1)

Source: Internet
Author: User

In-depth struts2 (1)
In fact, to learn about struts and other frameworks, we should not only know how to use them, but also look at the source code of the framework to know why it can be used in this way. Everything knows why and learns with this attitude, we can better understand the principles and implementation methods. This type of blog is mainly a summary of personal learning. If you have any shortcomings, please give me more advice so that I can make better progress .!!!! 1. Obtain the reqesut and response object Methods Map request = (Map) ActionContext. getContext (). get ("request"); HttpServletRequest request = ServletActionContext. getRequest (); implement the ServletRequestAware interface to inject the Request object <span style = "color: #000000;"> public class BaseAction <T> extends ActionSupport implements ModelDriven <T>, servletRequestAware {protected HttpServletRequest request @ Override public void setServletRequest (Http ServletRequest arg0) {this. request = arg0 ;}}</span> 2. Use the ModelDriven interface to pass the value in the front and back. The Code is as follows: public class BaseAction <T> extends ActionSupport implements ModelDriven <T> {protected T model; @ Override public T getModel () {return model ;}} if the name of the input tag on the page exactly corresponds to the attribute in the javabean, the background can inject the value into the object. I know how to use it, but we should also know how it works !!! When ModelDriven is implemented, You can inject parameters. Mainly, the struts Framework contains ModelDrivenInterceptor. Open the source code and you can see the interceptor and implementation code of ModelDriven in the source code. Continue to read the source code of struts, in the following source code, the struts interceptor puts parameters in the value stack. In this way, you can inject parameters. Think about the value transfer method on the frontend and backend, so it is not difficult to understand his principle !!! Public class ModelDrivenInterceptor extends actinterceptor {@ Override public String intercept (ActionInvocation invocation) throws Exception {Object action = invocation. getAction (); if (action instanceof ModelDriven) {ModelDriven modelDriven = (ModelDriven) action; <span style = "color: #000000;"> ValueStack stack = invocation. getStack (); Object model = modelDriven. getModel (); if (model! = Null) {stack. push (model) ;}</span> if (refreshModelBeforeResult) {invocation. addPreResultListener (new RefreshModelBeforeResult (modelDriven, model) ;}} return invocation. invoke () ;}} 3. In the struts framework, the PreResultListener API contains before, after, and PreResultListener interceptors. After interceptor refers to the code defined in the interceptor, which exists in invocation. before the invoke () code is executed, it is executed sequentially. after interception refers to the code defined in the interceptor, which exists in invocation. after the invoke () code is executed, sometimes before interception and after interception cannot meet our needs, because after the Action is executed, you have not performed any operations when you return to the view. This allows you to register in the Action method and use ActionInvocation. addPreResulteListener (). The Code is as follows: ActionInvocation actionInvocation = ActionContext. getContext (). getActionInvocation (); actionInvocation. addPreResultListener (new PreResultListener () {public void beforeResult (ActionInvocation action, String resultCode) {// TO-DO}

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.