项目处于招标期间,所以比较清闲,于是乎有了对自己知识总结的想法,又于是乎经历了.......
下载把重要代码贴出来给需要的同仁们参考参考(只供菜鸟参考,因为我也很菜)
首先是web.xml配置文件
<!--注释的为oscache缓存-->
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置Spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value> WEB-INF/classes/com/jack/user/config/dao.xml WEB-INF/classes/com/jack/user/config/servicesContext.xml </param-value> </context-param> <!-- 配置Struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.FilterDispatcher </filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置DWR --> <!-- dwr的servlet配置 add by jie.su--> <servlet> <servlet-name>dwr</servlet-name> <!-- org.directwebremoting.spring.DwrSpringServlet --> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> <!--开发时设为true,生产时设为false--> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>exposeInternals</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>Validate</servlet-name> <servlet-class>com.jack.user.common.Validate</servlet-class> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>Validate</servlet-name> <url-pattern>/servlet/Validate</url-pattern> </servlet-mapping> <!-- dwr的servlet配置 --> <!-- 配置Spring的监听 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--osehcache缓存首页配置 begin --> <!--<filter> <filter-name>CacheFilter</filter-name> <filter-class> com.opensymphony.oscache.web.filter.CacheFilter </filter-class> <init-param> <param-name>time</param-name> <param-value>600</param-value> </init-param> <init-param> <param-name>scope</param-name> <param-value>session</param-value> </init-param> </filter> <filter-mapping> <filter-name>CacheFilter</filter-name> <url-pattern>*.jsp</url-pattern> </filter-mapping> --><!-- osehcache缓存首页配置 end --> </web-app>
dwr配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd"> <dwr> <allow> <convert match="java.lang.StackTraceElement" converter="bean" /> <convert match="java.lang.Exception" converter="exception"></convert> <create creator="none" javascript="DWRAction"> <param name="class" value="org.directwebremoting.struts2.DWRAction" /> <include method="execute" /> </create> <convert converter="bean" match="com.opensymphony.xwork2.ValidationAwareSupport" /> <convert converter="bean" match="org.directwebremoting.struts2.ActionDefinition"> <param name="include" value="namespace,action,method,executeResult" /> </convert> <convert converter="bean" match="org.directwebremoting.struts2.AjaxResult" /> <!-- 相应的Struts2的Action --> <convert match="com.jack.user.action.DWRDemoAction" converter="bean" /> <convert match="com.jack.user.pojo.*" converter="bean"/> <convert match="com.jack.user.action.*" converter="bean"/> <!-- 用户名验证 --> <create javascript="validator" creator="new"> <param name="class" value="com.jack.user.model.impl.ValidatorUser"> </param> </create> <create javascript="testDwr" creator="new"> <param name="class" value="com.jack.user.model.impl.TestDwr"> </param> </create> <!-- 商品拼音取码 --> </allow> </dwr>
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.enable.DynamicMethodInvocation" value="false" /> <constant name="struts.devMode" value="false" /> <package name="user" extends="struts-default" namespace="/user"> <interceptors> <interceptor name="doInterceptor" class="doInterceptor"></interceptor> </interceptors> <!-- 用户登录 --> <action name="userLogin" class="loginAction" method="userLogin"> <interceptor-ref name="doInterceptor"></interceptor-ref> <interceptor-ref name ="params" /> <interceptor-ref name ="static-params" /> <result name="success" type="redirect">/Merchandise.jsp</result> <result name="noUser">/login.jsp</result> <result name="login">/login.jsp</result> </action> <!-- 普通用户注册用户名和密码,不做具体操作,只是把用户名和密码放在会话中 --> <action name="saveAccount" class="loginAction" method="saveAccount"> <result name="success">/userInfor.jsp</result> <result name="error">/register.jsp</result> </action> <!-- 普通用户注册完成 --> <action name="register" class="loginAction" method="userRegister"> <result name="success">/welcome.jsp</result> <result name="error">/register.jsp</result> </action> <!-- 企业用户注册用户名和密码,不做具体操作,只是把用户名和密码放在会话中 --> <action name="saveEnAccount" class="loginAction" method="saveEnAccount"> <result name="success">/enInfor.jsp</result> <result name="error">/enRegister.jsp</result> </action> <!-- 企业用户完成注册 --> <action name="enRegister" class="loginAction" method="enRegister"> <result name="success">/welcome.jsp</result> <result name="error">/enRegister.jsp</result> </action> <action name="fileUpload" class="fileUpload"> <interceptor-ref name="fileUpload"> <param name="allowedTypes"> image/bmp,image/png,image/gif,image/jpeg,image/jpg </param> </interceptor-ref> <interceptor-ref name="defaultStack"/> <result name="input">/fileupload.jsp</result> <result name="success">/showUpload.jsp</result> </action> <action name="dwrDemoAction" class="dwrDemoAction"> </action> <action name="merchandiseAction" class="merchandiseAction" method="saveMer"> <result name="success">/Merchandise.jsp</result> <result name="error">/error.jsp</result> </action> <action name="merchandiseSearchAction" class="merchandiseSearchAction"> <result name="success">/dt.jsp</result> <result name="error">/error.jsp</result> </action> <action name="merchandiseDeleteAction" class="merchandiseDeleteAction"> <result name="success">/Merchandise.jsp</result> <result name="error">/error.jsp</result> </action> <action name="merchandiseFindByIdAction" class="merchandiseFindByIdAction"> <result name="success">/Merchandise.jsp</result> <result name="error">/error.jsp</result> </action> <action name="merchandiseUpdateAction" class="merchandiseUpdateAction"> <result name="success">/Merchandise.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
分页标签
package com.jack.user.common;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class PagTag extends BodyTagSupport {
private String uri;
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
return super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return super.doEndTag();
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
int pageNow = 1;
int pageCount = 1;
int count = 1;
String temp = (String) request.getAttribute("pageNow");
if (temp != null)
pageNow = Integer.parseInt(temp);
temp=(String)request.getAttribute("pageCount");
if (temp != null)
pageCount=Integer.parseInt(temp);
temp=(String)request.getAttribute("count");
if (temp != null)
count=Integer.parseInt(temp);
temp=this.buildPage(uri, pageNow, pageCount, count);
try {
this.pageContext.getOut().println(temp);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return this.SKIP_BODY;
}
public String buildPage(String uri,int pageNow,int pageCount,int count)
{
StringBuffer sb = new StringBuffer();
sb.append("<TABLE border=\"0\" width=\"100%\">");
sb.append("<TR>");
sb.append("<TD align=\"right\" colspan=\"5\"><a href=\"#\" onclick=\"first();\">首页</a>|");
if(pageNow<pageCount)
sb.append("<a href=\"#\" onclick=\"next("+(pageNow+1)+");\">下一页</a>|");
else
sb.append("<font size=\"2\">下一页 </font>|");
if(pageNow>1)
sb.append("<a href=\"#\" onclick=\"next("+(pageNow-1)+");\">上一页</a>|");
else
sb.append("<font size=\"2\">上一页</font>|");
sb.append("<a href=\"#\" onclick=\"end("+pageCount+");\">尾页</a>|");
sb.append("<font size=\"2\">"+pageNow+"/"+pageCount+" 共:"+count+"条</font></TD>");
sb.append("</TR>");
sb.append("</TABLE>");
return sb.toString();
}
查询Action
package com.jack.user.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.jack.user.model.IMerchandiseServices;
import com.opensymphony.xwork2.ActionSupport;
public class MerchandiseSearchAction extends ActionSupport implements
ServletRequestAware {
private static final long serialVersionUID = 1351082640259559948L;
private List list;
private int pageNow;
private javax.servlet.http.HttpServletRequest request;
@SuppressWarnings("unused")
private IMerchandiseServices merchandiseServices;
public void setMerchandiseServices(IMerchandiseServices merchandiseServices) {
this.merchandiseServices = merchandiseServices;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public String execute() {
String result = "";
try {
int pageSize=5;
int pageNow=1;
int pageNowStr=this.getPageNow();
if(pageNowStr!=1&&pageNowStr!=0)
{
pageNow=pageNowStr;
}
list = merchandiseServices.searchAllMerchandise(pageSize,pageNow);
int pageCount=merchandiseServices.getPageCount(pageSize);
int count=merchandiseServices.getCount();
request.setAttribute("list",list);
request.setAttribute("pageNow",pageNow+"");
request.setAttribute("pageCount",pageCount+"");
request.setAttribute("count",count+"");
result = "success";
} catch (Exception e) {
// TODO: handle exception
result = "error";
}
return result;
}
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
public int getPageNow() {
return pageNow;
}
public void setPageNow(int pageNow) {
this.pageNow = pageNow;
}
}
}
页面提交由form表单提交和DWRUtilAction两种提交方式,现在看到的完全由DWRUtilAction方式提交
function saveMerchandise() { if(checkNonEmpty(document.getElementById('name'),'名称')) { if(checkNonEmpty(document.getElementById('manufacturer'),'厂商')) { DWRActionUtil.execute({namespace:'/user',action:'merchandiseAction',executeResult:'false'},document.mer,searchMerAll); } } } function searchMerAll() { DWRActionUtil.execute({namespace:'/user',action:'merchandiseSearchAction',executeResult:'true'},{},cal); } function cal(data) { document.getElementById('name').value=''; document.getElementById('manufacturer').value=''; document.getElementById('li').innerHTML=data; } function dele(id) { DWRActionUtil.execute({namespace:'/user',action:'merchandiseDeleteAction',executeResult:'false'},{id:id},searchMerAll); } function searchById(id) { DWRActionUtil.execute({namespace:'/user',action:'merchandiseFindByIdAction',executeResult:'false'},{id:id},fd); } function fd(data) { document.getElementById('id').value=data.merchandise.id document.getElementById('name').value=data.merchandise.name document.getElementById('manufacturer').value=data.merchandise.manufacturer } function cl() { document.getElementById('id').value=""; document.getElementById('name').value=""; document.getElementById('manufacturer').value=""; } function updateMerchandise() { if(document.getElementById('id').value==''||document.getElementById('id').value=='undefined'){ alert('请选择要修改的信息'); return; }else { if(checkNonEmpty(document.getElementById('name'),'名称')) { if(checkNonEmpty(document.getElementById('manufacturer'),'厂商')) { var id=document.getElementById('id').value; var name=document.getElementById('name').value; var manufacturer= document.getElementById('manufacturer').value; DWRActionUtil.execute({namespace:'/user',action:'merchandiseUpdateAction',executeResult:'false'},{id:id,name:name,manufacturer:manufacturer},searchMerAll); } } } } function searchReAll() { var allInput = document.getElementsByName("mid"); var loopTime = allInput.length; for(i = 0;i < loopTime;i++) { if(allInput[i].type == "checkbox") { if(allInput[i].checked==true) { allInput[i].checked=false; }else { allInput[i].checked=true; } } } } function batchDel() { var allInput = document.getElementsByName("mid"); var loopTime = allInput.length; var valuetext=""; for(i=0,num=0;i<loopTime;i++) { if(allInput[i].checked) { valuetext+=(valuetext=="")?allInput[i].value:","+allInput[i].value; num++; } } if(num>0) { alert(valuetext); } else { alert('请选择所要删除商品!'); } }
由于代码比较多这里就不一一贴出来,如果哪位朋友需要可留Email