Www.helper3.com
热门关键字: php net javascript java
Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2 <s:radio>单选按钮例子
Struts2 <s:radio>单选按钮例子
作者:--    发布时间:2019-11-20 10:15:08

在struts2,可以使用<s:radio>标签来创建一个html单选按钮。有很多方法来填充数据到单选按钮,可以通过list, ognl 或 object。请查看下面的例子就知道了。

struts2 <s:radio> 示例

举个例子来说明如何使用列表,ognl和对象数据填充到struts2中,使用<s:radio>标签创建一个单选按钮。

1. 动作 - action

action类来创建性别和语言值单选按钮列表。

radiobuttonaction.java

package com.h3.common.action;

import java.util.arraylist;
import java.util.list;

import com.h3.common.model.language;
import com.opensymphony.xwork2.actionsupport;

public class radiobuttonaction extends actionsupport{

	private list<string> genders;
	private list<language> languages ;
	
	private string yourgender;
	private string youranswer;
	private string yourlanguage;
	
	private static final string male = "male";
	private static final string female = "female";
	private static final string unknown = "unknown";
	
	public radiobuttonaction(){
		
		genders = new arraylist<string>();
		genders.add(male);
		genders.add(female);
		genders.add(unknown);
		
		languages = new arraylist<language>();
		languages.add( new language("en", "english") );
		languages.add( new language("fr", "france") );
		languages.add( new language("cn_zh", "chinese") );
		languages.add( new language("de", "german") );
		
	}
	
	//return default gender value
	public string getdefaultgendervalue(){
		return unknown;
	}

	//return default language value
	public string getdefaultlanguagevalue(){
		return "cn_zh";
	}
	
	public string execute() {
		return success;
	}
	public string display() {
		return none;
	}
	
	//getter ad setter methods
}

language.java

package com.h3.common.model;

public class language{

	private string languagecode;
	private string languagedisplay;
	
	public string getlanguagecode() {
		return languagecode;
	}
	public void setlanguagecode(string languagecode) {
		this.languagecode = languagecode;
	}
	public string getlanguagedisplay() {
		return languagedisplay;
	}
	public void setlanguagedisplay(string languagedisplay) {
		this.languagedisplay = languagedisplay;
	}
	
	public language(string languagecode, string languagedisplay) {
		this.languagecode = languagecode;
		this.languagedisplay = languagedisplay;
	}
}

2. 结果页面

结果页面是使用struts2 “s:radio” 通过list, ognl 和 object来创建的三个单选按钮。

radiobutton.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
</head>

<body>
<h1>struts 2 radio button example</h1>

<s:form action="resultaction" namespace="/">

<h2>
  <s:radio label="gender" name="yourgender" list="genders" value="defaultgendervalue" />
  <s:radio label="gender" name="yourlanguage" list="languages" 
      listkey="languagecode" listvalue="languagedisplay" value="defaultlanguagevalue" />
  <s:radio label="answer" name="youranswer" list="#{'1':'yes','2':'no'}" value="2" />
</h2> 

<s:submit value="submit" name="submit" />
	
</s:form>

</body>
</html>a

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>struts 2 radio button example</h1>

<h2>
  your selected gender : <s:property value="yourgender"/>
</h2> 
<h2>
  your selected language : <s:property value="yourlanguage"/>
</h2> 
<h2>
  your selected answer : <s:property value="youranswer"/>
</h2> 

</body>
</html>

3. struts.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public
"-//apache software foundation//dtd struts configuration 2.0//en"
"http://struts.apache.org/dtds/struts-2.0.dtd">
 
<struts>

<constant name="struts.devmode" value="true" />
 	
  <package name="com.h3.common.action" namespace="/" extends="struts-default">
		
    <action name="radiobuttonaction" 
         class="com.h3.common.action.radiobuttonaction" method="display">
		<result name="none">/pages/radiobutton.jsp</result>
    </action>
		
    <action name="resultaction" class="com.h3.common.action.radiobuttonaction">
		<result name="success">/pages/result.jsp</result>
    </action>
  </package>
	
</struts>

5. 示例

http://localhost:8080/struts2radio/radiobuttonaction.action


http://localhost:8080/struts2radio/resultaction.action


要了解如何预选值,读这篇文章 – 在struts2的单选按钮预选值

参数

  1. http://struts.apache.org/2.0.11.2/docs/radio.html
  2. http://struts.apache.org/2.1.8.1/docs/struts-2-form-tags.html

所有评论

loading......

滚动到顶部 滚动到底部
合作联系(微信):
www.helper3.com
关于本站:
宗旨
网站备案号:
粤ICP备17073857号-1

声明:
本站部分内容来自网络,如您发现本站内容侵害到您的利益,请联系本站管理员处理。