5月25日 CMS 周五

本文介绍了一个使用Struts2框架与JSP技术相结合的示例应用,展示了如何通过Struts2处理用户请求并传递数据到JSP页面进行展示。具体包括:页面布局设计、分页查询实现、数据回显功能以及Ajax交互方式。
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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 'list.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 type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
  <link rel="stylesheet" href="css/index_work.css" type="text/css"></link>

    <script type="text/javascript">
        function mohu(){
            var mohu = $("#mohu").val();
            location = "show.action?mohu="+mohu;
        }
        function gopage(cpage){
            var mohu = $("#mohu").val();
            location = "show.action?cpage="+cpage+"&mohu="+mohu;
        }
        function upd(id){
            location = "upd.jsp?id="+id;
        }
    </script>
  </head>

  <body>
    <table>
        <tr>
            <th colspan="21"><input id="mohu" value="${mohu}"><input type="button" value="查询" onclick="mohu()">
            <input type="button" value="添加"><input type="button" value="批量删除"></th>
        </tr>

        <tr>
            <th><input type="checkbox"></th>
            <th>教师编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>爱好</th>
            <th>院系编号</th>
            <th>家庭住址</th>
            <th>教师编号</th>
            <th>简介</th>
            <th>成立时间</th>
            <th>操作</th>
        </tr>
        <c:forEach var="s" items="${list}">
            <tr>
                <th><input type="checkbox"></th>
                <th>${s.id}</th>
                <th>${s.name}</th>
                <th>${s.sex}</th>
                <th>${s.hobby}</th>
                <th>${s.did}</th>
                <th>${s.address}</th>
                <th>${s.department}</th>
                <th>${s.content}</th>
                <th>${s.datea}</th>
                <th><input type="button" value="删除">
                <input type="button" value="修改" onclick="upd(${s.id})"></th>
            </tr>
        </c:forEach>
        <tr>
            <th colspan="21">
                <button onclick="gopage(1)">首页</button>
                <button onclick="gopage(${pu.cpage<=1?1:(pu.cpage-1)})">上页</button>
                <button onclick="gopage(${pu.cpage>=pu.totalPage?pu.totalPage:(pu.cpage+1)})">下页</button>
                <button onclick="gopage(${pu.totalPage})">末页</button>
                ${pu.cpage}/${pu.totalPage}页
            </th>
        </tr>
    </table>
  </body>
</html>
package com.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.alibaba.fastjson.JSON;
import com.dto.Dept;
import com.dto.TeaWith;
import com.opensymphony.xwork2.ActionSupport;
import com.service.TeacherService;
import com.util.PageUtils;

public class Action extends ActionSupport{
    private TeacherService service = new TeacherService();

    private List<TeaWith> list = new ArrayList();
    private List<Dept> listdown = new ArrayList();
    private String mohu;
    private String cpage;
    private PageUtils pu;
    private String id;
    private Dept dept;


    public String execute(){


        if(cpage==null){
            cpage="1";
        }

        Integer pageSize = 2;

        int count = service.count(mohu);

        pu = new PageUtils(Integer.valueOf(cpage), pageSize, count);

        list = service.list(pu,mohu);

        return "tolist";
    }

    public void down() throws IOException{
        listdown = service.down();
        String jsonString = JSON.toJSONString(listdown);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(jsonString);
    }

    public void toshow() throws IOException{
        System.out.println("toshow");

        TeaWith t = service.show(id);
        System.out.println(t);
        String jsonString = JSON.toJSONString(t);
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        response.getWriter().write(jsonString);
    }

    public String getMohu() {
        return mohu;
    }

    public void setMohu(String mohu) {
        this.mohu = mohu;
    }

    public List<TeaWith> getList() {
        return list;
    }

    public void setList(List<TeaWith> list) {
        this.list = list;
    }

    public String getCpage() {
        return cpage;
    }

    public void setCpage(String cpage) {
        this.cpage = cpage;
    }

    public PageUtils getPu() {
        return pu;
    }

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }



}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'upd.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 type="text/javascript" src="js/jquery-1.8.2.min.js"></script>
    <script type="text/javascript">
        var id = ${param.id};
        //下拉列表
        $.ajax({
            url:"show!down",
            type:"post",
            data:{id:id},
            dataType:"json",
            success:function (msg){
                for(var i in msg){
                    $("select").append("<option value="+msg[i].did+">"+msg[i].content+"</option>");
                }
            }
        });

        //回显
        $.ajax({
            url:"show!toshow",
            type:"post",
            data:{id:id},
            dataType:"json",
            success:function (msg){
                $("#id").val(msg.id);
                $("#name").val(msg.name);
                $("[value='"+msg.sex+"']").attr("checked",true);

                $("[value='"+msg.did+"']").attr("checked",true);
                $("#address").val(msg.address);
            }
        });

    </script>
  </head>

  <body>
    id:<input type="text" id="id" ><br>
    name:<input type="text" id="name"><br>
    sex:<input type="radio" value="男" id="sex"><input type="radio" value="女" id="sex"><br>
    hobby:<input type="checkbox" value="绘画" id="hobby">绘画
          <input type="checkbox" value="唱歌" id="hobby">唱歌
             <input type="checkbox" value="骑马" id="hobby">骑马<br>
    did:<select>

    </select>
        <br>
    address:<input type="text" id="address"><br>
    <input type="button" value="提交">
  </body>
</html>

想的太少可能失去做人的尊严,想的太多可能失去做人的快乐。如果想不通,可以说出来,如果想通了,不必说,笑笑即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值