一个简单Spring+Hibernate+Struts2.0+Ajax整合获取客户端IP、URL和请求时间

    Spring+Hibernate+Struts2.0+Ajax整合中对于初学者,有一个困惑的地方就是如何在jsp页面传递变量参数给Action,本文将提供一个获取客户端IP、URL和请求时间并持久化的简单的例子给大家参考....

一、提交jsp

      <%@ page language="java" import="java.util.*,java.text.DateFormat" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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 'success.jsp' starting page</title>
    <s:head theme="ajax" debug="true"/>
    <meta http-equiv="Content-Type" content="text/html; charset="UTF-8" />   
 <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">
 -->
  </head>
 
  <body>
    <%
       Date now = new Date();
       String Time = DateFormat.getDateTimeInstance().format(now);
       Time =  java.net.URLDecoder.decode(Time,"UTF-8");
    %>
       <img id = "indicator" src = "<s:url value = "704.jpg"/>" alt = "正在加载..." style = "display:none"/>
    <div id = 'msg'></div>
    <s:url id = "sendMessage" action = "/sendMessage.action">
    <s:param name="time"><%=Time%></s:param>
    </s:url>   
    <s:a theme = "ajax" href = "%{sendMessage}" targets = "msg">记录客户端每次访问时间、访问的URL、客户端IP</s:a>
  </body>
</html>

二、Struts.xml配置

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

<struts>
    <include file="struts-default.xml"/>
    <!-- 表示把struts2交给spring管理 -->
 <constant name="struts.objectFactory.spring.autoWrite" value="true"/>
 <constant name="struts.codebehind.pathPrefix" value="/WEB-INF/web/" />      
 <package name="default" extends="struts-default">
  <interceptors>
    <interceptor name = "count" class="countInterceptor"/>
  </interceptors>
  <default-interceptor-ref name="paramsPrepareParamsStack" />
        <action name="sendMessage" class="sendMessage">        
            <interceptor-ref name="count"/>
           <result name="success"  type="chain">login</result>
           <result name="login">/validate-fail.jsp</result>
        </action> 
        <action name="login" class="login">         
           <result>/success.jsp</result>
           <result name="input">/validate-fail.jsp</result>
        </action> 
 </package>   
</struts>
三、applicationContext.xml配置

   <?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">


 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation"
   value="classpath:hibernate.cfg.xml">
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     org.hibernate.dialect.SQLServerDialect
    </prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.connection.useUnicode">true</prop>
    
   </props>
  </property>
  <property name="namingStrategy">
   <bean class="org.hibernate.cfg.ImprovedNamingStrategy" />   
  </property>
 </bean>
 <!--  事务管理  -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 <!--  DAOBean定义  -->
 <bean id="ActionCountDAO" class="test.model.ActionCountDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="MdbtestDAO" class="test.model2.MdbtestDAO">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <!--  ActionBean定义  -->
 <bean id="sendMessage" class="test.action.SendMessage">  
 </bean>
 <bean id="login" class="test.action.LoginAction">
    <property name="actionCountDAO">
   <ref bean="ActionCountDAO" />
  </property>
  <property name="mdbtestDAO">
   <ref bean="MdbtestDAO" />
  </property>
 </bean>
 <bean id="countInterceptor" class="test.interceptor.CountInterceptor">
     <property name="actionCountDAO">
   <ref bean="ActionCountDAO" />
  </property>
 </bean>
</beans>

四、关于Action和Interceptor详见我的源代码.....

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值