政策查询系统总结
要求
这个系统对我来说最陌生的点在于建立树形结构
我使用了treeview控件
TreeView控件通过'nodeSelected'绑定点击事件
在进行多条件模糊查询的时候,我在写sql语句时遇到了困难,最终我采取了暴力的方式,将每个条件都遍历了出来
private QueryRunner queryRunner = new QueryRunner(DButil.getDruidDataSource()); public List<Policy> selectall() { List<Policy> policies = null; try { policies = queryRunner.query("select * from policy", new BeanListHandler<Policy>(Policy.class)); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectname(String name) { List<Policy> policies = null; name = "%" + name + "%"; Object[] prams = {name}; try { policies = queryRunner.query("select * from policy where name like ?", new BeanListHandler<Policy>(Policy.class), name); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameanddocument(String name, String document) { List<Policy> policies = null; name = "%" + name + "%"; document = "%" + document + "%"; Object[] prams = {name, document}; try { policies = queryRunner.query("select * from policy where name like ? and document like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameandorgan(String name, String organ) { List<Policy> policies = null; name = "%" + name + "%"; organ = "%" + organ + "%"; Object[] prams = {name, organ}; try { policies = queryRunner.query("select * from policy where name like ? and organ like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameandtext(String name, String text) { List<Policy> policies = null; name = "%" + name + "%"; text = "%" + text + "%"; Object[] prams = {name, text}; try { policies = queryRunner.query("select * from policy where name like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameanddocumentandorgan(String name, String document, String organ) { List<Policy> policies = null; name = "%" + name + "%"; document = "%" + document + "%"; organ = "%" + organ + "%"; Object[] prams = {name, document, organ}; try { policies = queryRunner.query("select * from policy where name like ? and document like ? and organ like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameanddocumentandtext(String name, String document, String text) { List<Policy> policies = null; name = "%" + name + "%"; document = "%" + document + "%"; text = "%" + text + "%"; Object[] prams = {name, document, text}; try { policies = queryRunner.query("select * from policy where name like ? and document like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameandorganandtext(String name, String organ, String text) { List<Policy> policies = null; name = "%" + name + "%"; organ = "%" + organ + "%"; text = "%" + text + "%"; Object[] prams = {name, organ, text}; try { policies = queryRunner.query("select * from policy where name like ? and organ like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameanddocumentandorganandtext(String name, String document, String organ, String text) { List<Policy> policies = null; name = "%" + name + "%"; document = "%" + document + "%"; organ = "%" + organ + "%"; text = "%" + text + "%"; Object[] prams = {name, document, organ, text}; try { policies = queryRunner.query("select * from policy where name like ? and document like ? and organ like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectdocument(String document) { List<Policy> policies = null; document = "%" + document + "%"; Object[] prams = {document}; try { policies = queryRunner.query("select * from policy where document like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectdocumentandorgan(String document, String organ) { List<Policy> policies = null; document = "%" + document + "%"; organ = "%" + organ + "%"; Object[] prams = {document, organ}; try { policies = queryRunner.query("select * from policy where document like ? and organ like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectdocumentandtext(String document, String text) { List<Policy> policies = null; document = "%" + document + "%"; text = "%" + text + "%"; Object[] prams = {document, text}; try { policies = queryRunner.query("select * from policy where document like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectdocumentandorganandtext(String document, String organ, String text) { List<Policy> policies = null; document = "%" + document + "%"; organ = "%" + organ + "%"; text = "%" + text + "%"; Object[] prams = {document, organ, text}; try { policies = queryRunner.query("select * from policy where document like ? and organ like ? and text = ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectorgan(String organ) { List<Policy> policies = null; organ = "%" + organ + "%"; Object[] prams = {organ}; try { policies = queryRunner.query("select * from policy where organ like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectorganandtext(String organ, String text) { List<Policy> policies = null; organ = "%" + organ + "%"; text = "%" + text + "%"; Object[] prams = {organ, text}; try { policies = queryRunner.query("select * from policy where organ like ? and text = ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selecttext(String text) { List<Policy> policies = null; text = "%" + text + "%"; Object[] prams = {text}; try { policies = queryRunner.query("select * from policy where text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selecttype(String type) { List<Policy> policies = null; type = "%" + type + "%"; Object[] prams = {type}; try { policies = queryRunner.query("select * from policy where type like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selecttypejichu() { List<Policy> policies = null; try { policies = queryRunner.query("select * from policy where type like '%基础研究与科研基地%' or type like '%基础研究%' or type like '%平台基地%'", new BeanListHandler<Policy>(Policy.class)); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selecttypeqiye() { List<Policy> policies = null; try { policies = queryRunner.query("select * from policy where type like '%企业技术进步与高新技术产业化%' or type like '%企业%' or type like '%产业%' or type like '%创新载体%'", new BeanListHandler<Policy>(Policy.class)); } catch (SQLException e) { e.printStackTrace(); } return policies; } public Policy select(String name){ Policy policy = null; try { policy = queryRunner.query("select * from policy where name = ?",new BeanHandler<Policy>(Policy.class),name); } catch (SQLException e) { e.printStackTrace(); } return policy; }
以下为整个系统代码
工程目录为
Policy.class
package bean; public class Policy { private String id; private String name; private String type; private String category; private String range; private String document; private String from; private String organ; private String viadata; private String pubdata; private String perdata; private String field; private String theme; private String keyword; private String superior; private String precursor; private String succed; private String state; private String text; private String pdf; private String redundancy; private String rank; private String policykey; private String newrank; private String year; private String newkey; private String secondtheme; private String allsum; public Policy(String id, String name, String type, String category, String range, String document, String from, String organ, String viadata, String pubdata, String perdata, String field, String theme, String keyword, String superior, String precursor, String succed, String state, String text, String pdf, String redundancy, String rank, String policykey, String newrank, String year, String newkey, String secondtheme, String allsum) { this.id = id; this.name = name; this.type = type; this.category = category; this.range = range; this.document = document; this.from = from; this.organ = organ; this.viadata = viadata; this.pubdata = pubdata; this.perdata = perdata; this.field = field; this.theme = theme; this.keyword = keyword; this.superior = superior; this.precursor = precursor; this.succed = succed; this.state = state; this.text = text; this.pdf = pdf; this.redundancy = redundancy; this.rank = rank; this.policykey = policykey; this.newrank = newrank; this.year = year; this.newkey = newkey; this.secondtheme = secondtheme; this.allsum = allsum; } public Policy() { } @Override public String toString() { return "Policy{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", type='" + type + '\'' + ", category='" + category + '\'' + ", range='" + range + '\'' + ", document='" + document + '\'' + ", from='" + from + '\'' + ", organ='" + organ + '\'' + ", viadata='" + viadata + '\'' + ", pubdata='" + pubdata + '\'' + ", perdata='" + perdata + '\'' + ", field='" + field + '\'' + ", theme='" + theme + '\'' + ", keyword='" + keyword + '\'' + ", superior='" + superior + '\'' + ", precursor='" + precursor + '\'' + ", succed='" + succed + '\'' + ", state='" + state + '\'' + ", text='" + text + '\'' + ", pdf='" + pdf + '\'' + ", redundancy='" + redundancy + '\'' + ", rank='" + rank + '\'' + ", policykey='" + policykey + '\'' + ", newrank='" + newrank + '\'' + ", year='" + year + '\'' + ", newkey='" + newkey + '\'' + ", secondtheme='" + secondtheme + '\'' + ", allsum='" + allsum + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getRange() { return range; } public void setRange(String range) { this.range = range; } public String getDocument() { return document; } public void setDocument(String document) { this.document = document; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getOrgan() { return organ; } public void setOrgan(String organ) { this.organ = organ; } public String getViadata() { return viadata; } public void setViadata(String viadata) { this.viadata = viadata; } public String getPubdata() { return pubdata; } public void setPubdata(String pubdata) { this.pubdata = pubdata; } public String getPerdata() { return perdata; } public void setPerdata(String perdata) { this.perdata = perdata; } public String getField() { return field; } public void setField(String field) { this.field = field; } public String getTheme() { return theme; } public void setTheme(String theme) { this.theme = theme; } public String getKeyword() { return keyword; } public void setKeyword(String keyword) { this.keyword = keyword; } public String getSuperior() { return superior; } public void setSuperior(String superior) { this.superior = superior; } public String getPrecursor() { return precursor; } public void setPrecursor(String precursor) { this.precursor = precursor; } public String getSucced() { return succed; } public void setSucced(String succed) { this.succed = succed; } public String getState() { return state; } public void setState(String state) { this.state = state; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getPdf() { return pdf; } public void setPdf(String pdf) { this.pdf = pdf; } public String getRedundancy() { return redundancy; } public void setRedundancy(String redundancy) { this.redundancy = redundancy; } public String getRank() { return rank; } public void setRank(String rank) { this.rank = rank; } public String getPolicykey() { return policykey; } public void setPolicykey(String policykey) { this.policykey = policykey; } public String getNewrank() { return newrank; } public void setNewrank(String newrank) { this.newrank = newrank; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } public String getNewkey() { return newkey; } public void setNewkey(String newkey) { this.newkey = newkey; } public String getSecondtheme() { return secondtheme; } public void setSecondtheme(String secondtheme) { this.secondtheme = secondtheme; } public String getAllsum() { return allsum; } public void setAllsum(String allsum) { this.allsum = allsum; } }
Type.class
package bean; public class Type { private String Type_ID; private String Type; @Override public String toString() { return "Type{" + "Type_ID='" + Type_ID + '\'' + ", Type='" + Type + '\'' + '}'; } public String getType_ID() { return Type_ID; } public void setType_ID(String type_ID) { Type_ID = type_ID; } public String getType() { return Type; } public void setType(String type) { Type = type; } public Type(String type_ID, String type) { Type_ID = type_ID; Type = type; } public Type() { } }
Policydao.class
package dao; import bean.Policy; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import util.DButil; import java.sql.SQLException; import java.util.List; public class Policydao { private QueryRunner queryRunner = new QueryRunner(DButil.getDruidDataSource()); public List<Policy> selectall() { List<Policy> policies = null; try { policies = queryRunner.query("select * from policy", new BeanListHandler<Policy>(Policy.class)); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectname(String name) { List<Policy> policies = null; name = "%" + name + "%"; Object[] prams = {name}; try { policies = queryRunner.query("select * from policy where name like ?", new BeanListHandler<Policy>(Policy.class), name); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameanddocument(String name, String document) { List<Policy> policies = null; name = "%" + name + "%"; document = "%" + document + "%"; Object[] prams = {name, document}; try { policies = queryRunner.query("select * from policy where name like ? and document like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameandorgan(String name, String organ) { List<Policy> policies = null; name = "%" + name + "%"; organ = "%" + organ + "%"; Object[] prams = {name, organ}; try { policies = queryRunner.query("select * from policy where name like ? and organ like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameandtext(String name, String text) { List<Policy> policies = null; name = "%" + name + "%"; text = "%" + text + "%"; Object[] prams = {name, text}; try { policies = queryRunner.query("select * from policy where name like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameanddocumentandorgan(String name, String document, String organ) { List<Policy> policies = null; name = "%" + name + "%"; document = "%" + document + "%"; organ = "%" + organ + "%"; Object[] prams = {name, document, organ}; try { policies = queryRunner.query("select * from policy where name like ? and document like ? and organ like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameanddocumentandtext(String name, String document, String text) { List<Policy> policies = null; name = "%" + name + "%"; document = "%" + document + "%"; text = "%" + text + "%"; Object[] prams = {name, document, text}; try { policies = queryRunner.query("select * from policy where name like ? and document like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameandorganandtext(String name, String organ, String text) { List<Policy> policies = null; name = "%" + name + "%"; organ = "%" + organ + "%"; text = "%" + text + "%"; Object[] prams = {name, organ, text}; try { policies = queryRunner.query("select * from policy where name like ? and organ like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectnameanddocumentandorganandtext(String name, String document, String organ, String text) { List<Policy> policies = null; name = "%" + name + "%"; document = "%" + document + "%"; organ = "%" + organ + "%"; text = "%" + text + "%"; Object[] prams = {name, document, organ, text}; try { policies = queryRunner.query("select * from policy where name like ? and document like ? and organ like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectdocument(String document) { List<Policy> policies = null; document = "%" + document + "%"; Object[] prams = {document}; try { policies = queryRunner.query("select * from policy where document like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectdocumentandorgan(String document, String organ) { List<Policy> policies = null; document = "%" + document + "%"; organ = "%" + organ + "%"; Object[] prams = {document, organ}; try { policies = queryRunner.query("select * from policy where document like ? and organ like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectdocumentandtext(String document, String text) { List<Policy> policies = null; document = "%" + document + "%"; text = "%" + text + "%"; Object[] prams = {document, text}; try { policies = queryRunner.query("select * from policy where document like ? and text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectdocumentandorganandtext(String document, String organ, String text) { List<Policy> policies = null; document = "%" + document + "%"; organ = "%" + organ + "%"; text = "%" + text + "%"; Object[] prams = {document, organ, text}; try { policies = queryRunner.query("select * from policy where document like ? and organ like ? and text = ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectorgan(String organ) { List<Policy> policies = null; organ = "%" + organ + "%"; Object[] prams = {organ}; try { policies = queryRunner.query("select * from policy where organ like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selectorganandtext(String organ, String text) { List<Policy> policies = null; organ = "%" + organ + "%"; text = "%" + text + "%"; Object[] prams = {organ, text}; try { policies = queryRunner.query("select * from policy where organ like ? and text = ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selecttext(String text) { List<Policy> policies = null; text = "%" + text + "%"; Object[] prams = {text}; try { policies = queryRunner.query("select * from policy where text like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selecttype(String type) { List<Policy> policies = null; type = "%" + type + "%"; Object[] prams = {type}; try { policies = queryRunner.query("select * from policy where type like ?", new BeanListHandler<Policy>(Policy.class), prams); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selecttypejichu() { List<Policy> policies = null; try { policies = queryRunner.query("select * from policy where type like '%基础研究与科研基地%' or type like '%基础研究%' or type like '%平台基地%'", new BeanListHandler<Policy>(Policy.class)); } catch (SQLException e) { e.printStackTrace(); } return policies; } public List<Policy> selecttypeqiye() { List<Policy> policies = null; try { policies = queryRunner.query("select * from policy where type like '%企业技术进步与高新技术产业化%' or type like '%企业%' or type like '%产业%' or type like '%创新载体%'", new BeanListHandler<Policy>(Policy.class)); } catch (SQLException e) { e.printStackTrace(); } return policies; } public Policy select(String name){ Policy policy = null; try { policy = queryRunner.query("select * from policy where name = ?",new BeanHandler<Policy>(Policy.class),name); } catch (SQLException e) { e.printStackTrace(); } return policy; } public static void main(String[] args) { Policydao policydao = new Policydao(); System.out.println(policydao.selectall().get(0).getText()); } }
Gettext.class
package servlet; import bean.Policy; import dao.Policydao; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; @WebServlet("/Gettext") public class Gettext extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); //解决乱码 resp.setContentType("text/html;charset=utf-8"); Policydao policydao = new Policydao(); Policy policy = null; String name = req.getParameter("name"); // System.out.println(name); policy = policydao.select(name); // String text = policy.getText(); HttpSession session = req.getSession(); session.setAttribute("policy",policy); resp.sendRedirect("/Select_war/text.jsp"); // req.getRequestDispatcher("http://localhost:8080/Select_war/text.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
Policyselser.class
package servlet; import bean.Policy; import com.google.gson.Gson; import dao.Policydao; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet("/Policyselser") public class Policyselser extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); String name = req.getParameter("name"); String document = req.getParameter("document"); String organ = req.getParameter("organ"); String text = req.getParameter("text"); Policydao policydao = new Policydao(); List<Policy> policies = null; int name1 = 0; int document1 = 0; int organ1 = 0; int text1 = 0; if (!"".equals(name)) { name1 = 1; } if (!"".equals(document)) { document1 = 1; } if (!"".equals(organ)) { organ1 = 1; } if (!"".equals(text)) { text1 = 1; } if (name1 == 0 && document1 == 0 && organ1 == 0 && text1 == 0) { policies = policydao.selectall(); } if (name1 == 1 && document1 == 0 && organ1 == 0 && text1 == 0) { policies = policydao.selectname(name); } if (name1 == 0 && document1 == 1 && organ1 == 0 && text1 == 0) { policies = policydao.selectdocument(document); } if (name1 == 0 && document1 == 0 && organ1 == 1 && text1 == 0) { policies = policydao.selectorgan(organ); } if (name1 == 0 && document1 == 0 && organ1 == 0 && text1 == 1) { policies = policydao.selecttext(text); } if (name1 == 1 && document1 == 1 && organ1 == 0 && text1 == 0) { policies = policydao.selectnameanddocument(name, document); } if (name1 == 1 && document1 == 0 && organ1 == 1 && text1 == 0) { policies = policydao.selectnameandorgan(name, organ); } if (name1 == 1 && document1 == 0 && organ1 == 0 && text1 == 1) { policies = policydao.selectnameandtext(name, text); } if (name1 == 1 && document1 == 1 && organ1 == 1 && text1 == 0) { policies = policydao.selectnameanddocumentandorgan(name, document, organ); } if (name1 == 1 && document1 == 1 && organ1 == 0 && text1 == 1) { policies = policydao.selectnameanddocumentandtext(name, document, text); } if (name1 == 1 && document1 == 0 && organ1 == 1 && text1 == 1) { policies = policydao.selectnameandorganandtext(name, organ, text); } if (name1 == 1 && document1 == 1 && organ1 == 1 && text1 == 1) { policies = policydao.selectnameanddocumentandorganandtext(name, document, organ, text); } if (name1 == 0 && document1 == 1 && organ1 == 1 && text1 == 0) { policies = policydao.selectdocumentandorgan(document, organ); } if (name1 == 0 && document1 == 1 && organ1 == 0 && text1 == 1) { policies = policydao.selectdocumentandtext(document, text); } if (name1 == 0 && document1 == 1 && organ1 == 1 && text1 == 1) { policies = policydao.selectdocumentandorganandtext(document, organ, text); } if (name1 == 0 && document1 == 0 && organ1 == 1 && text1 == 1) { policies = policydao.selectorganandtext(organ,text); } Gson gson = new Gson(); String s = gson.toJson(policies); resp.getWriter().write(s); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
Policyser.class
package servlet; import bean.Policy; import com.google.gson.Gson; import dao.Policydao; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet("/Policyser") public class Policyser extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); Policydao policydao = new Policydao(); List<Policy> policies = null; policies = policydao.selectall(); Gson gson = new Gson(); String s = gson.toJson(policies); resp.getWriter().write(s); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
Policytype.class
package servlet; import bean.Policy; import com.google.gson.Gson; import dao.Policydao; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.List; @WebServlet("/Policytype") public class Policytype extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=UTF-8"); String type = req.getParameter("type"); Policydao policydao = new Policydao(); List<Policy> policies = null; if (!"基础研究与科研基地".equals(type) && !"企业技术进步与高新技术产业化".equals(type)){ policies = policydao.selecttype(type); } if ("基础研究与科研基地".equals(type)){ policies = policydao.selecttypejichu(); } if ("企业技术进步与高新技术产业化".equals(type)){ policies = policydao.selecttypeqiye(); } Gson gson = new Gson(); String s = gson.toJson(policies); resp.getWriter().write(s); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
DButil.class
package util; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DButil { private static DruidDataSource druidDataSource; static { Properties properties = new Properties(); InputStream inputStream = DButil.class.getResourceAsStream("/datebase.properties"); try { properties.load(inputStream); try { druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection() { try { return druidDataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } public static void closeall(Connection connection, Statement statement, ResultSet resultSet) { try { if(resultSet != null) resultSet.close(); if(statement != null) statement.close(); if(connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } public static DruidDataSource getDruidDataSource() { return druidDataSource; } }
select.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>政策查询</title> <link rel="shortcut icon" href="favicon.ico"> <link href="css/bootstrap.min14ed.css?v=3.3.6" rel="stylesheet"> <link href="css/font-awesome.min93e3.css?v=4.4.0" rel="stylesheet"> <link href="css/plugins/iCheck/custom.css" rel="stylesheet"> <link href="css/animate.min.css" rel="stylesheet"> <link href="css/style.min862f.css?v=4.1.0" rel="stylesheet"> <style type="text/css"> body { margin: 0; padding: 0; height: 9999px; } .popup__wrapper { top: 0; left: 0; right: 0; bottom: 0; z-index: 999; display: flex; justify-content: center; align-items: center; } .popup { box-sizing: border-box; width: 670px; height: 300px; background: #fff; text-align: center; padding: 70px 10px 20px 10px; border-radius: 4px; } .popup__title { font-size: 30px; font-weight: 600; color: #004165; margin-bottom: 20px; } .popup__btnWrapper { width: 50%; margin: 0 auto; display: flex; justify-content: space-between; } .popup__text { font-size: 20px; margin-bottom: 50px; } button { width: 154px; cursor: pointer; border-radius: 4px; line-height: 45px; font-weight: 600; } .popup__yesBtn { color: #fff; background-color: #007dba; border: 1px solid #006ba1;} </style> <script type="text/javascript" src="jquery-3.5.1.js"></script> <script type="text/javascript"> var setcontent = function (name, organ, viadata, type) { for (var i = 0; i < name.length ; i++){ $('#tbody_1').append( '<tr><td >'+name[i]+'</td><td >'+organ[i]+'</td><td>'+viadata[i]+'</td><td> '+type[i]+'</td><td><a href="http://localhost:8080/Select_war/Gettext?name='+name[i]+'" target="_blank">查看</a></td></tr>' ) } } $(function () { $.get("http://localhost:8080/Select_war/Policyser", null, function (data) { var policy = eval(data) var name = []; var organ = []; var viadata = []; var type = []; for (var i = 0; i < policy.length; i++) { name.push(policy[i].name); organ.push(policy[i].organ); viadata.push(policy[i].viadata); type.push(policy[i].type); } setcontent(name,organ,viadata,type) }) $('#button1').click(function (){ var json = {"name":$("#name").val(),"document":$("#document").val(),"organ":$("#organ").val(),"text":$("#text").val(),} $.get("http://localhost:8080/Select_war/Policyselser", json, function (data) { var policy = eval(data) var name = []; var organ = []; var viadata = []; var type = []; for (var i = 0; i < policy.length; i++) { name.push(policy[i].name); organ.push(policy[i].organ); viadata.push(policy[i].viadata); type.push(policy[i].type); } $("#tbody_1").html(" ") setcontent(name,organ,viadata,type) }) }) $('#treeview1').on('nodeSelected',function(event, data) { var json1 = {"type" : data.name}; $.get("http://localhost:8080/Select_war/Policytype", json1, function (data) { var policy = eval(data) var name = []; var organ = []; var viadata = []; var type = []; for (var i = 0; i < policy.length; i++) { name.push(policy[i].name); organ.push(policy[i].organ); viadata.push(policy[i].viadata); type.push(policy[i].type); } $("#tbody_1").html(" ") setcontent(name,organ,viadata,type) }) }); }) </script> </head> <body class="gray-bg"> <div class="row wrapper wrapper-content animated fadeInRight" style="float: left"> <div class="col-sm-4"> <div class="ibox float-e-margins" style="width: 300px"> <div class="ibox-title"> <h5>分类</h5> </div> <div class="ibox-content"> <div id="treeview1" class="test"></div> </div> </div> </div> </div> <div class="wrapper wrapper-content animated fadeInRight" style="float: right"> <div class="row"> <div class="col-sm-12"> <div class="ibox float-e-margins" style="width: 1100px"> <div class="ibox-title"> <h5>政策列表</h5> </div> <div class="ibox-content"> <div class="row"> <form> <div class="col-sm-3"> <div class="input-group"> 政策名称<input type="text" placeholder="请输入政策名称" class="input-sm form-control" name="name" id="name"> </div> </div> <div class="col-sm-3"> <div class="input-group"> 发文字号<input type="text" placeholder="请输入发文字号" class="input-sm form-control" name="document" id="document"> </div> </div> <div class="col-sm-3"> <div class="input-group"> 发文机构<input type="text" class="input-sm form-control" name="organ" id="organ"> </div> </div> <div class="col-sm-3"> <div class="input-group"> 全文检索<input type="text" class="input-sm form-control" name="text" id="text"> <span class="input-group-btn"> <button type="button" class="btn btn-sm btn-primary" id="button1"> 搜索</button> </span> </div> </div> </form> </div> <div class="table-responsive"> <table class="table table-striped"> <thead> <tr> <th>政策名称</th> <th>发文机构</th> <th>颁布日期</th> <th>政策分类</th> <th>操作</th> </tr> </thead> <tbody id="tbody_1"> <!-- <tr><td>米莫说|MiMO Show</td><td>0.52/1.561</td><td>20%</td><td>2014.11.11</td><td>查看</td></tr>--> </tbody> </table> </div> </div> </div> </div> </div> </div> <script src="js/jquery.min.js?v=2.1.4"></script> <script src="js/bootstrap.min.js?v=3.3.6"></script> <script src="js/plugins/peity/jquery.peity.min.js"></script> <script src="js/content.min.js?v=1.0.0"></script> <script src="js/plugins/iCheck/icheck.min.js"></script> <script src="js/demo/peity-demo.min.js"></script> <script src="js/plugins/treeview/bootstrap-treeview.js"></script> <script src="js/demo/treeview-demo.min.js"></script> <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script> <script> $(document).ready(function () { $(".i-checks").iCheck({checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green",}) }); </script> <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script> </body> </html>
text.jsp
<%@ page import="bean.Policy" %><%-- Created by IntelliJ IDEA. User: Administrator Date: 2022/10/26 Time: 22:16 To change this template use File | Settings | File Templates. --%> <% // String text = (String) request.getAttribute("text"); HttpSession session1 = request.getSession(); String text = (String) session1.getAttribute("text"); Policy policy = null; policy = (Policy) session1.getAttribute("policy"); // String name = request.getParameter("name"); %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>政策详情</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style> body{ margin: 0; padding: 0; height: 9999px; //模仿内容很长的页面 } .popup__wrapper { top: 0; left: 0; right: 0; bottom: 0; z-index: 999; display: flex; justify-content: center; align-items: center; } .popup { box-sizing: border-box; width: 670px; height: 300px; background: #fff; text-align: center; padding: 70px 10px 20px 10px; border-radius: 4px; } .popup__title{ font-size: 30px; font-weight: 600; color: #004165; margin-bottom: 20px; } .popup__btnWrapper { width: 50%; margin: 0 auto; display: flex; justify-content: space-between; } .popup__text{ font-size: 20px; margin-bottom: 50px; } button{ width: 154px; cursor: pointer; border-radius: 4px; line-height: 45px; font-weight: 600; } .popup__yesBtn { color: #fff; background-color: #007dba; border: 1px solid #006ba1; } </style> </head> <body> <div class="popup__wrapper"> <div class="popup"> <%=policy.getText()%> </div> </div> </body> </html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Select</artifactId> <version>1.0-SNAPSHOT</version> <packaging>war</packaging> <name>Select Maven Webapp</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.7</maven.compiler.source> <maven.compiler.target>1.7</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/commons-io/commons-io --> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>1.3</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.19</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/taglibs/standard --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- json在java中的使用--> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <!-- java测试类--> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>RELEASE</version> <scope>compile</scope> </dependency> <!-- hamcrest-core --> <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core --> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-core</artifactId> <version>1.3</version> <scope>test</scope> </dependency> </dependencies> <build> <finalName>Select</finalName> <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) --> <plugins> <plugin> <artifactId>maven-clean-plugin</artifactId> <version>3.1.0</version> </plugin> <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging --> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>3.0.2</version> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.1</version> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.2.2</version> </plugin> <plugin> <artifactId>maven-install-plugin</artifactId> <version>2.5.2</version> </plugin> <plugin> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> </plugin> </plugins> </pluginManagement> </build> </project>