1. 程式人生 > >struts2-ajax-頁面的自動重新整理

struts2-ajax-頁面的自動重新整理

不多說,直接載入原始碼:

需要的包檔案:

commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging-1.1.jar
freemarker-2.3.13.jar
log4j-1.2.17.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
struts2-dojo-plugin-2.1.8.1.jar
xwork-2.1.2.jar

Struts2Test.java原始碼:
package com.test;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class Struts2Test extends ActionSupport{
	
	private static int count=0;    //注:此處必須得加上static,否則每次執行時都會載入count=0
	
	public int getCount() {
		
		return ++count;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return "success";
	}
	
}

struts.xml原始碼:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="default" extends="struts-default" namespace="/">
	<action name="sa" class="com.test.Struts2Test" >
		<result name="success">/success.jsp</result>
	</action>
</package> 
</struts>    

web.xml原始碼:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter> 
  <filter-mapping>
  	<filter-name>struts2</filter-name>
  	<url-pattern>/*</url-pattern>
  </filter-mapping>
  
</web-app>

index.jsp原始碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib prefix="sx" uri="/struts-dojo-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
  	<!-- 下面的頭資訊不可或缺,否則程式會報錯! -->
  	<!-- 不加下面的頭資訊,錯誤資訊為:ReferenceError: djConfig is not defined -->
  	<s:head/>
  	<sx:head parseContent="true"/>
  	<!-- 上面的頭資訊不可或缺,否則程式會報錯! -->
    <base href="<%=basePath%>">
    <title>My JSP 'index.jsp' starting page</title>
  </head>
  <body>
  	<sx:div href="sa.action" updateFreq="3000" >
  		<p>你好</p>
  	</sx:div>
  	<p>你好2</p>
  </body>
</html>

success.jsp原始碼:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="sx" uri="/struts-dojo-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
  <head>
  	<s:head/>
  	<sx:head parseContent="true"/>
    <base href="<%=basePath%>">
    <title>SUCCESS</title>
  </head>
  <body>
  	<p>頁面重新整理的次數</p>
  	<s:property value="count"/>
  </body>
</html>

執行的結果:


重新整理中。。。