java + struct + ajax‏

上一篇 / 下一篇  2009-07-06 22:16:59 / 个人分类:JAVA/JSP

页面部分:example.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP ´example.jsp´ starting page</title>
<meta. http-equiv="pragma" content="no-cache">
<meta. http-equiv="cache-control" content="no-cache">
<meta. http-equiv="expires" content="0">
<meta. http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta. http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script>

var req;
var which;
function retrieveURL(url) {
if (window.XMLHttpRequest) { // Non-IE browsers
req = new XMLHttpRequest();
req.onreadystatechange = processStateChange;
try {
req.open("GET", url, true);
} catch (e) {
alert(´创建对象失败´);
}
req.send(null);
} else if (window.ActiveXObject) { // IE
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req) {
req.onreadystatechange = processStateChange;
req.open("GET", url, true);
req.send(null);
}
}
}
function processStateChange(){
if (req.readyState == 4) { // Complete
if (req.status ==200) { // OK response
document.getElementById("test").innerHTML = req.responseText;
} else {
alert("Problem: " + req.statustext);
}
}
}
</script>
</head>
<body nload="retrieveURL(´/test/example.do´)">
<dir id="test"></dir>
</body>
</html>
action部分:ExampleAction.java
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.sbt.struts.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
* MyEclipse Struts
* Creation date: 01-11-2008
*
* XDoclet definition:
* @struts.action validate="true"
*/
public class ExampleAction extends Action {
/*
* Generated Methods
*/

/**
* Method execute
* @param mapping
* @param form.
* @param request
* @param response
* @return ActionForward
* @throws IOException
*/
public ActionForward execute(ActionMapping mapping, ActionForm. form,
HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html;charset=gbk");
PrintWriter ut = response.getWriter();
out.println("hello world!");
out.flush();
return null;
}
}
config部分:struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>
<data-sources />
<form-beans />
<global-exceptions />
<global-forwards />
<action-mappings >
<action path="/example" type="com.sbt.struts.action.ExampleAction" />

</action-mappings>

<message-resources parameter="com.sbt.struts.ApplicationResources" />
</struts-config>

web部分:web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>


TAG:

 

评分:0

我来说两句

日历

« 2024-04-25  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 30708
  • 日志数: 37
  • 建立时间: 2008-07-09
  • 更新时间: 2009-07-23

RSS订阅

Open Toolbar