简体   繁体   English

JSTL Scriptlet到Java类

[英]JSTL Scriptlet to java class

I'm pretty new to Java so bear with me. 我是Java的新手,所以请多多包涵。 I have a scriptlet that is put into a JSTL tag. 我有一个放入JSTL标记中的scriptlet。 I'm trying to do away with my scriptlets and put them in a seperate class. 我试图取消我的脚本,并将它们放在单独的类中。 Except I'm really stumped on this one. 除了我真的很为难。 I have a pretty basic Java method: 我有一个非常基本的Java方法:

static final String default = "Enter a message";

String subDate(String out){
    final String year = "" + Calendar.getInstance().get(Calendar.YEAR);
    return out.replaceAll("%CURRYEAR%", year);
}

In my JSTL I call it like below 在我的JSTL中,我这样称呼它

<c:out value="<%= subDate(msg) %>" default="<%= subDate(default) %>"

When I'm converting this to my Java class this is what I have. 当我将其转换为Java类时,这就是我所拥有的。 But I'm simply not getting anything. 但是我什么也没得到。 I'm also really confused on how I would pass it multiple parameters like I'm doing in my JSTL. 我也很困惑如何像在JSTL中一样传递多个参数。

public String getsubDate(String in){        
    return in.replaceAll("%CURRYEAR%", YEAR) + getMsg();            
}

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Since it sounds like you're putting msg on the request in the servlet using request.setAttribute , you can get the correct year in the servlet, replace all occurances of %CURRYEAR% right there and just put the correct message on the request: 既然听起来您是使用request.setAttribute在servlet中的请求上添加msg ,则可以在servlet中获得正确的年份,在此替换所有出现的%CURRYEAR% ,然后在请求上放置正确的消息:

String year = "" + Calendar.getInstance().get(Calendar.YEAR);
request.setAttribute("msg", msg.replaceAll("%CURRYEAR%", year));

Then on your page just do this: 然后在您的页面上执行以下操作:

<c:out value="${msg}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM