日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Struts2之Crud综合实例

發布時間:2024/7/23 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Struts2之Crud综合实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文是Struts2的綜合實例,主要包含以下功能

  • 添加,刪除,修改,查詢用戶
  • 上傳,下載圖片
  • 攔截器實現登陸功能
  • 驗證器檢查輸入
  • 下載圖片功能以前沒有實現過,步驟如下

    • 在類中增加兩個屬性
    //文件下載private InputStream inputStream;private String imageFileName;
    • 下載方法實現
    public String download(){path = ServletActionContext.getRequest().getParameter("path");filename = ServletActionContext.getRequest().getParameter("filename");String storePath = ServletActionContext.getServletContext().getRealPath("/files");try {inputStream = new FileInputStream(storePath+"\\"+path+"\\"+filename);} catch (FileNotFoundException e) {e.printStackTrace();}return SUCCESS;}

    配置struts.xml

    <action name="download" class="cn.itcast.domain.User" method="download"><interceptor-ref name="mydefaultstack"></interceptor-ref><result type="stream" name="success"><param name="contentType">application/octet-stream</param><param name="inputStream">inputStream</param><!-- 輸入是對應的動作類中的那個字段 --><param name="contentDisposition">attachment;filename=${filename}</param><!-- 要下載的文件名 --></result><result name="login">/login.jsp</result></action>

    JSP頁面實現

    <c:url value="/user/download" var="url"><c:param name="path" value="${user.path}"></c:param><c:param name="filename" value="${user.filename}"></c:param></c:url><a href="${url}">下載</a>

    登陸驗證功能

    定義攔截器類

    package cn.itcast.interceptor;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor;public class PermissionInterceptor implements Interceptor {public void destroy() {}public void init() {}public String intercept(ActionInvocation invocation) throws Exception {HttpSession session = ServletActionContext.getRequest().getSession();Object obj = session.getAttribute("user");if(obj==null){return "login";}else{return invocation.invoke();}}}

    struts.xml中的配置

    <package name="mydefault" extends="struts-default"><interceptors><interceptor name="permissionInterceptor" class="cn.itcast.interceptor.PermissionInterceptor"></interceptor><interceptor-stack name="mydefaultstack"><interceptor-ref name="defaultStack"></interceptor-ref><interceptor-ref name="permissionInterceptor"></interceptor-ref></interceptor-stack></interceptors></package>

    在其他Action中使用攔截

    <interceptor-ref name="mydefaultstack"></interceptor-ref>

    條件查詢實現

    JSP頁面

    <td><s:form action="user_queryCondition" namespace="/user"><s:textfield name="username" label="用戶名"></s:textfield><s:select list="#{'0':'','1':''}" label="性別" name="sex" headerKey="" headerValue="請選擇"></s:select><s:select label="學歷" name="education" list="#{'研究生':'研究生','本科':'本科','專科':'專科','高中':'高中'}" headerKey="" headerValue="請選擇"></s:select><s:submit value="查詢"></s:submit></s:form></td>

    javabean方法實現

    public String queryCondition(){List<User> users =s.findUserByCondition(this);ActionContext.getContext().put("users", users);// #usersreturn SUCCESS;}

    service層方法實現

    public List<User> findUserByCondition(User user) {boolean ok1 = true;boolean ok2 = true;boolean ok3 = true;StringBuffer sb = new StringBuffer("where 1=1 ");if(user.getUsername()!=null&&!user.getUsername().equals("")){ok1 = false;sb.append(" and username like '%"+user.getUsername()+"%' ");}if(user.getSex()!=null&&!user.getSex().equals("")){ok2 = false;sb.append(" and sex='"+user.getSex()+"'");}if(user.getEducation()!=null&&!user.getEducation().equals("")){ok3 = false;sb.append(" and education='"+user.getEducation()+"'");}boolean conditionOk = ok1&&ok2&&ok3;//如果為false,說明至少有一個查詢條件if(conditionOk){ // System.out.println("沒有查詢條件"); // return null;return dao.findUsersByCondition(null);}else{ // System.out.println("有查詢條件"); // System.out.println(sb.toString()); // return null;return dao.findUsersByCondition(sb.toString());}}

    其他增刪改查詳細實例參見github源碼

    ssh/Struts2Crud at master · whuhan2013/ssh

    效果如下

    總結

    以上是生活随笔為你收集整理的Struts2之Crud综合实例的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。