首页 新闻 会员 周边 捐助

struts2后台表单校验问题

0
悬赏园豆:200 [已解决问题] 解决于 2014-05-22 17:07

错误填写表单不能通过验证,能够正常跳转到设置的页面,但是跳转到错误的页面用<s:fielderror/> 输出不了设置的message难道是注解配置的不能这么输出么

java的action类如下

package xxx.user.controller;

import java.io.IOException;
import java.io.PrintWriter;
import com.opensymphony.xwork2.ActionSupport;  
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
import com.opensymphony.xwork2.validator.annotations.FieldExpressionValidator;  
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RegexFieldValidator;  
import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;  
import com.opensymphony.xwork2.validator.annotations.Validations;  
import com.opensymphony.xwork2.validator.annotations.ValidatorType;  
import javax.servlet.http.HttpSession;
import javax.swing.Action;

import org.apache.struts2.interceptor.validation.SkipValidation;

import xxx.common.BaseAction;
import xxx.common.SendMail;
import xxx.user.pojo.Conuser;
import xxx.user.service.userService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.Validations;

@Validations(  
        requiredStrings={
                @RequiredStringValidator(fieldName="conuser.userName",message="输入用户名吧", shortCircuit=true,trim=true,type=ValidatorType.FIELD),  
                @RequiredStringValidator(fieldName="conuser.userPwd",message="输入密码吧",shortCircuit=true,trim=true,type=ValidatorType.FIELD),  
                @RequiredStringValidator(fieldName="conuser.userEmail",message="输入Email吧",shortCircuit=true,trim=true,type=ValidatorType.FIELD),  
},        requiredFields={@RequiredFieldValidator(fieldName="conuser.userPhone",message="输入电话吧",shortCircuit=true,type=ValidatorType.FIELD)},

        regexFields={
                @RegexFieldValidator(fieldName="conuser.getUserName",message="请正确输入合法用户名",expression="^\\s*[.A-Za-z0-9_-]{5,15}\\s*$"),
                @RegexFieldValidator(fieldName="conuser.userPwd",message="请正确输入合法密码",expression="^\\s*[.A-Za-z0-9_-]{5,15}\\s*$"),
                @RegexFieldValidator(fieldName="conuser.userPhone",message="请正确输入电话号码",expression="^1[3|4|5|8][0-9]\\d{8}$"),
                @RegexFieldValidator(fieldName="conuser.userEmail",message="邮箱不合法",expression="^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,3})\\s*$"),
        }
      
)
public class userAction extends BaseAction implements ModelDriven<Conuser> {


    private Conuser conuser = new Conuser();

    private userService service;
    
    public Conuser getConuser() {
        return conuser;
    }
    
    public void setConuser(Conuser conuser) {
        this.conuser = conuser;
    }
    
    public void setService(userService service) {
        this.service = service;
    }
    
    public Conuser getModel() {

        return conuser;
    }
    
    public userService getService() {
        return service;
    }
    @SkipValidation
    public String check() throws IOException {

        // Serverlet根据请求的参数,验证完成后,以xml格式写下验证信息。
        // 供JSP页面的XMLHttpRequest对象读取。
        response.setContentType("text/xml");
        response.setHeader("Cache-Control", "no-cache");

        // 验证用户名
        boolean ck = service.checkUserName(conuser);
        // 根据返回的结果,写下相应的XML信息。
        if (ck) {
            response.getWriter().write("<message>1</message>"); // 用户名不存在

            return null;

        } else {
            response.getWriter().write("<message>0</message>"); // 用户名存在

            return null;

        }
        // return "check";
    }
//插入用户...
    
    public String addUser() throws IOException {
        SendMail sm =new SendMail();
        service.addUser(conuser);
        sm.send(conuser);
        return "add";

    }
    @SkipValidation
    public String login() throws IOException {

        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
System.out.println(conuser.getUserName());
        
        if (conuser.getUserLevel() != 0) {

            HttpSession session = request.getSession();
            session.setAttribute("userName", conuser.getUserName());
            session.setAttribute("userLevel", conuser.getUserLevel());
            return SUCCESS;
        }
        // 返回登陆标识
        int flag = service.checkLogin(conuser);
        if (flag == 0) {
        //    out.println("<script>alert('用户名或密码不正确!');history.back();</script>");
            HttpSession session = request.getSession();
            session.setAttribute("userName", conuser.getUserName());
        

            session.setAttribute("userNO","<font color='red' size='2px'><b>用户名或密码不正确</b></font>");
            return "no";
        } else {
            // 在session范围存储键值对
            HttpSession session = request.getSession();
            session.setAttribute("userName", conuser.getUserName());
            session.setAttribute("userLevel", flag);
            session.setAttribute("userNO","");
            return SUCCESS;

        }

    }
}
<?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>
    <package name="user" namespace="/user" extends="struts-default">
    <global-results>
            <result name="input" type="redirect">/error.jsp</result>
    </global-results>
        <action name="login" class="userAction" method="login">
            <result name="success" type="redirect">/index.jsp</result>
            <result name="no" type="redirect">/login.jsp</result>
        </action>
        <action name="check" class="userAction" method="check">
             <result name="check" type="redirect" >/addUser.jsp</result>
        </action>
        <action name="addUser" class="userAction" method="addUser">
            <result name="add" type="redirect">/login.jsp</result>
        </action>
        
    </package>
</struts>
<%@ taglib uri="/struts-tags" prefix="s"%>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <base href="<%=basePath%>">

        <title>My JSP 'addUser.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">
    -->
    <!--      <script type="text/javascript" src="js/check.js" />
        <script type="text/javascript" src="ja/jquery-1.7.2.js"></script>
-->

    </head>
    <!-- oncontextmenu="return false" ondragstart="return false"
        onselectstart="return false"  -->
    <body onload="ms();">
        <div>
            <form action="<%=request.getContextPath()%>/user/addUser.action"
                method="post" id="addUser" name="addUser" onkeydown="if(event.keyCode==13){return false;}">
                <table width="600" border="0" align="center" cellpadding="0"
                    cellspacing="0">
                    <tr>
                        <td height="50" colspan="5" align="center" bgcolor="#CCCCCC">
                            <blockquote>
                                <blockquote>
                                    <blockquote>
                                        <p>
                                            <strong>用户注册</strong>
                                            
                                        </p>
                                    </blockquote>
                                </blockquote>
                            </blockquote>
                        </td>
                    </tr>
                    <tr>
                        <td width="39" height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td width="175" height="50" align="right" bgcolor="#E4E4E4">
                            用 户 名:
                        </td>
                        <td height="50" colspan="2" bgcolor="#E4E4E4">
                            <input name="userName" type="text" id="userName" size="24"
                                maxlength="20" onBlur="checkUserNameAction();" >
                        </td>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            <span id="checkUserNameMsg"></span>
                        </td>
                    </tr>
                    <tr>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            <s:fielderror></s:fielderror>
                             <s:actionerror />
                             <s:actionmessage/>
                             
                        </td>
                        <td height="50" align="right" bgcolor="#E4E4E4">
                            密 码:
                        </td>
                        <td height="50" colspan="2" bgcolor="#E4E4E4">
                            <input name="userPwd" type="text" id="userPwd" size="24"
                                maxlength="20" onBlur="checkUserPwd();">
                        </td>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            <span id="checkUserPwd"></span>
                        </td>
                    </tr>
                    <tr>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td height="50" align="right" bgcolor="#E4E4E4">
                            确认密码:
                        </td>
                        <td height="50" colspan="2" bgcolor="#E4E4E4">
                            <input name="userPwd1" type="text" id="userPwd1" size="24"
                                maxlength="20"  onBlur="checkUserPwdAgin();">
                        </td>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                        <span id="checkUserPwdAgin"></span>    
                        </td>
                    </tr>
                    <tr>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td height="50" align="right" bgcolor="#E4E4E4">
                            电话号码:
                        </td>
                        <td height="50" colspan="2" bgcolor="#E4E4E4">
                            <input name="userPhone" type="text" id="userPhone" size="24"
                                maxlength="15"  onBlur="checkUserPhone();">
                        </td>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            <span id="checkUserPhone"></span>     <s:fielderror/>
                        </td>
                    </tr>
                    <tr>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td height="50" align="right" bgcolor="#E4E4E4">
                            邮箱:
                        </td>
                        <td height="50" colspan="2" bgcolor="#E4E4E4">
                            <input name="userEmail" type="text" id="userEmail" size="24px"
                                maxlength="30"  onBlur="checkUserEmail();">
                        </td>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            <span id="checkUserEmail"></span>    
                        </td>
                    </tr>
                    <tr>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td height="50" bgcolor="#E4E4E4">
                            <input type="hidden" name="userLevel" id="userLevel" value="2" />
                        </td>
                        <td width="41" height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td width="142" height="50" bgcolor="#E4E4E4">
                            <input type="submit" name="submit" id="submit"
                                onclick="return check();" value="提交">
                        </td>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            <span id="wait"></span> 
    
                        </td>
                    </tr>
                    <tr>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td height="50" colspan="2" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                        <td height="50" bgcolor="#E4E4E4">&nbsp;
                            
                        </td>
                    </tr>
                </table>
            </form>
        </div>
        <br>
    </body>

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

本系统出现了一个异常,请联系管理员
<hr><s:fielderror></s:fielderror>
<s:actionerror/>
<s:actionmessage/>
错误消息: ${msg}

是不是我注解配置的有问题,还是怎么求解答

VuVu的主页 VuVu | 初学一级 | 园豆:22
提问于:2014-05-19 08:41
< >
分享
最佳答案
1

<result name="input" type="redirect">/error.jsp</result>

去掉type="redirect"   给分

收获园豆:200
angelshelter | 大侠五级 |园豆:9914 | 2014-05-22 17:01

测试ing

VuVu | 园豆:22 (初学一级) | 2014-05-22 17:05

哥  太谢谢你了啊   终于又大神帮我解决了, 哥我太崇拜你了,我也忘记这里我当初  哎 当时忘记改回来了,什么都不说了   谢谢 

VuVu | 园豆:22 (初学一级) | 2014-05-22 17:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册