【java】jsp和struts2之间如何传值?

来源:互联网 发布:移动4g网络培训 编辑:程序博客网 时间:2024/05/16 14:07

一、前言

      近期做的项目使用的是SSH框架,其中当然也有很多的问题,自己也查了很多,其中有一个就是jsp和struts2之间如何传值?下面小编就向大家分享一下。

二、Action中获取页面中的数据

      这个分成两种情况:

1.如果页面中的数据来源于一张表,直接使用模型驱动获得就可以了2.如果页面中的数据来源于多张表,则可以采用模型驱动 结合属性驱动的方式。

2.1 一张表,模型驱动


这里写图片描述

      如上图所示,在图中,是部门管理,在部门管理中,有部门名称+部门说明。jsp页面显示的数据都是来自“部门”这张表的。所以可以直接使用模型驱动。

      DepartmentAction:

package cn.itcast.oa.struts2.action;import java.lang.reflect.InvocationTargetException;import java.util.Collection;import javax.annotation.Resource;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import cn.itcast.oa.domain.Department;import cn.itcast.oa.service.DepartmentService;import cn.itcast.oa.struts2.action.base.BaseAction;import cn.itcast.oa.utils.DeleteMode;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ModelDriven;@Controller("departmentAction")@Scope("prototype")public class DepartmentAction extends BaseAction implements ModelDriven<Department> {    //模型驱动实现方法,必须要有new Department(),否则数据为空    private Department department = new Department();    public Department getModel() {        return department;    }    //通过注解-注入DepartmentService    @Resource(name="departmentService")    private DepartmentService departmentService;    /**     * 获取所有的部门-2017年5月6日17:21:41     * @return     */    public String getAllDepartment(){        Collection<Department> DepartmentList = departmentService.getAllDepartment();        //把一个对象存放到map栈中        ActionContext.getContext().put("departmentList", DepartmentList);        return listAction;    }}

      这里值得提醒的是,这里我们实现了ModelDriven,就是模型驱动,模型驱动的作用是什么呢?

      首先,作用是把一个对象放到栈顶,其次会产生 getModel() 这个方法,我们通过private Department department = new Department()生成实体类,然后我们就可以获取到这个对象的内容了。所以我们使用的时候就可以直接department.getdname()就可以获得相应的属性的值了。

2.2 多张表,模型驱动+属性get set


这里写图片描述

      UserAction :

package cn.itcast.oa.struts2.action;import java.util.Collection;import java.util.Set;import javax.annotation.Resource;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Controller;import cn.itcast.oa.domain.Department;import cn.itcast.oa.domain.Post;import cn.itcast.oa.domain.User;import cn.itcast.oa.service.DepartmentService;import cn.itcast.oa.service.PostService;import cn.itcast.oa.service.UserService;import cn.itcast.oa.struts2.action.base.BaseAction;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ModelDriven;@Controller("userAction")@Scope("prototype")public class UserAction extends BaseAction implements ModelDriven<User> {    private User user = new User();    public User getModel() {        return user;    }    @Resource(name = "userService")    private UserService userService;    @Resource(name = "departmentService")    private DepartmentService departmentService;    @Resource(name = "postService")    private PostService postService;    private long did;    private long[] pids;    public long getDid() {        return did;    }    public void setDid(long did) {        this.did = did;    }    public long[] getPids() {        return pids;    }    public void setPids(long[] pids) {        this.pids = pids;    }    public String getAllUser() {        Collection<User> userList = this.userService.getAllUser();        ActionContext.getContext().getValueStack().push(userList);        return listAction;    }public String add() {        // 1.根据did提取改部门        Department department = this.departmentService                .getDepartmentById(this.did);        // 2.根据pids提取出岗位        Set<Post> posts = this.postService.getPostsByIds(pids);        // 3.建立user对象和部门和岗位之间的关系        user.setPosts(posts);        user.setDepartment(department);        // 4.save        this.userService.saveUser(user);        return action2action;    }}

      注:我们这里使用了private long did;建立了user实体类之外的属性,这就是属性驱动。这样这个属性就可以直接使用了。

三、jsp获取Action的数据

      Action中的数据放入的到值栈中,在jsp页面可以通过:

1.struts语言,类似<s:property value="username"/>2.EL表达式,类似${user.addr}3.JSP表达式,类似<%=user.getAddr( ) %>4.使用AJAX,下篇文章介绍

      在下文的jsp中,${department.dname}和混合使用,显示的效果图如下:


这里写图片描述

      list.jsp:

 <!--显示数据列表-->        <tbody id="TableData" class="dataContainer" datakey="userList">            <s:iterator>                <tr class="TableDetail1 template">                    <td>${department.dname}<input type="checkbox" name="userCheckBox"/></td>                    <td><s:property value="username"/></td>                    <td><s:property value="department.dname"/></td>                    <td>                        <s:iterator value="posts">                            <s:property value="pname"/>                        </s:iterator>                    </td>                    <td>                        <s:a action="userAction_delete?uid=%{uid}">删除</s:a>                        <s:a action="userAction_updateUI?uid=%{uid}">修改</s:a>                        <a href="javascript:privilegeclick();">设置权限</a>                    </td>                </tr>            </s:iterator>        </tbody>

四、小结

      通过总结分析前台和后台的Action传输数据的方式,真实解决了很大的麻烦,当自己不知道如何使用的时候也是非常费劲的。可以在jsp中看出我使用了s:iterator来进行遍历表格。其实如果我们使用AngularJS的话,可以直接在Action中把数据拼凑成json,然后存储在值栈中。在前台可以直接使用