小编har*_*ode的帖子

Velocity不解析foreach循环中的变量

我有非常简单的速度模板:

<html>
    <head>
        <title>Velocity template</title>
    </head>
    <body>      
        #foreach($p in $products)
            $p.name
        #end        
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

以及处理它的代码:

VelocityEngine engine = new VelocityEngine();
engine.init();
Template t = engine.getTemplate("./src/com/irbis/dms/velocity/template.html");
VelocityContext ctx = new VelocityContext();

Product p1 = new Product("fridge");
Product p2 = new Product("sofa");
Product p3 = new Product("table");
Product p4 = new Product("chair");

List<Product> products = new ArrayList<Product>();
products.add(p1);
products.add(p2);
products.add(p3);
products.add(p4);

ctx.put("products", products);
...

class Product implements Serializable {

    private String name;

    public Product() {
    }

    public Product(String name) {
        super();
        this.name …
Run Code Online (Sandbox Code Playgroud)

java foreach velocity

5
推荐指数
1
解决办法
1488
查看次数

Apache tika:删除结果字符串中的额外换行符

我有html文件:

<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;">
<div>Test message.</div>
<div>&nbsp;</div>
<div>More content here...</div>
<div>&nbsp;</div>
<div>Best regards,</div>
<div>Mr. Crowley</div></div></body></html>
Run Code Online (Sandbox Code Playgroud)

我尝试使用Apache Tika获取上面文件的内容......

final InputStream input = new FileInputStream("file.html");
final ContentHandler handler = new BodyContentHandler();
final Metadata metadata = new Metadata();

final HtmlParser htmlParser = new HtmlParser();
htmlParser.parse(input, handler, metadata, new ParseContext());
String plainText = handler.toString();
System.out.println(plainText);
Run Code Online (Sandbox Code Playgroud)

...除了额外的换行符外,一切都很好:

Test message.

 

More content here...

 

Best regards,

Mr. Crowley
<and 3 empty lines here>
Run Code Online (Sandbox Code Playgroud)

有可能避免这种行为吗?是否有可能获得更多预期结果:

Test message.
 
More content here...
 
Best regards,
Mr. Crowley
Run Code Online (Sandbox Code Playgroud)

?

代码结构如

plainText …
Run Code Online (Sandbox Code Playgroud)

java apache-tika

4
推荐指数
1
解决办法
2137
查看次数

按字符串中的字符查找单词

是否可以使用java通过使用java API或正则表达式的字符串中的字符来获取单词?

例如我有字符串:

`A broken friendship may be soldered, but will never be sound.` 
Run Code Online (Sandbox Code Playgroud)

并希望得到包括所有单词so(焊接和音),并取代它们 <b>soldered</b>和<b>sound</b>

谢谢!

java regex

-1
推荐指数
1
解决办法
79
查看次数

标签 统计

java ×3

apache-tika ×1

foreach ×1

regex ×1

velocity ×1