日期:2014-05-16 浏览次数:20423 次
import org.apache.struts2.interceptor.ServletResponseAware; public class StudentAction extends ActionSupport implements ServletResponseAware{ private javax.servlet.http.HttpServletResponse response; public String addStudent() throws IOException{ response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8");//防止弹出的信息出现乱码 PrintWriter out = response.getWriter(); studentDAO=new StudentDAO(); flag=studentDAO.addStudent(stu); if(flag){ out.print("<script>alert('添加成功!')</script>"); out.print("<script>window.location.href='stuaction.action'</script>"); out.flush(); out.close(); return null; }else{ return "error"; } } } // 获得HttpServletResponse对象 public void setServletResponse(HttpServletResponse response) { this.response = response; }
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%> <%@ taglib prefix="s" uri="/struts-tags"%> <html> <body> //fielderror <s:if test="hasFieldErrors()"> <s:iterator value="fieldErrors"> <s:iterator value="value" status="statu"> //field的错误消息是List 因此还需要迭代一次 <s:set name="index" value="#statu.index"/> <s:set name="msg" value="#msg==null?'':#msg+ value.get(#request.index).toString()+'///n'"/>//关键代码 </s:iterator> </s:iterator> </s:if> //actionerror <s:if test="hasActionErrors()"> <s:iterator value="actionErrors"> <script language="JavaScript"> alert("<s:property escape="false"/>") </script> </s:iterat