mvc 解析html,Spring MVC和解析HTML

@Controller

public class SiteEntryController

{

@RequestMapping(value="/index.htm")

public ModelAndView handleIndex(

@RequestParam(value="enableStats", required=false) String enableStats)

{

ModelMap map = new ModelMap();

...........

return new ModelAndView("index", map);

}

}

和ViewResolver的定义为如下:

p:prefix="/WEB-INF/jsp/" p:suffix=".jsp"/>

我需要做的是,要读取/解析仅具有html标记的index.jsp的内容,并返回有关内容中使用的标记的一些信息。 我将寻找如何查看我所收集到的信息,但首先,我无法弄清楚如何访问内容。 我熟悉Java,但全新的春。所以我的问题会很愚蠢。 :) 只是为了您的信息,我无法弄清楚是不是如何解析HTML,它是关于如何获得文件内容。

我做这个东西跟正常的文件操作,如下:

public class Main {

private static HTMLEditorKit.ParserCallback callback;

/**

* @param args

*/

public static void main(String[] args) {

try {

Reader r = new FileReader("D:/WS/TestP/resource/index.htm");

ParserDelegator parser = new ParserDelegator();

callback = new Detector();

parser.parse(r, callback, false);

} catch (Exception _ex) {

_ex.printStackTrace();

}

HashMap map = ((Detector) callback).getMap();

Set keys = map.keySet();

Iterator it = keys.iterator();

String key;

ArrayList list = new ArrayList();

TagFrequency tf;

int i = 0, j = 0;

while (it.hasNext()) {

key = it.next();

i = map.get(key);

tf = new TagFrequency(key, i);

if (list.size() == 0)

list.add(tf);

else {

j = 0;

while (j < list.size() && tf.compareTo(list.get(j)) > 0) {

j++;

}

if (j==list.size())

list.add(tf);

else {

list.add(j, tf);

}

}

}

for (int ind = list.size(); ind>0 ; ind--) {

System.out.println(list.get(ind-1).toString());

}

}

}

Detector.java:

public class Detector extends HTMLEditorKit.ParserCallback {

private HashMap map = new HashMap();

public Detector() {

}

@Override

public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {

String str = t.toString();

if (map.get(str) == null) {

map.put(str, Integer.valueOf(1));

} else {

map.put(str, map.get(str) + 1);

}

}

public HashMap getMap() {

return map;

}

}

和TagFrequency.java:

public class TagFrequency {

private String tag;

private Integer i;

public TagFrequency(String tag, Integer i) {

super();

this.tag = tag;

this.i = i;

}

public String getTag() {

return tag;

}

public void setTag(String tag) {

this.tag = tag;

}

public Integer getI() {

return i;

}

public void setI(Integer i) {

this.i = i;

}

public String toString() {

return this.tag + " " + this.i;

}

public int compareTo(TagFrequency tf) {

if (tf.i > this.i)

return -1;

else if (tf.i < this.i)

return 1;

else

return (-1)*this.tag.compareToIgnoreCase(tf.tag);

}

}

+0

如果它的任何安慰,它的琐碎,在PHP做。 :) –

2009-09-02 14:02:08

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值