Struts2标签的应用

39 篇文章 1 订阅
7 篇文章 0 订阅

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)、QQ技术交流群(183198395)。

下面使用一个注册的例子来说明Struts2常见标签的使用

index.jsp:

 

<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>起始页</title>
</head>
<body>

	<h2>用户注册</h2>
	<s:form action="test_.html" method="post">
		<s:textfield name="name" label="用户名" required="true"
			requiredPosition="left"></s:textfield>
		<s:password name="password" label="密码" required="true"
			requiredPosition="left"></s:password>
		<s:radio name="sex" list="#{1:'男',0:'女'}" label="性别" required="true"
			requiredPosition="left"></s:radio>
		<s:select list="{'请选择省份','吉林','四川','广东','云南'}" name="province"
			label="省份"></s:select>
		<s:checkboxlist list="{'足球','羽毛球','乒乓球','篮球'}" name="hobby" label="爱好"></s:checkboxlist>
		<s:textarea name="description" cols="30" rows="5" label="描述"></s:textarea>
		<s:submit value="注册"></s:submit>
		<s:reset value="重置"></s:reset>
	</s:form>

</body>
</html>


struts.xml:

 

 

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>

	<constant name="struts.i18n.encoding" value="UTF-8" />
	<constant name="struts.devMode" value="false" />
	<constant name="struts.i18n.reload" value="true" />
	<constant name="struts.configuration.xml.reload" value="true" />
	<constant name="struts.ognl.allowStaticMethodAccess" value="true" />
	<constant name="struts.action.extension" value="html" />

	<!-- 声明包 -->
	<package name="default" extends="struts-default" namespace="/">

		<action name="test_*" class="com.home.web.action.UserAction"
			method="{1}">
			<!-- 添加成功的映射页面 -->
			<result name="success">/pages/success.jsp</result>
			<!-- 修改成功的映射页面 -->
			<result name="error">/pages/error.jsp</result>
		</action>
	</package>
</struts>


UserAction:

 

 

package com.home.web.action;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

    /** serialVersionUID. */
    private static final long serialVersionUID = 1L;

    private String name;
    private String password;
    private String description;
    private int sex = 0;
    private String province;
    private String[] hobby;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getProvince() {
        return province;
    }

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

    public String[] getHobby() {
        return hobby;
    }

    public void setHobby(String[] hobby) {
        this.hobby = hobby;
    }

}


success.jsp:

 

 

<%@ page language="java" autoFlush="true"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<!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>成功提示页面</title>
</head>
<body>
	<div>
		<h2>用户注册信息</h2>
		<ul>
		<li>用户名:<s:property value="name"/></li>
		<li>密码:<s:property value="password"/></li>
		<li>性别:<s:if test="sex==0">女</s:if><s:else>男</s:else></li>
		<li>省份:<s:property value="province"/></li>
		<li>爱好:<s:property value="hobby"/></li>
		<li>描述:<s:property value="description"/></li>
		</ul>
	</div>
</body>
</html>


 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值