struts validate

1  login.jsp方式1

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <s:actionerror/>
    <form action="login" method="post">
            username:<input type="text" name="user.username" value="${user.username}"/>
                <span style="color: red">${fieldErrors['user.username'][0]}</span><br/>
            password:<input type="text" name="user.password" value="${user.password}"/>
                <span style="color: red">${fieldErrors['user.password'][0]}</span><br/>
            <input type="submit" value="submit"/>
    </form>
    <s:debug/>
  </body>
</html>

 

1.1  login.jsp方式2

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  <s:actionerror/>
    <form action="login" method="post">
            username:<input type="text" name="user.username" value="${user.username}"/>
                <span style="color: red"><s:fielderror name="username" value="user.username"></s:fielderror></span><br/>
            password:<input type="text" name="user.password" value="${user.password}"/>
                <span style="color: red">
                    <s:fielderror name="password" value="user.password"/></span><br/>
            <input type="submit" value="submit"/>
    </form>
    <s:debug/>
  </body>
</html>

2.LoginAction

package org.hbin.test.action;

import org.hbin.test.entities.User;
import com.opensymphony.xwork2.ActionSupport;


public class LoginAction extends ActionSupport {
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Override
    public String execute() throws Exception {
        System.out.println(user);
        return super.execute();
    }

    @Override
    public void validate() {
        if (null == user.getUsername() || "".equals(user.getUsername().trim())) {
            this.addFieldError("user.username", "username is required!");
            this.addActionError("处理动作失败!");
        }
        if (null == user.getPassword() || "".equals(user.getPassword().trim())) {
            this.addFieldError("user.password", "password is required!");
        }
    }
}

 

 

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"></constant>
    
    <package name="test" namespace="/" extends="struts-default">
        <global-results>
            <result name="success">/success.jsp</result>
            <result name="error">/error.jsp</result>
        </global-results>
     
        <action name="login" class="org.hbin.test.action.LoginAction">
            <result name="input">/login.jsp</result>
        </action>
    </package>
    
</struts>

 

posted @ 2014-09-23 15:54  a757956132  阅读(186)  评论(0编辑  收藏  举报