码迷,mamicode.com
首页 > 其他好文 > 详细

Struts(十四):通用标签-form表单

时间:2017-03-23 01:26:52      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:标签   pass   接收   res   block   box   输入   填充   jsp页面   

  • form标签是struts2标签中一个重要标签:
  1. 可以生成html标签,使用起来和html的form标签差不多;
  2. Strut2的form标签会生成一个table,进行自动布局;
  3. 可以对表单提交的值进行回填:从栈顶对象开始配置属性,并把匹配的属性值赋到对应的标签value中,若栈顶对象没有对应的属性,则依次向下栈中找对应的属性。
  • 为什么form标签可以实现填充form表单?

示例:写一个这样的form页面,提交form页面后跳转到自身页面

form-tags.jsp页面:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <s:debug></s:debug>
    <s:form action="formtags.action" method="post">
        <s:hidden name="userId" label="UserId"></s:hidden>
        <s:textfield name="userName" label="UserName"></s:textfield>
        <s:password name="password" label="Password"></s:password>
        
        <s:submit name="submit" label="Submit"></s:submit>
    </s:form>
</body>
</html>

struts.xml

<action name="formtags" class="com.dx.struts2.FormTagsAction" method="save">
            <result name="input">/form-tags.jsp</result>
        </action>

FormTagsAction页面:

package com.dx.struts2;

import com.opensymphony.xwork2.ActionContext;

public class FormTagsAction {
    private String userId;
    private String userName;
    private String password;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String save() {
        System.out.println(this);

        FormTagsAction action=new FormTagsAction();
        action.setUserId("1001");
        action.setUserName("AAA");
        action.setPassword("pwd");
        
        ActionContext.getContext().getValueStack().push(action);
        
        return "input";
    }

    @Override
    public String toString() {
        return "FormTagsAction [userId=" + userId + ", userName=" + userName + ", password=" + password + "]";
    } 
}

在浏览器中输入http://localhost:8080/Struts_01/form-tags.jsp在form表单中输入username:bb,password:cc,点击提交按钮,会在后台控制台上输出响应信息:

"FormTagsAction [userId=, userName=bb, password=cc]" 

程序会自动跳转到form-tags.jsp页面自身,这是我们发现username被自动填充为AAA,这说明了form表单会自动填充是因为------form标签会从值栈中自动寻找与自身标签相匹配的属性值作为标签值。

s:checkbox复选框
用法:

<s:form action="formtags.action" method="post">        
        <s:checkbox name="married" label="Married"></s:checkbox>
        
        <s:submit name="submit" label="Submit"></s:submit>
    </s:form>

FormTagsAction.java中追加:属性private String married;实现set,get方法。

使用married字段来接收页面提交的参数。

需要注意点:s:checkbox生成的html比较特殊

<input type="checkbox" name="married" value="true" id="formtags_married"/>
<
input type="hidden" id="__checkbox_formtags_married" name="__checkbox_married" value="true" />
<label for="formtags_married" class="checkboxLabel">Married</label>

它生成了一个hidden标签,如果缺少了个标签:如果未选中married复选框时,后台代码就接收不了参数值。

 

Struts(十四):通用标签-form表单

标签:标签   pass   接收   res   block   box   输入   填充   jsp页面   

原文地址:http://www.cnblogs.com/yy3b2007com/p/6602621.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!