局部动态加载页面

在web项目中为了减少服务器压力经常会用到异步刷新部分页面
这里写图片描述
如图所示,在点击左侧的li标签或者按钮,右边动态的显示对应的内容而不刷新整个页面。
今天用SSH+jquery来实现此功能

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript"
    src=" ${ pageContext.request.contextPath }/script/jquery/jquery-1.9.js"></script>
<script type="text/javascript"
    src="${ pageContext.request.contextPath }/script/js/my.js"></script>
<style type="text/css">
.left {
    width: 100px;
    float: left;
    border: red 1px solid;
    height: 500px;
}

.right {
    float: left;
    border: red 1px solid;
    height: 500px;
    width: 500px;
}

.content {
    width: 1000px;
    height: 600px;
}
</style>
</head>


<body>
    <div class="content">
        <div class="left">
            <ul>
                测试
                <li id="test">test1</li>
                <li>test2</li>
                <li>test3</li>
            </ul>
        </div>
        <div class="right" id="right"></div>
    </div>
</body>
</html>

界面:
这里写图片描述
现在要实现的就是点击左侧的test1,在右侧异步显示对应的页面

action:

package com.dust.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.dust.entity.Teacher;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class TeacherAction extends ActionSupport {

    public String show(){
        System.out.println("run...");
        Teacher t = new Teacher();
        t.setName("123");
        t.setGender("男");
        t.setId(10L);
        ActionContext.getContext().getValueStack().push(t);
        return "show";
    }
}

struts.xml

<package name="default" namespace="/" extends="struts-default">

        <action name="tea_*" method="{1}" class="teacherAction">
            <result name="show">/right.jsp</result>
        </action>

    </package>

right.jsp

<%@ page language="java" import="java.util.*" 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 'right.jsp' starting page</title>

<style type="text/css">
</style>
</head>

<body>

    <table>
        <thead>
            <tr>
                <td>&nbsp;id</td>
                <td>&nbsp;name</td>
                <td>&nbsp;desc</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><s:property value="id" /></td>
                <td><s:property value="name" /></td>
                <td><s:property value="gender" /></td>
            </tr>
        </tbody>
    </table>

</body>
</html>

哦差点忘了,还有一个Teacher的实体类

public class Teacher {
    private Long id;
    private String name;
    private String gender;

省略setter、getter方法

js

$(function(){
    $("#test").click(function(){
        $.ajax({
            url:"tea_show.do",
            dataType:"html",
            type:"get",
            success:function(data){
                $("#right").html(data);
            }
        });
    });
});

测试:
点击test1右侧显示:
这里写图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值