我在验证Struts 1.x时遇到了一些问题。我正在Ajax调用上打开一个JSP页面。在这个JSP页面(打开的弹出窗口)中,如果我想要像用户标题那样进行一些验证等,并且在提交表单时我直接将详细信息提交给所需的操作类方法。提交时我没有使用Ajax提交。我也试过Ajax提交,但它对我不起作用。下面的代码是我的动作类方法和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"> </td>
<td>
<html:checkbox property="notifyUsersFlag" styleId="notifyUsersFlag" ></html:checkbox> Notify All Users.
</td>
</tr>
<tr>
<td colspan="2"> </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');
}
});
}