使用XWork校验框架

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

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

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>
	<s:form action="test_.html" method="post">
		<s:textfield name="name" label="用户名" required="true"></s:textfield>
		<s:password name="password" label="密码" required="true"></s:password>
		<s:submit key="submit" value="登录"></s:submit>
	</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="input">/index.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;

    @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;
    }

}


UserAction-validation.xml:

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC
        "-//Apache Struts//XWork Validator 1.0.2//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd">
<validators>
	<field name="name">
		<field-validator type="requiredstring">
			<message>请输入用户名</message>
		</field-validator>
	</field>

	<field name="password">
		<field-validator type="requiredstring">
			<message>请输入密码</message>
		</field-validator>
	</field>

</validators>

 


UserAction-validation.xml要放在和UserAction同一包下。

 

 

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>
	<h2>
		<s:property value="name" />
		,登录成功
	</h2>
</body>
</html>


 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值