freemarker+struts2 怎么获取从一个FTL传到另一个FTL的值,也就是怎么获取页面之间传的值,在页面上显示

freemarker+struts2 怎么获取从一个FTL传到另一个FTL的值,也就是怎么获取页面之间传的值,在页面上显示,第1张

:springmvc的配置文件中对freemarker配置如下: 是这样方式: ${Session["user"]} ~如果你认可我的回答,请及时点击采纳为满意回答按钮 ~~手机提问的朋友在客户端右上角评价点满意即可。 ~你的采纳是我前进的动力 ~~O(∩_∩)O,记得好评和

/

      将解析之后的文件内容返回字符串

      @param name 模板文件名

      @param root 数据Map

      @return

     /

    public static String printString(String name,Map<String,Object> root) {

        StringWriter out = new StringWriter();

        try {

            //通过一个文件输出流,就可以写到相应的文件中

            Template temp = getTemplate(name);

            tempprocess(root, out);

        } catch (IOException e) {

            eprintStackTrace();

        } catch (TemplateException e) {

            eprintStackTrace();

        } finally {

            try {

                if(out!=null) outclose();

            } catch (IOException e) {

                eprintStackTrace();

            }

        }

        return outtoString();

    }

byte b[] = strgetBytes();

在项目应用中,遇到这样一个问题,当文本过长时,需要将前面的文本省略一部分,用…代替,而使用css只能在文本最后加…

我们可以通过freemarker自定义指令的方式实现上述功能。

freemarker自定义指令需要继承TemplateDirectiveModel接口,

Java代码 收藏代码

package comnexusyfreemarkerdirective;

import javaioIOException;

import javautilMap;

import freemarkercoreEnvironment;

import freemarkertemplateSimpleScalar;

import freemarkertemplateTemplateDirectiveBody;

import freemarkertemplateTemplateDirectiveModel;

import freemarkertemplateTemplateException;

import freemarkertemplateTemplateModel;

public class EllipsisDirective implements TemplateDirectiveModel {

@Override

public void execute(Environment env, @SuppressWarnings("rawtypes") Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)

throws TemplateException, IOException {

String text = "";

int length = 0;

if(paramsget("text") != null){

text = ((SimpleScalar) paramsget("text"))getAsString();

}

if(paramsget("length") != null){

length = IntegervalueOf(((SimpleScalar) paramsget("length"))getAsString());

}

if(length < textlength()){

text = "" + textsubstring(textlength() - length);

}

envgetOut()write(text);

}

}

然后在springmvc配置文件中配置该指令

Xml代码 收藏代码

<bean id="ellipsis" class="comnexusyfreemarkerdirectiveEllipsisDirective" />

<bean id="freemarkerConfig"

class="orgspringframeworkwebservletviewfreemarkerFreeMarkerConfigurer">

<property name="templateLoaderPath" value="/" />

<property name="freemarkerSettings">

<props>

<prop key="datetime_format">yyyy-MM-dd</prop>

<prop key="number_format">0##</prop>

<prop key="url_escaping_charset">UTF-8</prop>

<prop key="output_encoding">UTF-8</prop>

<prop key="template_exception_handler">ignore</prop>

</props>

</property>

<property name="freemarkerVariables">

<map>

<entry key="xml_escape" value-ref="fmXmlEscape" />

<entry key="ellipsis" value-ref="ellipsis" />

</map>

</property>

<property name="defaultEncoding" value="UTF-8" />

</bean>

在模版中使用

Html代码

<@ellipsis text="1234567" length="6"></@ellipsis>

以上就是关于freemarker+struts2 怎么获取从一个FTL传到另一个FTL的值,也就是怎么获取页面之间传的值,在页面上显示全部的内容,包括:freemarker+struts2 怎么获取从一个FTL传到另一个FTL的值,也就是怎么获取页面之间传的值,在页面上显示、freemarker、SpringMvc 整合Freemarker后,ftl页面怎么获取httpSession等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9409935.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存