(Struts2学习篇)Struts2标签库(表单标签)

一、jsp页面代码(index.jsp)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<!--head标签必须和datetimepicker标签一起使用  -->
	<sx:head/>
  </head>  
  <body>
  <!--struts2标签库 (表单标签) -->
  <s:form action="login">     	
  		<!--文本框  -->
        <s:textfield name="username" key="用户"/>
        <s:textfield name="password" key="密码"/>
        <!--按钮  -->
        <s:submit value="登入"/>
        <s:submit value="取消"/>
        <!--复选框  -->
     	<s:checkboxlist name="hobbies" label="兴趣爱好" labelposition="top" list="{'登山','游泳','阅读'}"/>
     	<!--下拉框  -->
     	<s:combobox name="book" label="请选择你感兴趣的图书" labelposition="top" list="{'EJB 入门精通','Android 的疯狂讲义','Spring2.0精解'}"></s:combobox>
     	<!--时间和日期框  -->
     	<sx:datetimepicker name="birthday" labelposition="top" label="日期选择框" toggleType="explode" value="today" />    
     	<!--级联列表框  --> 
     	<s:doubleselect name="province" list="{'湖南省','江西省'}" doubleList="top=='湖南省'?{'长沙','株洲','湘潭'}:{'南昌','新余','宜春'}" doubleName="city"></s:doubleselect>
     	
     	<!--生成列表框  -->
     	<s:select name="books" label="请选择你感兴趣的图书" labelposition="top" multiple="true" list="{'EJB 入门精通','Android 的疯狂讲义','Spring2.0精解','精通Struts2'}"></s:select>
     	<!--单选框  -->
     	<s:radio name="service" label="请选择相关服务" labelposition="top" list="{'Weblog','WebSphrere','JBoss'}"/>	
  </s:form>  
  </body>
</html>


二、action页面(Login.action)

public class LoginAction implements Action {
	//相关登入帐号和密码(文本框)
	private String username;
	private String password;
	//复选框
	private String[] hobbies;
	//下拉框
    private String[] book;
    //时间
    private String birthday;
    //级联表框
    private String province;
    private String city;
    //多项选择
    private String[] books;
    //单选框
    private String service;
	

	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 execute() throws Exception {
		// TODO Auto-generated method stub
		ActionContext context=ActionContext.getContext();
		//访问统计数
		Integer counter=(Integer) context.getApplication().get("counter");
		if(counter==null){
			counter=1;
		}else{
			counter=counter+1;
		}
		//通过ActionContext设置application 的范围属性
		context.getApplication().put("counter", counter);
		//通过ActionContext设置session的范围属性
		context.getSession().put("username", getUsername());
		if(getUsername().equals("admin")&&getPassword().equals("123456")){
			//通过ActionContext设置request的范围属性
			context.put("response", "登入成功!");
			return SUCCESS;
			
		}else{
			//通过ActionContext设置request的范围属性
			context.put("response", "登入失败!");
			return ERROR;
		}

	}


	public void setHobbies(String[] hobbies) {
		this.hobbies = hobbies;
	}


	public String[] getHobbies() {
		return hobbies;
	}


	public void setBook(String[] book) {
		this.book = book;
	}


	public String[] getBook() {
		return book;
	}


	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}


	public String getBirthday() {
		return birthday;
	}


	public void setProvince(String province) {
		this.province = province;
	}


	public String getProvince() {
		return province;
	}


	public void setCity(String city) {
		this.city = city;
	}


	public String getCity() {
		return city;
	}


	public void setBooks(String[] books) {
		this.books = books;
	}


	public String[] getBooks() {
		return books;
	}


	public void setService(String service) {
		this.service = service;
	}


	public String getService() {
		return service;
	}

}


三、结果页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    
    <title>My JSP 'index.jsp' starting page</title>
	
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
      
  <body>
       本站的访问次数:${applicationScope.counter} </br>
       ${sessionScope.username},你已经登入!</br>
       ${requestScope.response}  </br>
       <!--展示返回相关数据信息  -->
       复选框相关结果:
       <s:property value="hobbies"/><br>
      下拉框相关结果: 
       <s:property value="book"/><br>
   时间相关结果:
       <s:property value="birthday"/><br>
    级联列表结果:
     <s:property value="province"/><br>
      <s:property value="city"/><br>
    多项列表框
      <s:property value="books"/><br>
  单选框
  <s:property value="service"/><br>
      
   
  </body>
</html>


执行结果图:

结果:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值