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>

struts2之Action与JSP相互数据传递的更多相关文章

  1. Liferay7 BPM门户开发之40: Form表单的Action到Render的数据传递

    在Form提交后的变量,很多情况是要展现在jsp页面中,这时Action到Render的变量传递就非常有用. 例如,您在数据库中添加了学生的详细信息. 为了实现这一需求,先创建Form表单(学生的细节 ...

  2. JSP实现数据传递与保存

    业务逻辑: 1.登陆login.jsp 2.判断登陆是否成功check.jsp 3.登陆成功页面newsDetail.jsp 4.登陆失败转发到login.jsp 代码如下: <%@ page ...

  3. Struts2 后台action接收 jsp页面中checkbox中的值

    如前端页面jsp中的标签为: <form action="myurl"> <input type="checkbox" name=" ...

  4. JSP实现数据传递(web基础学习笔记三)

    get和post的区别: JSP内置对象: 1)out内置对象:out内置对象是在JSP开发过程中使用得最为频繁的对象,然而其使用起来也是最简单的.out对象用于向客户端浏览器输出数         ...

  5. Struts2之Action与配置文件

    一.Struts2配置文件 1.struts.properties 在学习Action之前先学下Struts2的配置文件,与Struts2相关的配置文件有好几个,常用的有Struts.xml,web. ...

  6. struts2在action中获取request、session、application,并传递数据

    假设仅仅是通过request.session.application传递数据,则不须要获取对应的对象也能够传递数据,代码例如以下: ScopeAction.java: package com.ithe ...

  7. 一、JSP、servlet、SQL三者之间的数据传递(前台与后台数据交互)

    背景: 目前业界很流行的MVC(model-view-control)开发模式,理解为 模型是Bean, 视图是 Html/Jsp, 控制是Servlet, 关联数据库的Dao web的运行机制: 数 ...

  8. JSP Servlet SQL 三者之间数据传递

    前言: 最近一直在做WEB开发,现总结一下这一段时间的体会和感触. 切记,web开发重点在于前台数据交互,页面美化而不要太沉溺于底层数据. 浏览器时代来到,向我们召唤出更炫.更简洁.更方便.更大气的网 ...

  9. Struts2初学 Struts2的action接收用户数据方式

    一.简介    开发Web应用程序,首先应会遇到对用户输入数据的接收,传统的Web应用程序是由开发人员调用HttpServletRequest的getparameter(String name)方法从 ...

随机推荐

  1. 关于controller,service,dao层的问题记录

    出错写法: 1>.AlarmRecordController art=new AlarmRecordController(); 2>.private  static SystemServi ...

  2. nginx配置文件详解【nginx.conf】

    #基本配置: #user nobody;#配置worker进程运行用户worker_processes 1;#配置工作进程数目,根据硬件调整.通常等于CPU数量或者2倍于CPU数量 比如四核电脑(可以 ...

  3. Nginx服务优化及优化深入(配置网页缓存时间、日志切割、防盗链等等)

    原文:https://blog.51cto.com/11134648/2134389 默认的Nginx安装参数只能提供最基本的服务,还需要调整如网页缓存时间.连接超时.网页压缩等相应参数,才能发挥出服 ...

  4. Luogu P1290 欧几里得的游戏/UVA10368 Euclid's Game

    Luogu P1290 欧几里得的游戏/UVA10368 Euclid's Game 对于博弈论的题目没接触过多少,而这道又是比较经典的SG博弈,所以就只能自己来推关系-- 假设我们有两个数$m,n$ ...

  5. Maven创建本地仓库

    1:创建仓库目录 在D盘Program Files目录下创建repository目录 2:修改settings.xml ​ ​ D:\ProgramFiles\repository  是我们创建的本地 ...

  6. [原创]extjs htmleditor增加截图快速粘贴功能 插件

    因客户需求,需要QQ截图后,能直接粘贴到htmleditor编辑器中,不要保存本地文件再上传,再插入到编辑器,太过麻烦. 常规做法:QQ截图-->选择保存路径-->确定保存文件--> ...

  7. P2161 [SHOI2009]会场预约[线段树/树状数组+二分/STL]

    题目描述 PP大厦有一间空的礼堂,可以为企业或者单位提供会议场地.这些会议中的大多数都需要连续几天的时间(个别的可能只需要一天),不过场地只有一个,所以不同的会议的时间申请不能够冲突.也就是说,前一个 ...

  8. WA又出现了

    为甚么本蒟蒻写的代码永远有BUG? 为甚么本蒟蒻永远检查不出错误? 通过良久的分析,我得出一个结论:写代码也要有信仰. 人是要有信仰的,OI选手也不例外. 原因就是写之前没有膜拜上帝.真主.释迦摩尼. ...

  9. python打造批量关键词排名查询工具

    自己做站点的时候,都看看收录和关键词排名什么的,所以打造的这个批量关键词查询工具. #encoding:utf-8 import urllib,re,random,time,sys,StringIO, ...

  10. mysqlcheck(MyISAM表维护工具)

    mysqlheck [option] db_name [tables] -c --check(检查表) -r  --repair(修复表) -a  --analyze(分析表) -o  --optim ...