Struts2 change the default resulttype

Source: Internet
Author: User

Recently, I encountered a small requirement. I want to set the resultType returned by some actions to freemarker by default. I first wanted to configure the global result on the action, and then use an expression for the name, and then type = "freemarker ", however, after the experiment, we found that the configuration was not successful because it was instantiated when struts2 was started, and then the expression could not be used in the action, and an exception was reported. so I tracked the source code and found the relevant code [java] public String getFullDefaultResultType () {if (defaultResultType = null) IN THE PackageConfig class )&&! Parents. isEmpty () {for (PackageConfig parent: parents) {String parentDefault = parent. getFullDefaultResultType (); if (parentDefault! = Null) {return parentDefault ;}} return defaultResultType;} This is a recursive query. If this package is not configured, the parent package is searched and the default-package is found, this parameter is specified as dispatcher. okay, the code is found. How can I change it, you only need to re-declare the result-type [html] <result-type name = "freemarker" class = "org. apache. struts2.views. freemarker. freemarkerResult "default =" true "/> and set it to default. When this package is parsed, it is set to the default result-type. the related code is in com. opensymphony. xwork2.config. providers. xmlequalatio In the addResultTypes method of the nProvider class. [java] protected void addResultTypes (PackageConfig. builder packageContext, Element element) {NodeList resultTypeList = element. getElementsByTagName ("result-type"); for (int I = 0; I <resultTypeList. getLength (); I ++) {Element resultTypeElement = (Element) resultTypeList. item (I); String name = resultTypeElement. getAttribute ("name"); String className = resultTypeElemen T. getAttribute ("class"); String def = resultTypeElement. getAttribute ("default"); Location loc = DomHelper. getLocationObject (resultTypeElement); Class clazz = verifyResultType (className, loc); if (clazz! = Null) {String paramName = null; try {paramName = (String) clazz. getField ("DEFAULT_PARAM "). get (null);} catch (Throwable t) {// if we get here, the result type doesn't have a default param defined .} resultTypeConfig. builder resultType = new ResultTypeConfig. builder (name, className ). defaultResultParam (paramName ). location (DomHelper. getLocationObject (resultTypeElement); Map <String, String> para MS = XmlHelper. getParams (resultTypeElement); if (! Params. isEmpty () {resultType. addParams (params);} packageContext. addResultTypeConfig (resultType. build (); // set the default result type if ("true ". equals (def) {packageContext. defaultResultType (name) ;}}} this code is used to parse the struts2 configuration file and obtain the result-type element. Then, parse the related elements. If the default value is set to true, the result-type is set to the default value.

Related Article

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.