struts2之Action与JSP相互数据传递

package com.loaderman.crm.action;


import com.loaderman.crm.entity.User;
import com.loaderman.crm.service.UserService;
import com.loaderman.crm.service.impl.UserServiceimp;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;

public class AddUserAction extends ActionSupport implements ModelDriven<User> {
    private User user = new User();


    public String execute() throws Exception {
        System.out.println(user.toString());
        UserService studentService = new UserServiceimp();
        HttpServletRequest request1 = ServletActionContext.getRequest();
        String opr = request1.getParameter("opr");
        int n = 0;
        if (opr.equals("addUser")) {
            n = studentService.addUser(user);
        } else if (opr.equals("modifyUser")) {
            int id = Integer.parseInt(request1.getParameter("id"));
            user.setId(id);
            n = studentService.modifyUser(user);
        }
        if (n > 0) {
            return "getUserList";
        } else {
            return "addUser";
        }
    }

    @Override
    public User getModel() {
        return user;
    }
}

使用ModelDriven可以接收表单传递过来的表单数据

使用下面可接收单个参数的数据

HttpServletRequest request1 = ServletActionContext.getRequest();

String opr = request1.getParameter("opr");

下面可以实现将actio数据传递到jsp页面显示

 

package com.loaderman.crm.action;

import com.loaderman.crm.entity.Policy;
import com.loaderman.crm.service.PolicyService;
import com.loaderman.crm.service.impl.PolicyServiceimp;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.RequestAware;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


public class GetPolicyListAciton extends ActionSupport implements RequestAware {
    private Map<String, Object> request;
    public String execute() throws Exception {
        HttpServletRequest request1 = ServletActionContext.getRequest();
        System.out.println("GetPolicyListAciton");
        String name = request1.getParameter("name");
        System.out.println("name"+name);
        if (name!=null&&(!name.isEmpty())){
            System.out.println("查询部分");
            name =new String(name.getBytes("ISO8859-1"), "UTF-8");
            PolicyService stuService = new PolicyServiceimp();
            List<Policy> list = stuService.getPolicyMoreInfoByName(name);
            request.put("list", list);
        }else {
            PolicyService stuService = new PolicyServiceimp();
            List<Policy> list = stuService.getAllPolicy();
            request.put("list", list);
        }

        return "success";

    }

    @Override
    public void setRequest(Map<String, Object> request) {
        this.request = request;
    }
}

 

package com.loaderman.crm.action;

import com.loaderman.crm.entity.User;
import com.loaderman.crm.service.UserService;
import com.loaderman.crm.service.impl.UserServiceimp;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class GetUserListAction extends ActionSupport  {


    public String execute() throws Exception {
        HttpServletRequest request1 = ServletActionContext.getRequest();
        String name = request1.getParameter("name");
        System.out.println("GetUserListAction name" + name);
        if ( name != null && (! name.isEmpty())) {
//            name =new String(name.getBytes("ISO8859-1"), "UTF-8");
            UserService stuService = new UserServiceimp();
            List<User> list = stuService.getUserByName( name);
            ActionContext.getContext().getSession().put("list", list);
        } else {
            UserService stuService = new UserServiceimp();
            List<User> list = stuService.getAllUser();
            ActionContext.getContext().getSession().put("list", list);

        }
        return "success";
    }



}
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ page isELIgnored="false" %>
<%@ 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>客户信息列表</title>
    <link href="${pageContext.request.contextPath }/css/common.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript">
        function check() {
            var mymessage = confirm("确定要删除吗?");
            if (mymessage == true) {
                return true;
            }
            else if (mymessage == false) {
                return false;
            }

        }

    </script>
</head>
<body>
<div align="right">欢迎你,${account.username } &nbsp;&nbsp;&nbsp;<a href="logout.jsp">退出</a></div>
<h2>客户信息列表</h2>
<div align="right" style="margin-right: 50px">
    <form action="/getUserList" style="text-align: right" >
        <tr>
            <input type="text"  placeholder="请输入要查询姓名" name="name"></td>
        </tr>
        <td  ><input type="submit" value="查询"></td>
        </tr>
    </form>
    <p>
        <a href="/pages/addUser.jsp"><input type="button" value="添加客户"></a>
        <a href="/getPolicyList"><input type="button" value="保单列表"></a>
    </p>

</div>


<table>
    <tr>
        <td class="listTable">编号</td>
        <td class="listTable">姓名</td>
        <td class="listTable">性别</td>
        <td class="listTable">年龄</td>
        <td class="listTable">电话</td>
        <td class="listTable">等级</td>
        <td class="listTable">身份证</td>
        <td class="listTable">地区</td>
        <td class="listTable">地址</td>
        <td class="listTable">微信</td>
        <td class="listTable">QQ</td>
        <td class="listTable">邮箱</td>
        <td class="listTable">职业</td>
        <td id="profile">备注</td>
        <td id="operate">操作</td>
    </tr>

    <c:forEach items="${list}" var="user" varStatus="status"  >
        <tr>
        <td>${status.count}
        </td>
        <td>${user.getName()}
        </td>

        <td>${user.getSex()}
        </td>
        <td>${user.getAge()}
        </td>
        <td>${user.getTelephone()}
        </td>
        <td>${user.getGrade()}
        </td>
        <td>${user.getIdCard()}
        </td>
        <td>${user.getArea()}
        </td>
        <td>${user.getAddress()}
        </td>
        <td>${user.getWeixin()}
        </td>
        <td>${user.getQq()}
        </td>
        <td>${user.getEmail()}
        </td>
        <td>${user.getJob()}
        </td>
        <td>${user.getRemark()}
        </td>
        <td><a href="/pages/userDetail.jsp?id=${user.getId()}">查看&nbsp;&nbsp;</a>
            <a href="/pages/modifyUser.jsp?id=${user.getId()}">修改&nbsp;&nbsp;</a>
            <a href="${pageContext.request.contextPath }/delUser?id=${user.getId()}"
               onclick="return check()">删除&nbsp;&nbsp;</a></td>
    </tr>
    </c:forEach >



</table>

</body>
</html>

 

posted on 2019-01-16 17:41  LoaderMan  阅读(721)  评论(0编辑  收藏  举报

导航