贴代码
package velocity.action; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import java.util.Properties; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.apache.velocity.app.VelocityEngine; import org.apache.velocity.context.Context; import org.apache.velocity.exception.ParseErrorException; import org.apache.velocity.exception.ResourceNotFoundException; public class LoginAction extends HttpServlet{ private static final long serialVersionUID = 5523228343945982882L; private VelocityEngine ve; Context context ; @Override public void init() throws ServletException { super.init(); context = new VelocityContext(); ve = new VelocityEngine();//得到velocity引擎 try { Properties prop = new Properties();// 设置vm模板的装载路径 String path = this.getServletContext().getRealPath("/"); prop.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path); ve.init(prop);//初始化配置文件 } catch (Exception e) { e.printStackTrace(); } } @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String vm = "/WebRoot/velocity/login.vm"; String out = "login.htm"; Template tpl = null; try { tpl = ve.getTemplate(vm); Writer writer = resp.getWriter(); context.put("name", "lele"); tpl.merge(context, writer); writer.write(out); System.out.println(out); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws ResourceNotFoundException, ParseErrorException, Exception { VelocityEngine v = new VelocityEngine(); v.init(); System.out.println(Thread.currentThread().getContextClassLoader().getResource(".").getPath()); Template tpl = v.getTemplate("/WebRoot/velocity/login.vm"); Context context = new VelocityContext(); context.put("name", "liujq"); StringWriter sw = new StringWriter(); tpl.merge(context, sw); System.out.println(sw.toString()); } }
问题:
org.apache.velocity.exception.ResourceNotFoundException: Unable to find resource ""/WebRoot/velocity/login.vm"" at org.apache.velocity.runtime.resource.ResourceManagerImpl.loadResource(ResourceManagerImpl.java:458) at org.apache.velocity.runtime.resource.ResourceManagerImpl.getResource(ResourceManagerImpl.java:341) at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:831) at org.apache.velocity.runtime.RuntimeInstance.getTemplate(RuntimeInstance.java:813) at org.apache.velocity.app.VelocityEngine.getTemplate(VelocityEngine.java:470) at velocity.action.LoginAction.service(LoginAction.java:49) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) at java.lang.Thread.run(Thread.java:724)
该类假如运行main方法,没有问题
配置路径
请高手们帮忙解决一下。
谢谢
解决方案:80分
WebRoot是一个虚拟目录,在webapps下是不存在的。去掉它。
解决方案:20分
配置tomcat执行路径不要加WebRoo就可以了,直接会访问你项目下myapp下。把文件保存到myapp下