合肥手机网站制作建设,自己做视频的网站,如何建设网站并与数据库相连,场口一站式建站哪家公司好目录
1.1准备测试数据 1.2 ApplicationContext应用上下文获取方式
1.2.1添加listener包
1.2.2解耦配置文件
1.2.3 继续提取ContextLoaderListener.java中参数app
1.2.4 Spring提供获取应用上下文的工具
二、SpringMVC
2.1概述 2.2数据准备 2.3添加success.jsp页面
2.4 …目录
1.1准备测试数据 1.2 ApplicationContext应用上下文获取方式
1.2.1添加listener包
1.2.2解耦配置文件
1.2.3 继续提取ContextLoaderListener.java中参数app
1.2.4 Spring提供获取应用上下文的工具
二、SpringMVC
2.1概述 2.2数据准备 2.3添加success.jsp页面
2.4 package com.study.test.Controller;添加类
2.5 创建spring-mvc.xml
2.6在web.xml中配置加载 spring-mvc.xml 的配置
三、Spring mvc 组件解析
3.1 SpringMVC的执行流程
3.1 SpringMVC的执行流程
3.2 SpringMVC组件解析
3.3 SpringMVC注解解析
3.4 SpringMVC的XML配置解析
3.5 知识要点 一、Spring与Web环境集成
1.1准备测试数据
pom.xml
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdcom.example/groupIdartifactIdSpring3-mvc/artifactIdversion1.0-SNAPSHOT/versionnameSpring3-mvc/namepackagingwar/packagingpropertiesmaven.compiler.target1.8/maven.compiler.targetmaven.compiler.source1.8/maven.compiler.sourcejunit.version5.7.1/junit.version/propertiesdependenciesdependencygroupIdjavax.servlet/groupIdartifactIdjavax.servlet-api/artifactIdversion4.0.1/versionscopeprovided/scope/dependencydependencygroupIdcom.example/groupIdartifactIdSpring-mvn-test/artifactIdversion1.0-SNAPSHOT/versionscopecompile/scope/dependency!-- https://mvnrepository.com/artifact/org.springframework/spring-web --dependencygroupIdorg.springframework/groupIdartifactIdspring-web/artifactIdversion5.3.14/version/dependency!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.3.16/version/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion8.0.15/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion1.0.9/version/dependency!-- https://mvnrepository.com/artifact/com.mchange/c3p0 --dependencygroupIdcom.mchange/groupIdartifactIdc3p0/artifactIdversion0.9.5.2/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion5.0.2.RELEASE/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.12/versionscopetest/scope/dependency/dependenciesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-war-plugin/artifactIdversion3.3.1/version/plugin /plugins/build
/project
dao层
package com.study.dao.impl;import com.study.dao.userDao;public class userDaoImpl implements userDao {Overridepublic void save() {System.out.println( 你好);}}service层
package com.study.Service.impl;
import com.study.Service.userService;
import com.study.dao.userDao;public class userServiceImpl implements userService {private userDao userDao;public void setUserDao(userDao userDao) {this.userDao userDao;}Overridepublic void inke() {userDao.save();}
}applicationContext.xml
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdbean iduserDao classcom.study.dao.impl.userDaoImpl/beanbean iduserService classcom.study.Service.impl.userServiceImplproperty nameuserDao refuserDao/property/bean/beans
遇到一次报错
java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext 原因把maven中导入的依赖的jar包从本地仓库找中拿出来放到了输出根目录里面。在maven项目中启动tomcat 后日志中就出现异常信息找不到WebApplicationContextWare可能是找不到相关jar包导致但是Maven依赖导入的jar包没问题所以它是在根目录下找不到jar包所以报错。 ———————————————— 版权声明本文为CSDN博主「技动」的原创文章遵循CC 4.0 BY-SA版权协议转载请附上原文出处链接及本声明。 原文链接https://blog.csdn.net/mo_ji63/article/details/107969415 解决办法就是同原文链接 web层
package com.study.test.web;import com.study.test.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;public class testServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ApplicationContext app new ClassPathXmlApplicationContext(applicationContext.xml);UserServiceImpl bean app.getBean(UserServiceImpl.class);bean.invoke();}Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}配置web.xml ?xml version1.0 encodingUTF-8?
web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0servletservlet-nameServlet/servlet-nameservlet-classcom.study.test.web.testServlet/servlet-class/servletservlet-mappingservlet-nameServlet/servlet-nameurl-pattern/service/url-pattern/servlet-mapping
/web-app 1.2 ApplicationContext应用上下文获取方式
原因应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的但是每次从 容器中获得Bean时都要编写newClasspathXmlApplicationContext(spring配置文件) 这样的弊端是配置 文件加载多次应用上下文对象创建多次。
处理方式在Web项目中可以使用ServletContextListener监听Web应用的启动我们可以在Web应用启动时就加 载Spring的配置文件创建应用上下文对象ApplicationContext在将其存储到最大的域servletContext域 中这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
1.2.1添加listener包
package com.study.test.listener;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;/*** author Alina* date 2022年03月18日 10:51 下午*/
public class ContextLoaderListener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent sce) {ApplicationContext app new ClassPathXmlApplicationContext(applicationContext.xml);//存储到最大的域ServletContext servletContext sce.getServletContext();System.out.println(监听器);servletContext.setAttribute(app,app);}Overridepublic void contextDestroyed(ServletContextEvent sce) {}
}web.xml添加 listenerlistener-classcom.study.test.listener.ContextLoaderListener/listener-class
/listener 1.2.2解耦配置文件
web.xml添加 !--添加全局配置参数--
context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:applicationContext.xml/param-value
/context-param package com.study.test.listener;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;/*** author Alina* date 2022年03月18日 10:51 下午*/
public class ContextLoaderListener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent sce) {ServletContext servletContext sce.getServletContext();//读取全局参数配置文件String contextConfigLocation servletContext.getInitParameter(contextConfigLocation);ApplicationContext app new ClassPathXmlApplicationContext(contextConfigLocation);//存储到最大的域System.out.println(监听器);servletContext.setAttribute(app,app);}Overridepublic void contextDestroyed(ServletContextEvent sce) {}
}1.2.3 继续提取ContextLoaderListener.java中参数app
在package com.study.test.listener;添加WebApplicationContextUtils 工具类
public class WebApplicationContextUtils {public static ApplicationContext getApplicationContext(ServletContext servletContext){return (ApplicationContext) servletContext.getAttribute(app);}
}
package com.study.test.web;改写
package com.study.test.web;import com.study.test.listener.WebApplicationContextUtils;
import com.study.test.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;public class testServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ServletContext servletContext request.getServletContext();ApplicationContext app WebApplicationContextUtils.getApplicationContext(servletContext);UserServiceImpl bean app.getBean(UserServiceImpl.class);bean.invoke();}Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}1.2.4 Spring提供获取应用上下文的工具
1在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
dependencygroupIdorg.springframework/groupIdartifactIdspring-web/artifactIdversion5.3.14/version
/dependency !--添加全局配置参数--
context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:applicationContext.xml/param-value
/context-param
listenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class
/listener
2.使用WebApplicationContextUtils获得应用上下文对象ApplicationContext package com.study.test.web;import com.study.test.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;public class testServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ServletContext servletContext request.getServletContext();
// ApplicationContext app WebApplicationContextUtils.getApplicationContext(servletContext);WebApplicationContext app WebApplicationContextUtils.getWebApplicationContext(servletContext);UserServiceImpl bean app.getBean(UserServiceImpl.class);bean.invoke();}Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}二、SpringMVC
2.1概述
SpringMVC 是一种基于 Java 的实现 MVC 设计模型的请求驱动类型的轻量级 Web 框架属于 SpringFrameWork 的后续产品已经融合在 Spring Web Flow 中。
SpringMVC 已经成为目前最主流的MVC框架之一并且随着Spring3.0 的发布全面超越 Struts2成为最优 秀的 MVC 框架。它通过一套注解让一个简单的 Java 类成为处理请求的控制器而无须实现任何接口。同时 它还支持 RESTful 编程风格的请求。 2.2数据准备
pom.xml !--Spring坐标--dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion5.0.5.RELEASE/version/dependency !--SpringMVC坐标--dependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion5.0.5.RELEASE/version/dependency!--Servlet坐标-- dependencygroupIdjavax.servlet/groupIdartifactIdservlet-api/artifactIdversion2.5/version
/dependency !--Jsp坐标--dependencygroupIdjavax.servlet.jsp/groupIdartifactIdjsp-api/artifactIdversion2.0/version/dependency web.xml添加 !-- SpringMVC的核心控制器--servletservlet-nameDispatcherServlet/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-class
!-- 服务器启动时就添加--load-on-startup1/load-on-startup/servletservlet-mappingservlet-nameDispatcherServlet/servlet-nameurl-pattern//url-pattern/servlet-mapping 2.3添加success.jsp页面
% page contentTypetext/html;charsetUTF-8 languagejava %
html
headtitle1/title
/head
body
你好/body
/html2.4 package com.study.test.Controller;添加类
package com.study.test.Controller;import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;/*** author Alina* date 2022年03月19日 12:04 上午*/
//放在spring 容器内
Controller
public class QuickController {//添加映射地址RequestMapping(/quick)public String quickMethod(){System.out.println(quickMethod running.....);return success.jsp; }}2.5 创建spring-mvc.xml ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:contexthttp://www.springframework.org/schema/contextxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
!-- 添加组件扫描--context:component-scan base-packagecom.student.test.controller//beans 2.6在web.xml中配置加载 spring-mvc.xml 的配置
在web.xml中配置
?xml version1.0 encodingUTF-8?
web-app xmlnshttp://xmlns.jcp.org/xml/ns/javaeexmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsdversion4.0
!-- SpringMVC的核心控制器--servletservlet-nameDispatcherServlet/servlet-nameservlet-classorg.springframework.web.servlet.DispatcherServlet/servlet-classinit-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:spring-mvc.xml/param-value/init-param
!-- 服务器启动时就添加--load-on-startup1/load-on-startup/servletservlet-mappingservlet-nameDispatcherServlet/servlet-nameurl-pattern//url-pattern/servlet-mapping!--添加全局配置参数--context-paramparam-namecontextConfigLocation/param-nameparam-valueclasspath:applicationContext.xml/param-value/context-paramlistenerlistener-classorg.springframework.web.context.ContextLoaderListener/listener-class/listenerservletservlet-nameServlet/servlet-nameservlet-classcom.study.test.web.testServlet/servlet-class/servletservlet-mappingservlet-nameServlet/servlet-nameurl-pattern/service/url-pattern/servlet-mapping
/web-app 三、Spring mvc 组件解析
3.1 SpringMVC的执行流程 3.1 SpringMVC的执行流程
1 用户发送请求至前端控制器DispatcherServlet。 2 DispatcherServlet收到请求调用HandlerMapping处理器映射器。 3 处理器映射器找到具体的处理器(可以根据xml配置、注解进行查找)生成处理器对象及处理器拦截器(如果
有则生成)一并返回给DispatcherServlet。 4 DispatcherServlet调用HandlerAdapter处理器适配器。 5 HandlerAdapter经过适配调用具体的处理器(Controller也叫后端控制器)。 6 Controller执行完成返回ModelAndView。 7 HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。 8 DispatcherServlet将ModelAndView传给ViewReslover视图解析器。 9 ViewReslover解析后返回具体View。 10 DispatcherServlet根据View进行渲染视图(即将模型数据填充至视图中)。DispatcherServlet响应用户。
3.2 SpringMVC组件解析
1. 前端控制器:DispatcherServlet
用户请求到达前端控制器它就相当于 MVC 模式中的 CDispatcherServlet 是整个流程控制的中心由 它调用其它组件处理用户的请求DispatcherServlet 的存在降低了组件之间的耦合性。
2. 处理器映射器:HandlerMapping
HandlerMapping 负责根据用户请求找到 Handler 即处理器SpringMVC 提供了不同的映射器实现不同的 映射方式例如:配置文件方式实现接口方式注解方式等。
3. 处理器适配器:HandlerAdapter
通过 HandlerAdapter 对处理器进行执行这是适配器模式的应用通过扩展适配器可以对更多类型的处理 器进行执行。
4. 处理器:Handler
它就是我们开发中要编写的具体业务控制器。由 DispatcherServlet 把用户请求转发到 Handler。由 Handler 对具体的用户请求进行处理。
5. 视图解析器:View Resolver
View Resolver 负责将处理结果生成 View 视图View Resolver 首先根据逻辑视图名解析成物理视图名即 具体的页面地址再生成 View 视图对象最后对 View 进行渲染将处理结果通过页面展示给用户。
6. 视图:View
SpringMVC 框架提供了很多的 View 视图类型的支持包括:jstlView、freemarkerView、pdfView等。最 常用的视图就是 jsp。一般情况下需要通过页面标签或页面模版技术将模型数据通过页面展示给用户需要由程 序员根据业务需求开发具体的页面 3.3 SpringMVC注解解析
RequestMapping
作用:用于建立请求 URL 和处理请求方法之间的对应关系 位置:
类上请求URL 的第一级访问目录。此处不写的话就相当于应用的根目录
方法上请求 URL 的第二级访问目录与类上的使用ReqquestMapping标注的一级目录一起组成访问虚拟路径 属性:
value:用于指定请求的URL。它和path属性的作用是一样的 method:用于指定请求的方式 params:用于指定限制请求参数的条件。它支持简单的表达式。要求请求参数的key和value必须和配置的一模一样
例如: params {accountName}表示请求参数必须有accountName params {moeny!100}表示请求参数中money不能是100
1. mvc命名空间引入
命名空间:xmlns:contexthttp://www.springframework.org/schema/context xmlns:mvchttp://www.springframework.org/schema/mvc
约束地址:http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
2. 组件扫描
SpringMVC基于Spring容器所以在进行S pri ngM VC操作时需要将C ont rol ler存储到 Spr ing容器中如果使 用Controller注解标注的话就需要使用context:component-scan base- package“com.itheima.controller/进行组件扫描。
3.4 SpringMVC的XML配置解析 1. 视图解析器
SpringMVC有默认组件配置默认组件都是DispatcherServlet.properties配置文件中配置的该配置文件地址 org/springframework/web/servlet/DispatcherServlet.properties该文件中配置了默认的视图解析器如下:
org.springframework.web.servlet.ViewResolverorg.springframework.web.servlet.view.I nternalResourceViewResolver
翻看该解析器源码可以看到该解析 器的默 认设置 如下 : 3.5 知识要点
SpringMVC的相关组件
• 前端控制器:DispatcherServlet
• 处理器映射器:HandlerMapping
• 处理器适配器:HandlerAdapter 总结 处理器:Handler 视图解析器:View Resolver 视图:View
SpringMVC的注解和配置
• 请求映射注解:RequestMapping
• 视图解析器配置: REDIRECT_URL_PREFIX redirect:
FORWARD_URL_PREFIX forward: prefix ; suffix ;