Struts2+Spring 整合成功测试案例----利用Dojo实现简单的Ajax的效果(一) .

来源:互联网 发布:java反射获得属性值 编辑:程序博客网 时间:2024/04/30 20:00

 

利用Dojo实现简单的Ajax的效果 .

继续使用上两个案例的代码。

一、安装Dojo插件,如下三个步骤:

    1.导入jar包:struts2-dojo-plugin-2.1.8.1.jar ,复制到WEB-INF / lib 目录下。

    2.在使用Dojo标签的Jsp页面使用如下代码导入Dojo标签:

        <%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

    3.在需要使用Dojo标签的JSP页面加入Dojo的head标签。如下代码所示:

        <head>

           ......

          <sx:head/>
      </head>

二、定义异步表单。

        1. 改写addperson.jsp 界面

       

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加界面</title>
<sx:head/>
</head>
<body>
  
   <s:form action ="add.action" method="post">
    
      <s:textfield name="per.pid" />
      <s:textfield name="per.pname" />
      <s:textfield name="per.page" />
      <s:textfield name="per.paddr" />
      <s:submit method="addui"/>
      <s:submit value="List" method="listui"/>
      <sx:submit value="List persons" method="listui" targets="show"/>
     
     
   </s:form>
   <div id="show"></div>

</body>
</html> 

         2.新增界面listperson.jsp

      

          

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
        <%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

</head>
<body>

<div id="allTypes" >
  <table border='1'>
  <th>编号</th>
  <th>姓名</th>
  <th>年龄</th>
  <th>地址</th>
  <th>动作</th>
 
  <s:iterator value="lsPer" id="alltype">
      <tr>
      <td>
       <input type="checkbox"
       lang="<s:property value="#alltype.id"/>" /></td>
       <td>
       <s:property value="#alltype.pname"/>
       </td>
       <td>
       <s:property value="#alltype.page"/>
       </td>
       <td>
       <s:property value="#alltype.paddr"/>
       </td>
       <td>
        <a href="#">编辑</a>/<a href="#">删除</a>
       </td>
       </tr>
    </s:iterator>
   
    </table>
</div>


 
 

</body>
</html>

 

       3.改写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>
     <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <constant name="struts.objectFactory" value="spring"></constant>
  
  
     <package name="person" extends="struts-default" namespace="/">
   
          <action name="add" class="personAction" method="addui">
           <!--  <result name="input">/addperson.jsp</result> -->
             <result name="success">/success.jsp</result>
             <result name="error">/addperson.jsp</result>
          </action>
         
          <action name="addlist" class="personAction" method="listui">
          <result name="success">/listperson.jsp</result>
          </action>
       
       
   </package>

</struts>   

 

         4.启动Server进行测试。

    .......................

      继续Ajax的学习...................