Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Struts 1.x中检查Ajax弹出JSP页面的验证_Java_Ajax_Jsp_Struts - Fatal编程技术网

Java 如何在Struts 1.x中检查Ajax弹出JSP页面的验证

Java 如何在Struts 1.x中检查Ajax弹出JSP页面的验证,java,ajax,jsp,struts,Java,Ajax,Jsp,Struts,我在验证Struts 1.x时遇到了一些问题。我正在打开一个关于Ajax调用的JSP页面。在这个JSP页面(打开的弹出窗口)中,如果我想进行一些验证,比如需要用户标题等,并且在提交表单时,我直接将详细信息提交给所需的action类方法。提交时,我没有使用Ajax提交。我也尝试过Ajax submit,但它对我不起作用。下面的代码是我的action类方法和JSP页面: Action class public ActionForward saveNotificationsFormDetails(Ac

我在验证Struts 1.x时遇到了一些问题。我正在打开一个关于Ajax调用的JSP页面。在这个JSP页面(打开的弹出窗口)中,如果我想进行一些验证,比如需要用户标题等,并且在提交表单时,我直接将详细信息提交给所需的action类方法。提交时,我没有使用Ajax提交。我也尝试过Ajax submit,但它对我不起作用。下面的代码是我的action类方法和JSP页面:

Action class
public ActionForward saveNotificationsFormDetails(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        DynaValidatorActionForm notificationsForm=(DynaValidatorActionForm)form;
        log.info("In saveNotificationsFormDetails method of NotificationsManagementAction");
        String forwardName="failure";
        String filePath = null;
        String webPath = null;
        FileOutputStream outputStream = null;
        ActionMessages messages = new ActionMessages();     
        User user=null;     
        try
        {
            int userId=0;
            HttpSession session=request.getSession(false);
            if(session!=null)
            {
                user=(User)session.getAttribute("user");
                if(user!=null)
                {

                    if(user.isAdmin()==true)
                    {
                        userId=user.getPrimaryKey();
                        log.info("user is admin");
                    }
                    else
                    {
                        log.info("User has no rights to access this page ");
                        return mapping.findForward("userrestricted");
                    }
                }
                else
                {
                    return mapping.findForward("login");    
                }
            }
            log.info("The user id -"+userId+"-- is is requesting to Notification Details.");

            NotificationsDto notificationsDto = new NotificationsDto();
            NotificationsDao notificationsDao = NotificationsDao.getInstance();
            String  title =  (String) notificationsForm.get("title");
            String  content =  (String) notificationsForm.get("content");

            // uploading the file to notifications directory.
            FormFile url =(FormFile)notificationsForm.get("url");
            String fileName = url.getFileName();
            filePath = getServlet().getServletContext().getRealPath("")+"/notifications/"+ fileName;

            if(fileName != null && !fileName.equals("")) {
                outputStream = new FileOutputStream(new File(filePath));
                outputStream.write(url.getFileData());

                // getting the web path for the uploaded file
                //String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
                webPath =request.getContextPath()+"/notifications/"+url.getFileName();
            }
            String fromDate = (String) notificationsForm.get("fromDate");
            String toDate = (String) notificationsForm.get("toDate");
            String notifyUsersFlag = (String) notificationsForm.get("notifyUsersFlag");

            boolean notifyUsers = (notifyUsersFlag!=null && notifyUsersFlag.trim().length() > 0 && ( (notifyUsersFlag.trim().equals("on")) || (notifyUsersFlag.trim().equals("true")) ) ) ? true : false;

            //code written for sending the mails to user, if user click on notification checkbox

            if(notifyUsers){
                //NotificationEmailDispatcherJob();
                UserDao userDao = new UserDao();
                try {

                    Properties pro = new Properties();
                    InputStream in = PropertiesFileReader.getInstance().getInputStreamInstance("mail.properties");
                    pro.load(in);
                    String basePath = pro.getProperty("basePath");


                    // Algorithm Steps:
                    /*
                     * Step1: Get all the notifications which are still available to show.
                     * Step2: If notifications size > 0 do the steps(3,4,5) else stop processing.
                     * Step3: Get all users registered and activated in prep511.
                     * Step4: Prepare email content to be sent for each user.
                     * Step5: Validate the email content and send it to user. 
                     */ 

                    // Get all the notifications which are still available to show.
                    List<NotificationsDto> notifications = NotificationsDao.getInstance().getDisplayNotifications();


                    if(notifications !=null && notifications.size() > 0){
                        List<User> activatedUsers = userDao.getAllUsersForAnnoucement();

                        for (Iterator<NotificationsDto> iterator = notifications.iterator(); iterator
                                .hasNext();) {
                            notificationsDto =  iterator.next();
                            Iterator<User> usersIterator = activatedUsers.iterator();
                            while( usersIterator.hasNext() ) {
                                user =  usersIterator.next();

                                String emailContent = getEmailContent(user, basePath, notificationsDto);

                                //verify the emailContent for validness before sendig email.

                                if(emailContent !=null && emailContent.trim().length()>0 && !emailContent.contains("Due to internal server problems your request could not be completed.") && emailContent.contains("Wayne Jones") ){
                                    // send email
                                    String subject = "Prep511 Announcement: "+notificationsDto.getTitle();
                                    sendAnnouncementEmail(pro, basePath, user, emailContent, subject);

                                }
                                else{
                                    // send an email or notification to ven regarding the exception.
                                    // continue for other users.
                                    continue;
                                }

                            }
                        }
                    }


                } catch (Exception e) {
                    // TODO: handle exception
                    e.printStackTrace();
                }
            }
            /*if(title == null || title.equals("")){
                ActionMessage message = new ActionMessage("NotificationForm.title.required");
                messages.add("NotificationForm.title.required", message);//jsp
                saveMessages(request, messages);
                return mapping.findForward("NotificationForm.title.required");
            } */

            content = content.replace("\n", "<br />");
            notificationsForm.set("path",filePath);

            notificationsDto.setTitle(title);
            notificationsDto.setContent(content);
            notificationsDto.setFromDate(DateUtils.convertDateFormat(fromDate, "MMM dd, yyyy", "yyyy-MM-dd HH:mm:ss", false) );
            notificationsDto.setToDate(DateUtils.convertDateFormat(toDate, "MMM dd, yyyy", "yyyy-MM-dd HH:mm:ss", false) );
            notificationsDto.setUrl(webPath);
            notificationsDto.setCreatedBy(user);
            notificationsDto.setLastModifiedBy(user);
            notificationsDto.setNotifyUsersFlag(notifyUsers);
            notificationsDto = notificationsDao.insertRecord(notificationsDto);

            if (notificationsDto.getNotificationsId() > 0 ) {
                //forward to notificationsList.jsp
                // reload notifications
                ServletContext application = this.getServlet().getServletContext();
                new NotificationsLoader().reloadNotifications(application);

                return mapping.findForward("success");
            } else {
                // forward display VendorForm
                // Add action message also
                //record insertion failed.
                ActionMessage message = new ActionMessage("request.process.failed");// resources.properties
                messages.add("request.process.failed", message);//jsp
                saveMessages(request, messages);
                return mapping.findForward(forwardName);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            log.error(e.getMessage());
            return mapping.findForward(forwardName);
        }
    }


jsp page code is and it is a child page ..

<html:form action="processNotifications.do?processName=saveNotificationsFormDetails" styleId="NotificationForm" method="POST" enctype="multipart/form-data"> 

            <%-- <html:form action="/processNotifications" styleId="NotificationForm" method="POST" enctype="multipart/form-data"> --%> 

                <table width="100%" border="0" cellspacing="6" cellpadding="1" class="tabcont">
                  <tr>
                    <td align="right">Title<span style="color:red">*</span></td>
                    <td>
                    <html:text property="title" styleClass="registration_form_text1"></html:text>
                    </td>
                  </tr>

                  <tr>
                    <td align="right">Content<span style="color:red">*</span></td>
                    <td>
                        <html:textarea property="content" styleClass="registration_form_textArea"></html:textarea>
                    </td>
                  </tr>

                  <tr>
                    <td align="right">Url</td>
                    <td>
                        <html:file property="url" />
                   <%--<html:text property="url" styleClass="registration_form_text"></html:text> --%>
                    </td>
                  </tr>

                  <tr>
                    <td align="right">From Date<span style="color:red">*</span></td>
                    <td><html:text property="fromDate" styleClass="registration_form_text1" styleId="fromDate" style="autocomplete:off"></html:text>
                    </td>
                  </tr>

                  <tr>
                    <td align="right">To Date<span style="color:red">*</span></td>
                    <td>                   
                        <html:text property="toDate" styleClass="registration_form_text1" styleId="toDate" style="autocomplete:off"></html:text>
                    </td>
                  </tr>

                  <tr>
                    <td align="right">&nbsp;</td>
                    <td>                   
                        <html:checkbox property="notifyUsersFlag" styleId="notifyUsersFlag" ></html:checkbox>&nbsp; Notify All Users.
                    </td>
                  </tr>
                    <tr>
                    <td colspan="2">&nbsp;</td>
                  </tr>
                  <tr>
                  <tr>
                  <td></td>
                    <td colspan="2"  align="left">
                   <%--  <input type="button" value="Save" onclick="submitNotificationForm();">  --%>
                   <input type="submit" value="Save">
                    </td>
                  </tr>               
                  </table>               
              </html:form>

the required ajax call for calling this page is in parent class and the code for this is ........

function displayNotificationForm(){
        var basePath='<%=basePath%>';
        j("#notificationFormDetails").html(waitImage);
        j('#notificationFormDetails').dialog('open');
        j('#notificationFormDetails').dialog('option', 'title', 'Add Home Page Announcement');

        j.ajax({
        type : "GET",
        url : "notifications.do?processName=displayNotificationsFormDetails",
        success : function(data) {
             j("#notificationFormDetails").html(data);
        },
        error : function(jqXHR, textStatus) {
            alert("error" + textStatus);
            j("#notificationFormDetails").dialog('close');
        }

        });
    }
动作类
公共ActionForward saveNotificationsFormDetails(ActionMapping映射、ActionForm表单、,
HttpServletRequest请求,HttpServletResponse响应){
DynaValidatorActionForm notificationsForm=(DynaValidatorActionForm)表单;
log.info(“在saveNotificationsFormDetails方法中的NotificationsManagementAction”);
String forwardName=“失败”;
字符串filePath=null;
字符串webPath=null;
FileOutputStream outputStream=null;
ActionMessages=新的ActionMessages();
User=null;
尝试
{
int userId=0;
HttpSession session=request.getSession(false);
if(会话!=null)
{
user=(user)session.getAttribute(“用户”);
如果(用户!=null)
{
if(user.isAdmin()==true)
{
userId=user.getPrimaryKey();
log.info(“用户是管理员”);
}
其他的
{
log.info(“用户无权访问此页面”);
返回mapping.findForward(“userrestricted”);
}
}
其他的
{
返回mapping.findForward(“登录”);
}
}
log.info(“用户id-“+userId+”--正在请求通知详细信息。”);
NotificationsDto NotificationsDto=新的NotificationsDto();
NotificationsDao NotificationsDao=NotificationsDao.getInstance();
字符串标题=(字符串)notificationsForm.get(“标题”);
字符串内容=(字符串)notificationsForm.get(“内容”);
//正在将文件上载到通知目录。
FormFile url=(FormFile)notificationsForm.get(“url”);
字符串fileName=url.getFileName();
filePath=getServlet().getServletContext().getRealPath(“”+“/notifications/”+fileName;
if(fileName!=null&&!fileName.equals(“”){
outputStream=新文件outputStream(新文件(文件路径));
write(url.getFileData());
//获取上载文件的web路径
//字符串basePath=request.getScheme()+“:/”+request.getServerName()+“:”+request.getServerPort()+request.getContextPath();
webPath=request.getContextPath()+“/notifications/”+url.getFileName();
}
String fromDate=(String)notificationsForm.get(“fromDate”);
String toDate=(String)notificationsForm.get(“toDate”);
String notifyUsersFlag=(String)notificationsForm.get(“notifyUsersFlag”);
布尔notifyUsers=(notifyUsersFlag!=null&¬ifyUsersFlag.trim().length()>0&((notifyUsersFlag.trim().equals(“on”))| |(notifyUsersFlag.trim().equals(“true”))?true:false;
//如果用户单击通知复选框,则编写用于向用户发送邮件的代码
如果(通知用户){
//NotificationEmailDispatcherJob();
UserDao UserDao=newuserdao();
试一试{
Properties pro=新属性();
InputStream in=PropertiesFileReader.getInstance().getInputStreamInstance(“mail.properties”);
预加载(in);
字符串basePath=pro.getProperty(“basePath”);
//算法步骤:
/*
*步骤1:获取所有仍然可以显示的通知。
*步骤2:如果通知大小>0,请执行步骤(3、4、5),否则停止处理。
*步骤3:在prep511中注册并激活所有用户。
*步骤4:为每个用户准备要发送的电子邮件内容。
*步骤5:验证电子邮件内容并将其发送给用户。
*/ 
//获取仍可显示的所有通知。
列表通知=NotificationsDao.getInstance().getDisplayNotifications();
if(通知!=null&¬ifications.size()>0){
List activatedUsers=userDao.getAllUsersForanouncement();
for(迭代器迭代器=通知。迭代器();迭代器
.hasNext();){
notificationsDto=iterator.next();
迭代器usersIterator=activatedUsers.Iterator();
while(usersIterator.hasNext()){
user=usersIterator.next();
字符串emailContent=getEmailContent(用户、基本路径、通知SDTO);
//发送电子邮件之前,请验证电子邮件内容的有效性。
如果(emailContent!=null&&emailContent.trim().length()>0&&emailContent.contains(“由于内部服务器问题,您的请求无法完成”)&&emailContent.contains(“Wayne Jones”)){
//发送电子邮件
String subject=“Prep511公告:”+notificationsDto.getTitle();
sendAnnouncementEmail(pro、basePath、用户、emailContent、主题);