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

  • 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复选框时,后台代码就接收不了参数值。

  • s:radio、s:select(单选框),s:checkboxlist(复选框):

1、list,listKey,listValue这3个属性对radio,select,checkboxlist等标签非常重要;

2、可以把一个String,一个数据,一个Enumeration,Iterator,Map或Collection赋值给list属性。

form-tags.jsp

        <s:radio list="#{'0':'male','1':'famel' }" name="gender" label="Gender"></s:radio>
<% List<City> cities=new ArrayList<City>();
cities.add(new City(1001,"City1"));
cities.add(new City(1002,"City2"));
cities.add(new City(1003,"City3"));
cities.add(new City(1004,"City4"));
request.setAttribute("cities", cities);
%>
<s:checkboxlist list="#request.cities" listKey="cityId" listValue="cityName" name="cities" label="City"></s:checkboxlist> <s:select list="#{10:10,20:20,30:30,40:40,50:50}" label="Age" name="age" headerKey="" headerValue="please select ...">
<s:optgroup label="11-19" list="#{11:11,12:12,13:13,14:14,15:15,16:16,17:17,18:18,19:19 }"></s:optgroup>
<s:optgroup label="11-19" list="#{21:21,22:22,23:23,24:24,25:25,26:26,27:27,28:28,29:29 }"></s:optgroup>
</s:select>

需要注意事项:如果后台要接受参数checkboxlist后台类型不能是数据,否则接收参数会失败,需要是List类型最好。

s:select第一个例子:

<s:select list="{'aa','bb','cc'}" theme="simple" headerKey="00" headerValue="00"></s:select>

s:select第二个例子:

<s:select list="#{1:'aa',2:'bb',3:'cc'}"  label="abc" listKey="key" listValue="value"  headerKey="0" headerValue="aabb">

s:select第三个例子:

< %
Java.util.HashMap map = new java.util.LinkedHashMap();
map.put(1,"aaa");
map.put(2,"bbb");
map.put(3,"ccc");
request.setAttribute("map",map);
request.setAttribute("aa","2");
%>
< s:select list="#request.map"  label="abc" listKey="key" listValue="value"
value="#request.aa"  headerKey="0" headerValue="aabb"></
s:select
>
headerKey headerValue 为设置缺省值
摘抄自 http://www.cnblogs.com/modou/articles/1326683.html

s:select第四个例子

public class Program implements Serializable {
private static final long serialVersionUID = 1L;
private int programid;
private String programName;
public int getProgramid() {
return programid;
}
public void setProgramid(int programid) {
this.programid = programid;
}
public String getProgramName() {
return programName;
}
public void setProgramName(String programName) {
this.programName = programName;
}
} 在 xxx extends ActionSupport {
private List<Program> programs ;
public List<Program> getPrograms() {
return programs;
}
public void setPrograms(List<Program> programs) {
this.programs = programs;
}   private MyBean bean;
  public MyBean getBean(){
    return bean;
  }
  public void setBean(MyBean bean){
    this.bean=bean;
  }
}

在jsp页面

<s:select list="programs"  listValue="programName" listKey="programid"  name="program" id="program" headerKey="0l" headerValue=" "   value="bean.programid">

</s:select>  
红色部分为在action里面的list,黄色为<option value="xxx">value</option>对应bean里面的字段programName

绿色部分为<option value="xxx",对应bean里面的字段programid

紫色部分为设定select被选中的值,s:select 会自动在bean选中key对应的值

上一篇:MFC原理第一讲.MFC的本质.以及手工编写MFC的程序


下一篇:Linux JDK 安装