4-7的路径问题。
老师对于xml写入的路径有些疑问
我刚开始的xml文件是放在/src/mgallery/painting.xml
但在写入里面打印出来的路径是
/mgallery/build/classes/painting.xml
而且的确只有这个里面的数据更改了,src目录下的那个xml数据没有变,问下这个是为什么呢? 为什么src底下的那个xml数据不变呢?
正在回答
同学你好,这里可以这样理解一下: 当项目发布到tomcat中启动执行后,它就会执行在Tomcat的目录下的发布项目中的内容,比如:
而此时并不会修改源代码中的内容,项目发布出去后,它执行的是发布到服务器中的内容,而不会修改源码项目中的内容。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
老师不好意思更新一下,tomcat重新启动以后就会自动读取我exlips目录下build里面的xml并且把tomcat-webapp目录下面的xml覆盖,所以就只有src下面和build下面两个不同的xml
package mgallery.dao; import java.util.ArrayList; import java.util.List; import mgallery.entity.Painting; import mgallery.utils.PageModel; import mgallery.utils.XmlDataSource; public class PaintingDao { public PageModel pagination(int page, int rows) { List<Painting> list = XmlDataSource.getRawData(); PageModel pageModel = new PageModel(list,page,rows); return pageModel; } public PageModel pagination(int category, int page, int rows) { List<Painting> list = XmlDataSource.getRawData(); List<Painting> categoryList = new ArrayList(); for(Painting p: list) { if(p.getCategroy() ==category ) { categoryList.add(p); } } PageModel pageModel = new PageModel(categoryList,page,rows); return pageModel; } public void create(Painting painting) { XmlDataSource.append(painting); } }
package mgallery.service; import javax.management.RuntimeErrorException; import mgallery.dao.PaintingDao; import mgallery.entity.Painting; import mgallery.utils.PageModel; public class PaintingService { private PaintingDao paintingDao = new PaintingDao(); public PageModel pagination(int page,int rows, String...category) { if (rows == 0) { throw new RuntimeException("无效的rows 参数"); } if(category.length ==0 || category[0]==null) { return paintingDao.pagination(page, rows); }else { return paintingDao.pagination(Integer.parseInt(category[0]), page, rows); } } public void create(Painting painting) { paintingDao.create(painting); } }
package mgallery.controller; import java.io.IOException; 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 mgallery.service.PaintingService; import mgallery.utils.PageModel; /** * Servlet implementation class PaintingController */ @WebServlet("/page") public class PaintingController extends HttpServlet { private static final long serialVersionUID = 1L; private PaintingService paintingSerivce = new PaintingService(); /** * @see HttpServlet#HttpServlet() */ public PaintingController() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub String page = request.getParameter("p"); String rows = request.getParameter("r"); String category = request.getParameter("c"); if(page == null) { page = "1"; } if(rows == null) { rows = "6"; } PageModel pageModel =paintingSerivce.pagination(Integer.parseInt(page), Integer.parseInt(rows),category); request.setAttribute("pageModel", pageModel); request.getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response); } }
package mgallery.controller; import java.io.File; import java.io.IOException; import java.util.List; import java.util.UUID; 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 org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import mgallery.entity.Painting; import mgallery.service.PaintingService; import mgallery.utils.PageModel; /** * Servlet implementation class ManagementController */ @WebServlet("/management") public class ManagementController extends HttpServlet { private static final long serialVersionUID = 1L; private PaintingService paintingService = new PaintingService(); /** * @see HttpServlet#HttpServlet() */ public ManagementController() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); String method = request.getParameter("method"); if(method.equals("list")) { this.list(request,response); }else if(method.equals("show_create")){ this.showCreatePage(request,response); }else if(method.equals("create")) { this.create(request, response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String p = request.getParameter("p"); String r = request.getParameter("r"); if(p==null) { p="1"; } if(r == null) { r ="6"; } PageModel pageModel = paintingService.pagination(Integer.parseInt(p),Integer.parseInt(r)); request.setAttribute("pageModel", pageModel); request.getRequestDispatcher("/WEB-INF/jsp/list.jsp").forward(request, response); } private void showCreatePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getRequestDispatcher("/WEB-INF/jsp/create.jsp").forward(request, response); } private void create(HttpServletRequest request, HttpServletResponse response) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload sf = new ServletFileUpload(factory); try { List<FileItem> formData = sf.parseRequest(request); Painting painting = new Painting(); for (FileItem fi:formData) { if(fi.isFormField() ) { //System.out.println(fi.getFieldName() ); switch (fi.getFieldName()) { case "pname": painting.setPname(fi.getString("UTF-8")); break; case "category": painting.setCategroy(Integer.parseInt(fi.getString("UTF-8"))); break; case "price": painting.setPrice(Integer.parseInt(fi.getString("UTF-8"))); break; case "description": painting.setDescrption(fi.getString("UTF-8")); break; default: break; } }else { String path = request.getServletContext().getRealPath("/upload"); //System.out.println(path); String fileName = UUID.randomUUID().toString(); String suffix = fi.getName().substring(fi.getName().lastIndexOf(".")); fi.write(new File(path,fileName + suffix)); painting.setPreview("/upload/" + fileName + suffix); } } paintingService.create(painting); response.sendRedirect("/management?method=list"); } catch ( Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package mgallery.utils; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.net.URLDecoder; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; import mgallery.entity.Painting; //将xml对象解析为java对象 public class XmlDataSource { //通过static关键字保证全局唯一 private static List data = new ArrayList(); private static String dataFile; static { //在程序运行以后得到类路径目录下的painint.xml的路径地址 dataFile = XmlDataSource.class.getResource("/painting.xml").getPath(); //System.out.println(dataFile); //倘若上面读取到的路径包含有特殊字符,类似于空格, //会被自动转换为%20,导致读取时出错,底下为解决方法 reload(); } private static void reload() { URLDecoder decode = new URLDecoder(); try { dataFile = decode.decode(dataFile,"UTF-8"); //利用dom4j对xml文档进行解析 SAXReader reader = new SAXReader(); //1.获取Document对象 Document document = reader.read(dataFile); //2.利用xpath得到xml节点集合,导入一定是org.dom4j.node List<Node> nodes = document.selectNodes("/root/painting"); data.clear(); for (Node node:nodes) { Element element = (Element)node; String id = element.attributeValue("id"); String pname = element.elementText("pname"); Painting painting = new Painting(); painting.setId(Integer.parseInt(id)); painting.setPname(pname); painting.setCategroy(Integer.parseInt(element.elementText("category"))); painting.setPrice(Integer.parseInt(element.elementText("price"))); painting.setPreview(element.elementText("preview")); painting.setDescrption(element.elementText("description")); data.add(painting); } } catch (UnsupportedEncodingException | DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static List<Painting> getRawData(){ return data; } public static void append(Painting painting) { SAXReader reader = new SAXReader(); Writer writer = null; try { Document document = reader.read(dataFile); Element root = document.getRootElement(); Element p = root.addElement("painting"); p.addAttribute("id", String.valueOf(data.size()+1)); p.addElement("pname").setText(painting.getPname()); p.addElement("category").setText(painting.getCategroy().toString()); p.addElement("price").setText(painting.getPrice().toString()); p.addElement("preview").setText(painting.getPreview()); p.addElement("description").setText(painting.getDescrption()); writer = new OutputStreamWriter(new FileOutputStream(dataFile),"UTF-8"); System.out.println(dataFile); document.write(writer); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(writer != null) { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } reload(); } } public static void main(String[] args) { Painting p = new Painting(); p.setPname("测试油画"); p.setCategroy(1); p.setPrice(4000); p.setPreview("/upload/1.jpg"); p.setDescrption("描述信息"); XmlDataSource.append(p); } }
package mgallery.entity; public class Painting { private Integer id; private String pname; private Integer categroy; private Integer price; private String preview; private String descrption; public Painting() { } @Override public String toString() { return "Painting [id=" + id + ", pname=" + pname + ", categroy=" + categroy + ", price=" + price + ", preview=" + preview + ", descrption=" + descrption + "]"; } public Painting(Integer id, String pname, Integer categroy, Integer price, String preview, String descrption) { super(); this.id = id; this.pname = pname; this.categroy = categroy; this.price = price; this.preview = preview; this.descrption = descrption; } /** * @return the id */ public Integer getId() { return id; } /** * @param id the id to set */ public void setId(Integer id) { this.id = id; } /** * @return the pname */ public String getPname() { return pname; } /** * @param pname the pname to set */ public void setPname(String pname) { this.pname = pname; } /** * @return the categroy */ public Integer getCategroy() { return categroy; } /** * @param categroy the categroy to set */ public void setCategroy(Integer categroy) { this.categroy = categroy; } /** * @return the price */ public Integer getPrice() { return price; } /** * @param price the price to set */ public void setPrice(Integer price) { this.price = price; } /** * @return the preview */ public String getPreview() { return preview; } /** * @param preview the preview to set */ public void setPreview(String preview) { this.preview = preview; } /** * @return the descrption */ public String getDescrption() { return descrption; } /** * @param descrption the descrption to set */ public void setDescrption(String descrption) { this.descrption = descrption; } }
<root> <painting id="1"> <pname>古典油画鉴赏1</pname> <category>1</category> <price>3800</price> <preview>/upload/1.jpg</preview> <description>古典油画鉴赏1的描述文本</description> </painting> <painting id="2"> <pname>古典油画鉴赏2</pname> <category>1</category> <price>3800</price> <preview>/upload/2.jpg</preview> <description>古典油画鉴赏2的描述文本</description> </painting> <painting id="3"> <pname>古典油画鉴赏3</pname> <category>1</category> <price>3800</price> <preview>/upload/3.jpg</preview> <description>古典油画鉴赏3的描述文本</description> </painting> <painting id="4"> <pname>古典油画鉴赏4</pname> <category>1</category> <price>3800</price> <preview>/upload/4.jpg</preview> <description>古典油画鉴赏4的描述文本</description> </painting> <painting id="5"> <pname>古典油画鉴赏5</pname> <category>1</category> <price>3800</price> <preview>/upload/5.jpg</preview> <description>古典油画鉴赏5的描述文本</description> </painting> <painting id="6"> <pname>古典油画鉴赏6</pname> <category>1</category> <price>3800</price> <preview>/upload/6.jpg</preview> <description>古典油画鉴赏6的描述文本</description> </painting> <painting id="7"> <pname>古典油画鉴赏7</pname> <category>1</category> <price>3800</price> <preview>/upload/7.jpg</preview> <description>古典油画鉴赏7的描述文本</description> </painting> <painting id="8"> <pname>古典油画鉴赏8</pname> <category>1</category> <price>3800</price> <preview>/upload/8.jpg</preview> <description>古典油画鉴赏8的描述文本</description> </painting> <painting id="9"> <pname>古典油画鉴赏9</pname> <category>1</category> <price>3800</price> <preview>/upload/9.jpg</preview> <description>古典油画鉴赏9的描述文本</description> </painting> <painting id="10"> <pname>抽象派油画鉴赏1</pname> <category>2</category> <price>3800</price> <preview>/upload/10.jpg</preview> <description>抽象派油画鉴赏1的描述文本</description> </painting> <painting id="11"> <pname>抽象派油画鉴赏2</pname> <category>2</category> <price>3800</price> <preview>/upload/11.jpg</preview> <description>抽象派油画鉴赏2的描述文本</description> </painting> <painting id="12"> <pname>抽象派油画鉴赏3</pname> <category>2</category> <price>3800</price> <preview>/upload/12.jpg</preview> <description>抽象派油画鉴赏3的描述文本</description> </painting> <painting id="13"> <pname>抽象派油画鉴赏4</pname> <category>2</category> <price>3800</price> <preview>/upload/13.jpg</preview> <description>抽象派油画鉴赏4的描述文本</description> </painting> <painting id="14"> <pname>抽象派油画鉴赏5</pname> <category>2</category> <price>3800</price> <preview>/upload/14.jpg</preview> <description>抽象派油画鉴赏5的描述文本</description> </painting> <painting id="15"> <pname>抽象派油画鉴赏6</pname> <category>2</category> <price>3800</price> <preview>/upload/15.jpg</preview> <description>抽象派油画鉴赏6的描述文本</description> </painting> <painting id="16"> <pname>测试油画</pname> <category>1</category> <price>4000</price> <preview>/upload/1.jpg</preview> <description>描述信息</description> </painting> <painting id="17"> <pname>测试油画</pname> <category>1</category> <price>4000</price> <preview>/upload/1.jpg</preview> <description>描述信息</description> </painting> <painting id="18"> <pname>测试油画2</pname> <category>1</category> <price>1200</price> <preview>/upload/e2896ea4-1208-4927-8490-a8305b7a88eb.jpg</preview> <description>测试油画2</description> </painting> <painting id="19"> <pname>测试油画3</pname> <category>2</category> <price>1111</price> <preview>/upload/4add04d4-fca4-49a3-905a-a90c3dcb6143.png</preview> <description>测试油画3</description> </painting> </root>
D:/apache-tomcat-8.5.57/webapps/mgallery/WEB-INF/classes/painting.xml
eclipse-workspace/mgallery/src/painting.xml
<root> <painting id="1"> <pname>古典油画鉴赏1</pname> <category>1</category> <price>3800</price> <preview>/upload/1.jpg</preview> <description>古典油画鉴赏1的描述文本</description> </painting> <painting id="2"> <pname>古典油画鉴赏2</pname> <category>1</category> <price>3800</price> <preview>/upload/2.jpg</preview> <description>古典油画鉴赏2的描述文本</description> </painting> <painting id="3"> <pname>古典油画鉴赏3</pname> <category>1</category> <price>3800</price> <preview>/upload/3.jpg</preview> <description>古典油画鉴赏3的描述文本</description> </painting> <painting id="4"> <pname>古典油画鉴赏4</pname> <category>1</category> <price>3800</price> <preview>/upload/4.jpg</preview> <description>古典油画鉴赏4的描述文本</description> </painting> <painting id="5"> <pname>古典油画鉴赏5</pname> <category>1</category> <price>3800</price> <preview>/upload/5.jpg</preview> <description>古典油画鉴赏5的描述文本</description> </painting> <painting id="6"> <pname>古典油画鉴赏6</pname> <category>1</category> <price>3800</price> <preview>/upload/6.jpg</preview> <description>古典油画鉴赏6的描述文本</description> </painting> <painting id="7"> <pname>古典油画鉴赏7</pname> <category>1</category> <price>3800</price> <preview>/upload/7.jpg</preview> <description>古典油画鉴赏7的描述文本</description> </painting> <painting id="8"> <pname>古典油画鉴赏8</pname> <category>1</category> <price>3800</price> <preview>/upload/8.jpg</preview> <description>古典油画鉴赏8的描述文本</description> </painting> <painting id="9"> <pname>古典油画鉴赏9</pname> <category>1</category> <price>3800</price> <preview>/upload/9.jpg</preview> <description>古典油画鉴赏9的描述文本</description> </painting> <painting id="10"> <pname>抽象派油画鉴赏1</pname> <category>2</category> <price>3800</price> <preview>/upload/10.jpg</preview> <description>抽象派油画鉴赏1的描述文本</description> </painting> <painting id="11"> <pname>抽象派油画鉴赏2</pname> <category>2</category> <price>3800</price> <preview>/upload/11.jpg</preview> <description>抽象派油画鉴赏2的描述文本</description> </painting> <painting id="12"> <pname>抽象派油画鉴赏3</pname> <category>2</category> <price>3800</price> <preview>/upload/12.jpg</preview> <description>抽象派油画鉴赏3的描述文本</description> </painting> <painting id="13"> <pname>抽象派油画鉴赏4</pname> <category>2</category> <price>3800</price> <preview>/upload/13.jpg</preview> <description>抽象派油画鉴赏4的描述文本</description> </painting> <painting id="14"> <pname>抽象派油画鉴赏5</pname> <category>2</category> <price>3800</price> <preview>/upload/14.jpg</preview> <description>抽象派油画鉴赏5的描述文本</description> </painting> <painting id="15"> <pname>抽象派油画鉴赏6</pname> <category>2</category> <price>3800</price> <preview>/upload/15.jpg</preview> <description>抽象派油画鉴赏6的描述文本</description> </painting> </root>
eclipse-workspace/mgallery/build/classes/painting.xml
<root> <painting id="1"> <pname>古典油画鉴赏1</pname> <category>1</category> <price>3800</price> <preview>/upload/1.jpg</preview> <description>古典油画鉴赏1的描述文本</description> </painting> <painting id="2"> <pname>古典油画鉴赏2</pname> <category>1</category> <price>3800</price> <preview>/upload/2.jpg</preview> <description>古典油画鉴赏2的描述文本</description> </painting> <painting id="3"> <pname>古典油画鉴赏3</pname> <category>1</category> <price>3800</price> <preview>/upload/3.jpg</preview> <description>古典油画鉴赏3的描述文本</description> </painting> <painting id="4"> <pname>古典油画鉴赏4</pname> <category>1</category> <price>3800</price> <preview>/upload/4.jpg</preview> <description>古典油画鉴赏4的描述文本</description> </painting> <painting id="5"> <pname>古典油画鉴赏5</pname> <category>1</category> <price>3800</price> <preview>/upload/5.jpg</preview> <description>古典油画鉴赏5的描述文本</description> </painting> <painting id="6"> <pname>古典油画鉴赏6</pname> <category>1</category> <price>3800</price> <preview>/upload/6.jpg</preview> <description>古典油画鉴赏6的描述文本</description> </painting> <painting id="7"> <pname>古典油画鉴赏7</pname> <category>1</category> <price>3800</price> <preview>/upload/7.jpg</preview> <description>古典油画鉴赏7的描述文本</description> </painting> <painting id="8"> <pname>古典油画鉴赏8</pname> <category>1</category> <price>3800</price> <preview>/upload/8.jpg</preview> <description>古典油画鉴赏8的描述文本</description> </painting> <painting id="9"> <pname>古典油画鉴赏9</pname> <category>1</category> <price>3800</price> <preview>/upload/9.jpg</preview> <description>古典油画鉴赏9的描述文本</description> </painting> <painting id="10"> <pname>抽象派油画鉴赏1</pname> <category>2</category> <price>3800</price> <preview>/upload/10.jpg</preview> <description>抽象派油画鉴赏1的描述文本</description> </painting> <painting id="11"> <pname>抽象派油画鉴赏2</pname> <category>2</category> <price>3800</price> <preview>/upload/11.jpg</preview> <description>抽象派油画鉴赏2的描述文本</description> </painting> <painting id="12"> <pname>抽象派油画鉴赏3</pname> <category>2</category> <price>3800</price> <preview>/upload/12.jpg</preview> <description>抽象派油画鉴赏3的描述文本</description> </painting> <painting id="13"> <pname>抽象派油画鉴赏4</pname> <category>2</category> <price>3800</price> <preview>/upload/13.jpg</preview> <description>抽象派油画鉴赏4的描述文本</description> </painting> <painting id="14"> <pname>抽象派油画鉴赏5</pname> <category>2</category> <price>3800</price> <preview>/upload/14.jpg</preview> <description>抽象派油画鉴赏5的描述文本</description> </painting> <painting id="15"> <pname>抽象派油画鉴赏6</pname> <category>2</category> <price>3800</price> <preview>/upload/15.jpg</preview> <description>抽象派油画鉴赏6的描述文本</description> </painting> <painting id="16"> <pname>测试油画</pname> <category>1</category> <price>4000</price> <preview>/upload/1.jpg</preview> <description>描述信息</description> </painting> <painting id="17"> <pname>测试油画</pname> <category>1</category> <price>4000</price> <preview>/upload/1.jpg</preview> <description>描述信息</description> </painting> </root>
增加一点 在最后在tomcat上面运行的时候增加的路径是
D:/apache-tomcat-8.5.57/webapps/mgallery/WEB-INF/classes/painting.xml
并且前面单独运行datasorce类里面增加的两个油画 没有被读取到,这个又是什么原因呢?
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星