java web程序设计

view

    Example View


Example jsp view

The value added to the request was<%=request.getAttribute("date")%> HiddenFieldServlet
"http://localhost:8080/Controller",method="GET"> type="hidden" NAME="service" VALUE="/ExampleService"> type="hidden" NAME="target" VALUE="/ExampleView.jsp"> type="submit" VALUE="Submit">
model public interface Service { public void execute(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws Exception; } public class ExampleService implements Service { public ExampleService() { } public void execute(HttpServletRequest request, HttpServletResponse response, ServletContext context) throws Exception { Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); request.setAttribute("date", "format.format(date)"); PrintWriter out = response.getWriter(); out.write("format.format(date)"); } } controller public class Controller extends HttpServlet { public void init(ServletConfig config) throws ServletException { super.init(config); } protected void forward(HttpServletRequest request, HttpServletResponse response, String target) throws ServletException { try { ServletContext context=getServletContext(); if (!target.equals("false")) { RequestDispatcher dispatcher = context.getRequestDispatcher(target); dispatcher.forward(request, response); } } catch (IOException ioe) { throw new ServletException(ioe.getMessage()); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String serviceName = request.getParameter("service"); if (serviceName == null) { throw new ServletException("No service named!"); } String target = request.getParameter("target"); if (target == null) { throw new ServletException("No target named!"); } ServletContext context = getServletContext(); try { Class cls = Class.forName(serviceName); Service service = (Service) cls.newInstance(); service.execute(request, response, context); } catch (ClassNotFoundException ce) { } catch (IllegalAccessException iae) { throw new ServletException(iae.getMessage()); } catch (Exception e) { throw new ServletException(e.getMessage()); } forward(request,response,target); } } 在这个MVC中,找不到ExampleService类的原因是什么,为什么ExampleService里的数据无法传达给ExampleView.jsp



看一下报错信息

在这个MVC中,找不到ExampleService类的原因可能是类路径不正确或未正确引用所需的库。要确保ExampleService类的路径正确,并且在controller中正确地使用了类名。另外,ExampleService中的数据无法传达给ExampleView.jsp可能是因为您使用了错误的语法。在ExampleService类中,您应该使用request.setAttribute("date", format.format(date))将date的格式化字符串设置为date属性。在ExampleView.jsp中,您应该使用<%=request.getAttribute("date")%>来检索属性并将其呈现在页面上。

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/341610
  • 这篇博客也不错, 你可以看下java web 图书管理系统
  • 除此之外, 这篇博客: java web连接数据库中的 总结 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    这里我创建数据库时,使用的是Navicat,大家使用Navicat将数据库创建成功之后,过几天在访问改数据库时,可能会弹出这样的窗口
    在这里插入图片描述
    出现这个问题的原因可能是:MySQL数据库的服务没有打开
    在这里插入图片描述
    在这里插入图片描述

    打开之后就可以正常访问自己所创建的数据库了。
    这里我是连接的MySQL数据库。如果大家想连接别的数据可,需要修改的地方:1.导入在项目中lib目录下的厂商驱动包要换成对应数据库的厂商驱动包。2.代码中的步骤1 、2要改成对应数据库的加载驱动和创建连接。
    如果有啥错误的地方,欢迎大家指出来,有啥不懂的也可以在下方留言,我会及时回复的!!!欢迎大家批评指正!!!!

  • 您还可以看一下 Long老师的Java Web从入门到电商项目实战挑战万元高薪(javaweb教程)课程中的 课程介绍小节, 巩固相关知识点

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^