Struts中的参数传值

来源:互联网 发布:php java 前景 编辑:程序博客网 时间:2024/06/03 18:57

学习Struts肯定要了解Struts中界面和action相互传值的方式,从界面到action的传递方式就十分简单,只需要对应javabean中的属性就好。

<body><!-- 界面向action传递值只需要命名对应就好 -->    <form action="hello" method="post">        用户:<input type="text" name="user.name"> <br>        密码:<input type="password" name="user.password"><br>         <input type="submit" value="ok">     </form>    <a href="hello_add">通配符测试1</a><a href="hello_edit">通配符测试2</a></body>

action到界面的传值就分为三种方式,普通的传值、actionContext传值和使用servlet的API进行传值,每种都可以使用el表达式和struts的标签来显示值。

//包括el表达式和struts的标签传值都需要get和set方式    private User user;    public User getUser() {        return user;    }    public void setUser(User user) {        this.user = user;    }    public String execute() throws Exception {        System.out.println("执行了execute!");        UserServer server = new UserServer();        if (server.login(user)) {            //使用ActionContext来传递参数            ActionContext.getContext().put("wood", 123);            ActionContext.getContext().put("free", "two");            //直接使用servlet中的API进行传值            ServletActionContext.getRequest().setAttribute("wood", "three");            return SUCCESS;        } else {            return ERROR;        }    }
<body><!-- el表达式的获取值方式 -->${user.name },你的密码是:${user.password }<br><!-- struts标签获取值的方式   在el表达式的基础上可以实现设置基础值  去除html标签等功能--><s:property value="user.name" default="Free"/><s:property value="user.password"/><br><!-- 使用actionConte来传递数据,其中使用s:property来访问的时候需要加#(2.3之后有改动,但建议加上!) -->${wood },${free }<br><s:property value="#wood"/><s:property value="#free"/><br><!-- 使用servlet的api获取值同样可使用struts的标签,注意加上获取的方式! -->${wood } <s:property value="#request.wood" /></body>
0 0
原创粉丝点击