(Struts2学习篇)Struts2标签库(表单标签)
小虾米
阅读:661
2021-04-01 10:26:52
评论:0
一、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>
执行结果图:
结果:
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。