项目作完了,现在没什么事情做,继续研究一下Struts2框架.今天作的是Struts2种的Ajax的实现。这里是用自带的DOJO实现的。先看配置文件,配置了Struts2和Freemarker
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Ajax</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>freemarker</servlet-name> <servlet-class> org.apache.struts2.views.freemarker.FreemarkerServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>freemarker</servlet-name> <url-pattern>*.ftl</url-pattern> </servlet-mapping> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="Struts2Ajax" extends="struts-default"> <action name="ShowArticle" class="com.zzh.action.ShowArticle"> <result type="freemarker">/result.ftl</result> </action> </package> </struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Ajax</title>
<s:head theme="ajax" />
<script type="text/javascript">
dojo.event.topic.subscribe('getArticle', this, function(data, type, e){
if(type == 'load') {
showArticle(data);
} else if(type == 'error') {
alert('Can not connect to the server');
}
});
function showArticle(data) {
document.getElementById("article").innerHTML=data;
}
</script>
</head>
<body>
<s:a theme="ajax" href="ShowArticle.action?id=1" notifyTopics="getArticle" >新聞1</s:a>
<s:a theme="ajax" href="ShowArticle.action?id=2" notifyTopics="getArticle" >新聞2</s:a>
<s:a theme="ajax" href="ShowArticle.action?id=3" notifyTopics="getArticle" >新聞3</s:a>
<s:a theme="ajax" href="ShowArticle.action?id=4" notifyTopics="getArticle" >新聞4</s:a>
<div id="article"></div>
</body>
</html>
ShowArticle.java
package com.zzh.action;
public class ShowArticle {
private Article article;
private String id;
public Article getArticle() {
return article;
}
public void setArticle(Article article) {
this.article = article;
}
public String execute()
{
article = new Article();
article.setTitle("新聞標題"+id);
article.setContent("新聞内容"+id);
return "success";
}
public void setId(String id) {
this.id = id;
}
}
Article.java
package com.zzh.action;
public class Article {
private String title;
private String content;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
作成画面一覧