Strust2_常用标签的使用

package org.com.test.model;

public class Group {
	private int id;
	private String name;
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Group(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}
	public Group() {
		super();
	}
	@Override
	public String toString() {
		return "Group [id=" + id + ", name=" + name + "]";
	}
	
}

package org.com.test.action;

import java.util.ArrayList;
import java.util.List;

import org.com.test.model.Group;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@SuppressWarnings("serial")
public class GroupAction extends ActionSupport implements ModelDriven<Group>{
	private Group group;
	
	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

	public String add() {
		
		return SUCCESS;
	}
	
	public String addInput(){
		//group = new Group(1, "阿帕奇");
		group.setId(1);
		group.setName("李白");
		List<String> interests  = new ArrayList<String>();
		interests.add("football");
		interests.add("basketball");
		List<Group> groups = new ArrayList<Group>();
		groups.add(new Group(1, "传销"));
		groups.add(new Group(2, "直销"));
		groups.add(new Group(3, "丐帮"));
		groups.add(new Group(4, "逍遥派"));
		ActionContext.getContext().put("username", "张三");
		ActionContext.getContext().put("groups", groups);
		ActionContext.getContext().put("interests", interests);
		return SUCCESS;
	}

	@Override
	public Group getModel() {
		if (group==null) {
			group = new Group();
		}
		return group;
	}
}

<%@ 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>测试Struts标签</title>
</head>
<body>
	<s:debug/>
	<%-- <form action="Group_add.action" method="post">
		ID标识:<input type="text" name="id" value="${id }">
		组名称:<input type="text" name="name" value="${name }">
		<input type="submit">
	</form> --%>
	<hr>
	<s:form action="Group_add" method="post">
		<!-- 从Root中的Model(Group)取值 -->
		<s:textfield label="ID标识" name="id"/>
		<s:textfield label="组名称" name="name"/>
		
		<!-- 在Struts2中要在Struts2的标签中引入相应的ActionContext的值,要使用%{xxx} -->
		
		<s:textfield label="用户名" name="00username" value="%{username}"/>
		
		<!-- 对于列表而言,引入ActionContext的值,新版本已经可以使用#`{xx}(视频讲解说这个,可自己用这个不行) ;
		(这个可以#interests)来访问,但是依然建议使用%{xx}来访问 -->
		<!-- 从ActionContext中取值 -->
		<s:checkboxlist label="兴趣" name="interest" listKey="key" listValue="value"
		list="#{'football':'足球','basketball':'篮球','yu':'羽毛球'}" value="%{interests}"/>	
		
		<s:radio list="#{'0':'男','1':'女' }" label="性别" name="xingbie" value="0"/>
		<s:select list="#groups" listKey="id" listValue="name"
		 label="选择组" headerKey="-1" headerValue="选择相应的组"></s:select>
		<s:submit value="添加"/>
	</s:form>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值