此係列博文基於同一個項目已上傳至github 傳送門javascript
JavaWeb_(Struts2框架)Struts建立Action的三種方式 傳送門css
JavaWeb_(Struts2框架)struts.xml核心配置、動態方法調用、結果集的處理 傳送門html
JavaWeb_(Struts2框架)Log4j的配置以及解決中文亂碼 傳送門前端
JavaWeb_(Struts2框架)參數傳遞之接收參數與傳遞參數 傳送門java
JavaWeb_(Struts2框架)Ognl小案例查詢帖子 傳送門jquery
JavaWeb_(Struts2框架)Action中struts-default下result的各類轉發類型 傳送門git
JavaWeb_(Struts2框架)攔截器interceptor 傳送門github
建立paste帖子表web
CREATE TABLE `strutstest`.`paste` ( `id` VARCHAR(50) NOT NULL, `answer` INT NULL, `offer` INT NULL, `title` VARCHAR(300) NULL, `content` VARCHAR(300) NULL, PRIMARY KEY (`id`));
添加假數據sql
INSERT INTO `strutstest`.`paste` (`id`, `answer`, `offer`, `title`, `content`) VALUES ('1', '1', '1', '1title', '1content'); INSERT INTO `strutstest`.`paste` (`id`, `answer`, `offer`, `title`, `content`) VALUES ('2', '2', '2', '2title', '2content'); INSERT INTO `strutstest`.`paste` (`id`, `answer`, `offer`, `title`, `content`) VALUES ('3', '3', '3', '3title', '3content'); INSERT INTO `strutstest`.`paste` (`id`, `answer`, `offer`, `title`, `content`) VALUES ('4', '4', '4', '4title', '4content');
建立查詢帖子
Dao層
package com.Gary.dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.Gary.domain.Paste; import com.yl.lain.utils.C3p0DataSourceUtils; public class PasteDao { public List<Paste> findAllPaste() throws SQLException { QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource()); String sql = "select * from paste"; return runner.query(sql, new BeanListHandler<Paste>(Paste.class)); } }
Domain層
package com.Gary.domain; public class Paste { private String id; private Integer ansnum; private Integer offer; private String title; private String content; public String getId() { return id; } public void setId(String id) { this.id = id; } public Integer getAnsnum() { return ansnum; } public void setAnsnum(Integer ansnum) { this.ansnum = ansnum; } public Integer getOffer() { return offer; } public void setOffer(Integer offer) { this.offer = offer; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
Service層
package com.Gary.service; import java.sql.SQLException; import java.util.List; import com.Gary.dao.PasteDao; import com.Gary.domain.Paste; public class PasteService { public List<Paste> findAllPaste() throws SQLException { PasteDao pasteDao = new PasteDao(); return pasteDao.findAllPaste(); } }
Web層
package com.Gary.web; import java.util.List; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.Gary.domain.Paste; import com.Gary.service.PasteService; public class PasteAction extends ActionSupport{ public String getAllPaste() throws Exception { PasteService pasteService = new PasteService(); List<Paste> pasteList = pasteService.findAllPaste(); ActionContext.getContext().put("pasteList", pasteList); System.out.println(pasteList); return "index"; } }
struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- name:配置包名 namespace:給action的訪問路徑定義一個命名空間 --> <package name="MyPackage" namespace="/" extends="struts-default"> <!-- 增長動態方法調用的安全性 --> <global-allowed-methods>regex:.*</global-allowed-methods> <!-- action:配置action類 name:決定了action訪問的資源名稱 servlet:url-pattern class:action的完整類名 method:指定調用action中的哪一個方法來去處理請求 --> <action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}"> <!-- 默認爲轉發 redirect設置爲重定向 --> <result name="success" type="redirect">/index.html</result> <!-- 默認爲轉發 --> <result name="error">/login.jsp</result> </action> <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute"> </action> <action name="LoginActionImpl_*" class="com.Gary.web.ImplAction" method="{1}"> <!-- 轉發到LoginActionDefault --> <result name="defaultAction" type="chain">LoginActionDefault</result> <!-- 重定向到Action(LoginAction_*) --> <result name="toLogin" type="redirectAction"> <param name="actionName">LoginAction_login</param> <param name="username">${username}</param> <param name="password">${password}</param> </result> </action> </package> <package name="PastePackage" namespace="/" extends="struts-default"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="PasteAction_*" class="com.Gary.web.PasteAction" method="{1}"> <result name="index">/index.jsp</result> </action> </package> </struts>
能夠看到查詢到4條帖子假數據,這4條數據已經經過ActionContext.getContext().put("pasteList", pasteList)存放到ActionContext域中。
接下來經過Ognl與Struct標籤,將帖子在前端動態的顯示出來
<s:iterator value="pasteList" var="paste"></s:iterator> <s:property value="ansnum" /> 若是要訪問的數據是包裝類型,咱們要加# <s:property value="#user.username"/> <s:if test="ansnum%2==0"></s:if> <s:else></s:else>
在index.jsp首頁頭部引入JSTL標籤庫
<%@taglib uri="/struts-tags" prefix="s"%>
循環數據庫中帖子表全部的數據
<s:iterator value="pasteList" var="paste"></s:iterator>
<s:property value="ansnum" />
<s:iterator value="pasteList" var="paste"> <div class="dvques"> <div class="quesCount"> <div class="count"><s:property value="id"/></div> <div class="ques">回答數</div> </div> <div class="quesContent"> <div class="quesTitle"> <s:property value="offer"/> <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="title"/></span> </div> <div class="qContent"><s:property value="content"/></div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> 張大值 <div class="liulan">瀏覽(9) 30分鐘前</div> </div> </div> </div> </div> </s:iterator>
經過訪問http://localhost:8080/StrutsForum_Login/PasteAction_getAllPaste能夠將數據庫Paste帖子表的數據展現到首頁index.jsp中
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/index.css"> </head> <div class="dvhead"> <div class="dvlogo"> <a href="index.html">你問我答</a> </div> <div class="dvsearch">10秒鐘註冊帳號,找到你的同窗</div> <div class="dvreg"> 已有帳號,當即 <a href="login.html">登陸</a> </div> </div> <div class="dvContent"> <div class="dvquesleft"> <div class="dvqstitle"> <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">問答</span> <span class="back"><ab href="">《《返回上一頁</a></span> </div> <div class="dvtabhead"> <div class="tabheads tabcurrent">所有問題</div> <div class="tabheads">個人問題</div> <div class="tabheads">關注問題</div> <div class="tabheads">問題標籤</div> </div> <div class="tabContent"> <div class="dvtags"> <a class="curenttag">待解決</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解決</a> </div> <div class="tab"> <s:iterator value="pasteList" var="paste"> <div class="dvques"> <div class="quesCount"> <div class="count"><s:property value="id"/></div> <div class="ques">回答數</div> </div> <div class="quesContent"> <div class="quesTitle"> <s:property value="offer"/> <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="title"/></span> </div> <div class="qContent"><s:property value="content"/></div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> 張大值 <div class="liulan">瀏覽(9) 30分鐘前</div> </div> </div> </div> </div> </s:iterator> </div> <div class="tab hidden">2</div> <div class="tab hidden">3</div> <div class="tab hidden">4</div> </div> </div> <div class="dvquesright"> <div> <buton class="btnques" onclick="location.href='add.html'">提個問題</buton> </div> <div class="dvorder"> <div class="orderTitle">專家排行榜</div> <div class="users"> <image class="userface" src="images/0.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/1.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/2.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/3.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/4.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/5.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/6.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> </div> </div> </div> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { $(".tabheads").click(function() { $(".tabheads").removeClass("tabcurrent").eq($(this).index()).addClass("tabcurrent"); $(".tab").hide().eq($(this).index()).show(); }); }); </script> <body> </body> </html>
若是咱們須要放置的是一個對象
在PasteAction.java中放置一個User對象
User user = new User(); user.setUsername("Gary"); user.setPassword("111"); ActionContext.getContext().put("user", user);
前臺經過<s:property value="#user.username"/>訪問user中的對象
<div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> <s:property value="#user.username"/> <div class="liulan">瀏覽(9) 30分鐘前</div> </div> </div>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link rel="stylesheet" type="text/css" href="css/index.css"> </head> <div class="dvhead"> <div class="dvlogo"> <a href="index.html">你問我答</a> </div> <div class="dvsearch">10秒鐘註冊帳號,找到你的同窗</div> <div class="dvreg"> 已有帳號,當即 <a href="login.html">登陸</a> </div> </div> <div class="dvContent"> <div class="dvquesleft"> <div class="dvqstitle"> <image class="imgbean" src="images/bean.jpg"> <span class="qsTitle">問答</span> <span class="back"><ab href="">《《返回上一頁</a></span> </div> <div class="dvtabhead"> <div class="tabheads tabcurrent">所有問題</div> <div class="tabheads">個人問題</div> <div class="tabheads">關注問題</div> <div class="tabheads">問題標籤</div> </div> <div class="tabContent"> <div class="dvtags"> <a class="curenttag">待解決</a><span class="line"></span><a>高分</a><span class="line"></span><a>新回答</a><span class="line"></span><a>已解決</a> </div> <div class="tab"> <s:iterator value="pasteList" var="paste"> <div class="dvques"> <div class="quesCount"> <div class="count"><s:property value="id"/></div> <div class="ques">回答數</div> </div> <div class="quesContent"> <div class="quesTitle"> <s:property value="offer"/> <image src="images/bean.jpg" class="bean"> <span class="spanques"><s:property value="title"/></span> </div> <div class="qContent"><s:property value="content"/></div> <div class="tags"> <span class="tag">excel</span><span class="tag">程序</span> </div> <div class="quesUser"> <image src="images/0.gif" class="imguser" /> <div class="userName"> <s:property value="#user.username"/> <div class="liulan">瀏覽(9) 30分鐘前</div> </div> </div> </div> </div> </s:iterator> </div> <div class="tab hidden">2</div> <div class="tab hidden">3</div> <div class="tab hidden">4</div> </div> </div> <div class="dvquesright"> <div> <buton class="btnques" onclick="location.href='add.html'">提個問題</buton> </div> <div class="dvorder"> <div class="orderTitle">專家排行榜</div> <div class="users"> <image class="userface" src="images/0.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/1.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/2.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/3.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/4.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/5.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> <div class="users"> <image class="userface" src="images/6.gif" /> <div class="dvuser"> <div class="userTitle">陳有龍</div> <div class="userdeital">大牛6級 豆:14006</div> </div> </div> </div> </div> </div> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript"> $(function() { $(".tabheads").click(function() { $(".tabheads").removeClass("tabcurrent").eq($(this).index()).addClass("tabcurrent"); $(".tab").hide().eq($(this).index()).show(); }); }); </script> <body> </body> </html>
web層
package com.Gary.web; import java.util.List; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.Gary.domain.Paste; import com.Gary.domain.User; import com.Gary.service.PasteService; public class PasteAction extends ActionSupport{ public String getAllPaste() throws Exception { PasteService pasteService = new PasteService(); List<Paste> pasteList = pasteService.findAllPaste(); User user = new User(); user.setUsername("Gary"); user.setPassword("111"); ActionContext.getContext().put("pasteList", pasteList); ActionContext.getContext().put("user", user); System.out.println(pasteList); return "index"; } }
service層
package com.Gary.service; import java.sql.SQLException; import java.util.List; import com.Gary.dao.PasteDao; import com.Gary.domain.Paste; public class PasteService { public List<Paste> findAllPaste() throws SQLException { PasteDao pasteDao = new PasteDao(); return pasteDao.findAllPaste(); } }
domain層
package com.Gary.domain; public class Paste { private String id; private Integer ansnum; private Integer offer; private String title; private String content; public String getId() { return id; } public void setId(String id) { this.id = id; } public Integer getAnsnum() { return ansnum; } public void setAnsnum(Integer ansnum) { this.ansnum = ansnum; } public Integer getOffer() { return offer; } public void setOffer(Integer offer) { this.offer = offer; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
dao層
package com.Gary.dao; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import com.Gary.domain.Paste; import com.yl.lain.utils.C3p0DataSourceUtils; public class PasteDao { public List<Paste> findAllPaste() throws SQLException { QueryRunner runner = new QueryRunner(C3p0DataSourceUtils.getDataSource()); String sql = "select * from paste"; return runner.query(sql, new BeanListHandler<Paste>(Paste.class)); } }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.devMode" value="true"></constant> <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant> <!-- name:配置包名 namespace:給action的訪問路徑定義一個命名空間 --> <package name="MyPackage" namespace="/" extends="struts-default"> <!-- 增長動態方法調用的安全性 --> <global-allowed-methods>regex:.*</global-allowed-methods> <!-- action:配置action類 name:決定了action訪問的資源名稱 servlet:url-pattern class:action的完整類名 method:指定調用action中的哪一個方法來去處理請求 --> <action name="LoginAction_*" class="com.Gary.web.UserAction" method="{1}"> <!-- 默認爲轉發 redirect設置爲重定向 --> <result name="success" type="redirect">/index.html</result> <!-- 默認爲轉發 --> <result name="error">/login.jsp</result> </action> <action name="LoginActionDefault" class="com.Gary.web.DefaultAction" method="execute"> </action> <action name="LoginActionImpl_*" class="com.Gary.web.ImplAction" method="{1}"> <!-- 轉發到LoginActionDefault --> <result name="defaultAction" type="chain">LoginActionDefault</result> <!-- 重定向到Action(LoginAction_*) --> <result name="toLogin" type="redirectAction"> <param name="actionName">LoginAction_login</param> <param name="username">${username}</param> <param name="password">${password}</param> </result> </action> </package> <package name="PastePackage" namespace="/" extends="struts-default"> <global-allowed-methods>regex:.*</global-allowed-methods> <action name="PasteAction_*" class="com.Gary.web.PasteAction" method="{1}"> <result name="index">/index.jsp</result> </action> </package> </struts>