{"id":34347,"date":"2025-04-29T10:38:03","date_gmt":"2025-04-29T02:38:03","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/34347.html"},"modified":"2025-04-29T10:38:03","modified_gmt":"2025-04-29T02:38:03","slug":"%e6%89%8b%e5%86%99springmvc%e5%9f%ba%e6%9c%ac%e6%a1%86%e6%9e%b6","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/34347.html","title":{"rendered":"\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6)"},"content":{"rendered":"<\/p>\n<h2>\u670d\u52a1\u5668\u542f\u52a8\u9636\u6bb5\u5904\u7406<\/h2>\n<h3>\u5206\u6790\u670d\u52a1\u5668\u542f\u52a8\u9636\u6bb5\u90fd\u90fd\u9700\u8981\u521d\u59cb\u5316\u4ec0\u4e48&#xff1f;<\/h3>\n<h4 style=\"background-color:transparent\">1.\u521d\u59cb\u5316Spring\u5bb9\u5668<\/h4>\n<li>\u7ec4\u4ef6\u626b\u63cf\u5305\u4e0b\u7684\u7c7b\u7eb3\u5165IOC\u5bb9\u5668\u7ba1\u7406<\/li>\n<li>\u521b\u5efa\u89c6\u56fe\u89e3\u6790\u5668\u5bf9\u8c61<\/li>\n<li>\u521b\u5efa\u6240\u6709\u7684\u62e6\u622a\u5668\u5bf9\u8c61<\/li>\n<li>\u626b\u63cf\u8fd9\u548c\u5305\u4e0b\u7684\u6240\u6709\u7c7borg.myspringmvc.web.servlet.mvc.method.annotation&#xff0c;\u5168\u90e8\u5b9e\u4f8b\u5316&#xff0c;\u7eb3\u5165IoC\u5bb9\u5668\u7ba1\u7406<\/li>\n<p>Spring\u5bb9\u5668&#xff1a;ApplicationContext<\/p>\n<p>SpringWeb\u5bb9\u5668&#xff1a;WebApplicationContext<\/p>\n<h5>\u7ec4\u4ef6\u626b\u63cf<\/h5>\n<h5>springmvc.xml\u6587\u4ef6<\/h5>\n<p>&lt;?xml version&#061;&#034;1.0&#034; encoding&#061;&#034;utf-8&#034; ?&gt;<\/p>\n<p>&lt;beans&gt;<br \/>\n    &lt;!&#8211;<br \/>\n        \u62e6\u622a\u5668<br \/>\n        \u89c6\u56fe\u89e3\u6790\u5668<br \/>\n        \u7ec4\u4ef6\u626b\u63cf<br \/>\n    &#8211;&gt;<\/p>\n<p>    &lt;component-scan base-package&#061;&#034;org.springmvc.oa.controller&#034;\/&gt;<br \/>\n&lt;!&#8211;    \u89c6\u56fe\u89e3\u6790\u5668&#8211;&gt;<br \/>\n    &lt;bean class&#061;&#034;org.springmvc.web.servlet.view.InternalResourceViewResolver&#034;&gt;<br \/>\n&lt;!&#8211;        \u524d\u7f00&#8211;&gt;<br \/>\n        &lt;property name&#061;&#034;prefix&#034;&gt;<br \/>\n            &lt;value&gt;\/WEB-INF\/jsp\/&lt;\/value&gt;<br \/>\n        &lt;\/property&gt;<br \/>\n&lt;!&#8211;        \u540e\u7f00&#8211;&gt;<br \/>\n        &lt;property name&#061;&#034;suffix&#034;&gt;<br \/>\n            &lt;value&gt;.jsp&lt;\/value&gt;<br \/>\n        &lt;\/property&gt;<br \/>\n    &lt;\/bean&gt;<\/p>\n<p>&lt;!&#8211;    \u62e6\u622a\u5668&#8211;&gt;<br \/>\n    &lt;interceptors&gt;<br \/>\n        &lt;bean class&#061;&#034;org.springmvc.oa.interceptors.Interceptor1&#034;&gt;&lt;\/bean&gt;<br \/>\n        &lt;bean class&#061;&#034;org.springmvc.oa.interceptors.Interceptor2&#034;&gt;&lt;\/bean&gt;<br \/>\n    &lt;\/interceptors&gt;<br \/>\n&lt;\/beans&gt; <\/p>\n<p>\u6dfb\u52a0\u89e3\u6790XML\u6587\u4ef6\u7684\u4f9d\u8d56<\/p>\n<p>&lt;!&#8211;dom4j&#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;dom4j&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;dom4j&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;1.6.1&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt;<br \/>\n&lt;!&#8211;jaxen&#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;jaxen&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;jaxen&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;1.1.6&lt;\/version&gt;<br \/>\n&lt;\/dependency&gt; <\/p>\n<h6>\u00a0ApplicationContext<\/h6>\n<p>public class ApplicationContext {<br \/>\n    private Map&lt;String, Object&gt; beanMap &#061; new HashMap&lt;&gt;();<\/p>\n<p>    public ApplicationContext(String xmlPath) throws Exception {<br \/>\n        \/\/ \u7ec4\u4ef6\u626b\u63cf<br \/>\n        SAXReader saxReader &#061; new SAXReader();<br \/>\n        Document document &#061; saxReader.read(new File(xmlPath));<br \/>\n        Element componentScanElement &#061; (Element)document.selectSingleNode(&#034;\/beans\/context:component-scan&#034;);<br \/>\n        String basePackage &#061; componentScanElement.attributeValue(&#034;base-package&#034;);<br \/>\n        System.out.println(&#034;\u7ec4\u4ef6\u626b\u63cf&#xff1a;&#034; &#043; basePackage);<br \/>\n        componentScan(basePackage);<br \/>\n        System.out.println(&#034;Spring Web\u5bb9\u5668\u5f53\u4e0b\u72b6\u6001&#xff1a;&#034; &#043; beanMap);<br \/>\n    }<\/p>\n<p>    private void componentScan(String basePackage) throws Exception{<br \/>\n        String dirPath &#061; Thread.currentThread().getContextClassLoader().getResource(basePackage.replace(&#034;.&#034;, &#034;\/&#034;)).getPath();<br \/>\n        File file &#061; new File(URLDecoder.decode(dirPath));<br \/>\n        if(file.isDirectory()){<br \/>\n            File[] files &#061; file.listFiles();<br \/>\n            for (File classFile : files){<br \/>\n                if(classFile.getName().endsWith(&#034;.class&#034;)){<br \/>\n                    String className &#061; basePackage &#043; &#034;.&#034; &#043; classFile.getName().substring(0, classFile.getName().lastIndexOf(&#034;.&#034;));<br \/>\n                    Class&lt;?&gt; clazz &#061; Class.forName(className);<br \/>\n                    Constructor&lt;?&gt; defaultCon &#061; clazz.getDeclaredConstructor();<br \/>\n                    Object bean &#061; defaultCon.newInstance();<br \/>\n                    beanMap.put(firstCharLowerCase(clazz.getSimpleName()), bean);<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    }<\/p>\n<p>    private String firstCharLowerCase(String simpleName) {<br \/>\n        return simpleName.substring(0, 1).toLowerCase() &#043; simpleName.substring(1);<br \/>\n    }<\/p>\n<p>    public Object getBean(String beanName){<br \/>\n        return beanMap.get(beanName);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u00a0\u7ec4\u4ef6\u626b\u63cf\u7684beanMap\u7ed3\u679c<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"354\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023759-68103b87475f6.png\" width=\"1410\" \/><\/p>\n<h6>WebApplicationContext<\/h6>\n<p>package org.springmvc.web.context;<\/p>\n<p>import jakarta.servlet.ServletContext;<br \/>\nimport org.dom4j.DocumentException;<br \/>\nimport org.springmvc.context.ApplicationContext;<\/p>\n<p>import java.lang.reflect.InvocationTargetException;<\/p>\n<p>public class WebApplicationContext extends ApplicationContext {<br \/>\n    private ServletContext servletContext;<br \/>\n    private String springMvcConfigPath;<\/p>\n<p>    public WebApplicationContext() {<br \/>\n    }<\/p>\n<p>    public WebApplicationContext(ServletContext servletContext, String springMvcConfigPath) throws DocumentException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {<br \/>\n        super(springMvcConfigPath);<br \/>\n        this.servletContext &#061; servletContext;<br \/>\n        this.springMvcConfigPath &#061; springMvcConfigPath;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return servletContext<br \/>\n     *\/<br \/>\n    public ServletContext getServletContext() {<br \/>\n        return servletContext;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param servletContext<br \/>\n     *\/<br \/>\n    public void setServletContext(ServletContext servletContext) {<br \/>\n        this.servletContext &#061; servletContext;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return springMvcConfigPath<br \/>\n     *\/<br \/>\n    public String getSpringMvcConfigPath() {<br \/>\n        return springMvcConfigPath;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param springMvcConfigPath<br \/>\n     *\/<br \/>\n    public void setSpringMvcConfigPath(String springMvcConfigPath) {<br \/>\n        this.springMvcConfigPath &#061; springMvcConfigPath;<br \/>\n    }<\/p>\n<p>    public String toString() {<br \/>\n        return &#034;WebApplicationContext{servletContext &#061; &#034; &#043; servletContext &#043; &#034;, springMvcConfigPath &#061; &#034; &#043; springMvcConfigPath &#043; &#034;}&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<h5 style=\"background-color:transparent\">\u521b\u5efa\u89c6\u56fe\u89e3\u6790\u5668\u5bf9\u8c61\u00a0<\/h5>\n<h6>InternalResourceViewResolver<\/h6>\n<p>\u5185\u90e8\u8d44\u6e90\u89c6\u56fe\u89e3\u6790\u5668<\/p>\n<p>package org.springmvc.web.servlet.view;<\/p>\n<p>import org.springmvc.web.servlet.View;<br \/>\nimport org.springmvc.web.servlet.ViewResolver;<\/p>\n<p>import java.util.Locale;<\/p>\n<p>\/*<br \/>\n    \u5185\u90e8\u8d44\u6e90\u89e3\u6790\u5668<br \/>\n *\/<br \/>\npublic class InternalResourceViewResolver implements ViewResolver {<br \/>\n    String prefix;<br \/>\n    String suffix;<\/p>\n<p>    public InternalResourceViewResolver() {<br \/>\n    }<\/p>\n<p>    public InternalResourceViewResolver(String prefix, String suffix) {<br \/>\n        this.prefix &#061; prefix;<br \/>\n        this.suffix &#061; suffix;<br \/>\n    }<br \/>\n    \/*<br \/>\n    \u5c06\u903b\u8f91\u8bd5\u56fe\u540d\u5b57 \u8f6c\u6362\u4e3a\u7269\u7406\u89c6\u56fe\u540d\u79f0 \u5e76\u4ee5View\u5bf9\u8c61\u8fd4\u56de<br \/>\n     *\/<br \/>\n    &#064;Override<br \/>\n    public View resolveViewName(String viewName, Locale locale) throws Exception {<\/p>\n<p>        return new InternalResourceView(&#034;text\/html;charset-UTF-8&#034;, prefix&#043;viewName&#043;suffix);<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return prefix<br \/>\n     *\/<br \/>\n    public String getPrefix() {<br \/>\n        return prefix;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param prefix<br \/>\n     *\/<br \/>\n    public void setPrefix(String prefix) {<br \/>\n        this.prefix &#061; prefix;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return suffix<br \/>\n     *\/<br \/>\n    public String getSuffix() {<br \/>\n        return suffix;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param suffix<br \/>\n     *\/<br \/>\n    public void setSuffix(String suffix) {<br \/>\n        this.suffix &#061; suffix;<br \/>\n    }<\/p>\n<p>    public String toString() {<br \/>\n        return &#034;InternalResourceViewResolver{prefix &#061; &#034; &#043; prefix &#043; &#034;, suffix &#061; &#034; &#043; suffix &#043; &#034;}&#034;;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return suffix<br \/>\n     *\/<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return prefix<br \/>\n     *\/<\/p>\n<p>}<\/p>\n<p>\u00a0\u521b\u5efa\u89c6\u56fe\u89e3\u6790\u5668\u5bf9\u8c61<\/p>\n<p>private void createViewResolver(Element viewResolverElement) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {<br \/>\n        String viewResolverClassName&#061;viewResolverElement.attributeValue(&#034;class&#034;);<br \/>\n        System.out.println(&#034;\u89c6\u56fe\u89e3\u6790\u5668\u7c7b\u540d&#034;&#043;viewResolverClassName);<br \/>\n        Class clazz&#061; Class.forName(viewResolverClassName);<br \/>\n        Object bean &#061; clazz.newInstance();<br \/>\n        \/\/bean\u8282\u70b9\u4e0b\u5b50\u8282\u70b9property<br \/>\n        List&lt;Element&gt; propertyElements&#061;viewResolverElement.elements(Constant.PROPERTY_TAG_NAME);<br \/>\n        for(Element propertyElement:propertyElements){<br \/>\n            \/\/\u83b7\u53d6\u5c5e\u6027\u540d<br \/>\n            String fieldName&#061;propertyElement.attributeValue(Constant.PROPERTY_NAME);<br \/>\n            String value&#061;propertyElement.element(Constant.PROPERTY_VALUE).getTextTrim();<br \/>\n            \/\/\u5c06\u5c5e\u6027\u540d\u8f6c\u4e3aset\u65b9\u6cd5<br \/>\n            String setMethodName&#061;fieldNameToSetMethod(fieldName);<\/p>\n<p>            \/\/\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u8c03\u7528set\u65b9\u6cd5<br \/>\n            Method setMethod &#061; clazz.getDeclaredMethod(setMethodName,String.class);<br \/>\n            setMethod.invoke(bean,value);<br \/>\n        }<br \/>\n        beanMap.put(Constant.VIEW_RESOLVER, bean);<br \/>\n    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"411\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023759-68103b878bd88.png\" width=\"1399\" \/>\u00a0<\/p>\n<h5>\u521b\u5efa\u62e6\u622a\u5668\u5bf9\u8c61<\/h5>\n<p> private void createInteceptor(Element interceptorElement) throws ClassNotFoundException, InstantiationException, IllegalAccessException {<br \/>\n        List&lt;Element&gt; interceptorElements&#061;interceptorElement.elements(&#034;bean&#034;);<br \/>\n        List&lt;HandlerInterceptor&gt; interceptors&#061;new ArrayList&lt;&gt;();<br \/>\n        for(Element interceptor:interceptorElements){<\/p>\n<p>            Class&lt;?&gt; clazz&#061;Class.forName(interceptor.attributeValue(&#034;class&#034;));<br \/>\n            Object bean&#061;clazz.newInstance();<br \/>\n            interceptors.add((HandlerInterceptor)bean);<br \/>\n        }<br \/>\n        beanMap.put(Constant.INTERCEPROTS,interceptors);<br \/>\n    } <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"690\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023759-68103b87d8660.png\" width=\"1404\" \/>\u00a0<\/p>\n<h4>2.\u521d\u59cb\u5316HandlerMapping\u548c\u521d\u59cb\u5316HandlerAdapter\u00a0<\/h4>\n<p>\u521d\u59cb\u5316annotation\u5305\u4e0b\u6240\u6709\u7c7b\u7684\u5b9e\u4f8b<\/p>\n<p> private void createHandlerMapping(String defaultPackage,Map&lt;RequestMappingInfo, HandlerMethod&gt;map) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {<br \/>\n        \/\/System.out.println(defaultPackage);<br \/>\n        \/\/ \u5c06\u8fd9\u4e2a\u5305\u4e0b\u6240\u6709\u7684\u7c7b\u5b9e\u4f8b\u5316&#xff1a;org.myspringmvc.web.servlet.mvc.method.annotation<\/p>\n<p>        \/\/\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<br \/>\n        String dirPath&#061;Thread.currentThread().getContextClassLoader().getResource(defaultPackage.replace(&#039;.&#039;, &#039;\/&#039;)).getPath();<br \/>\n        File file&#061;new File(URLDecoder.decode(dirPath));<br \/>\n        \/\/\u83b7\u53d6\u91cc\u9762\u6240\u6709\u7684\u5b50\u6587\u4ef6<br \/>\n        if(file.isDirectory()){<br \/>\n            File[] files &#061; file.listFiles();<br \/>\n            for(File classFile : files){<br \/>\n                if(classFile.getName().endsWith(Constant.SUFFER_CLASS)){<br \/>\n                    \/\/\u5168\u7c7b\u540d<br \/>\n                    String className&#061;defaultPackage&#043;&#034;.&#034;&#043;classFile.getName().substring(0,classFile.getName().lastIndexOf(&#034;.&#034;));<br \/>\n                    Class&lt;?&gt;clazz&#061;Class.forName(className);<br \/>\n                    if(HandlerMapping.class.isAssignableFrom(clazz)){<br \/>\n                        Constructor&lt;?&gt;defaultCon&#061; clazz.getDeclaredConstructor(Map.class);<br \/>\n                        Object bean&#061;defaultCon.newInstance(map);<br \/>\n                        beanMap.put(Constant.HANDLER_MAPPING, bean);<br \/>\n                    }<br \/>\n                    if(HandlerAdapter.class.isAssignableFrom(clazz)){<br \/>\n                        Constructor&lt;?&gt;defaultCon&#061; clazz.getDeclaredConstructor();<br \/>\n                        Object bean&#061;defaultCon.newInstance();<br \/>\n                        beanMap.put(Constant.HANDLER_ADAPTER, bean);<br \/>\n                    }<br \/>\n                }<br \/>\n            }<br \/>\n        }<br \/>\n    } <\/p>\n<p>\u00a0<img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"673\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023800-68103b8849d3a.png\" width=\"1400\" \/><\/p>\n<\/p>\n<h3>2.\u521d\u59cb\u5316Servlet\u5bf9\u8c61<\/h3>\n<h4>DispatchServlet init()<\/h4>\n<p> public void init(){<br \/>\n        \/\/\u627e\u5230springmvc.xml\u6587\u4ef6<br \/>\n        \/*<br \/>\n            &lt;init-param&gt;<br \/>\n                &lt;param-name&gt;contextConfigLocation&lt;\/param-name&gt;<br \/>\n                &lt;param-value&gt;classpath:springmvc.xml&lt;\/param-value&gt;<br \/>\n            &lt;\/init-param&gt;<br \/>\n         *\/<br \/>\n        \/\/\u6839\u636e\u4ee5\u4e0a\u914d\u7f6espringmvc.xml\u6587\u4ef6<br \/>\n        \/\/\u83b7\u53d6SpringConfig\u5bf9\u8c61(Servlet\u914d\u7f6e\u4fe1\u606f\u5bf9\u8c61 \u8be5\u5bf9\u8c61\u7531web\u5bb9\u5668\u81ea\u52a8\u521b\u5efa \u5e76\u4e14\u5c06\u5176\u4f20\u9012\u7ed9init\u65b9\u6cd5<br \/>\n        \/\/ )<br \/>\n        ServletConfig servletConfig &#061; this.getServletConfig();<br \/>\n        String contextConfigLocation &#061; servletConfig.getInitParameter(Constant.CONTEXT_CONFIG_LOCATION);<br \/>\n        String springMvcXmlPath&#061;getSpringMvcXmlPath(contextConfigLocation);<br \/>\n        System.out.println(&#034;Spring MVC\u914d\u7f6e\u6587\u4ef6\u8def\u5f84\u89e3\u6790\u5b8c\u6210&#xff1a;&#034; &#043; springMvcXmlPath);<\/p>\n<p>        \/\/\u521d\u59cb\u5316Spring Web\u5bb9\u5668<br \/>\n        WebApplicationContext webApplicationContext &#061; null;<br \/>\n        try {<br \/>\n            webApplicationContext &#061; new WebApplicationContext(this.getServletContext(), springMvcXmlPath);<br \/>\n        } catch (DocumentException e) {<br \/>\n            throw new RuntimeException(e);<br \/>\n        } catch (ClassNotFoundException e) {<br \/>\n            throw new RuntimeException(e);<br \/>\n        } catch (InvocationTargetException e) {<br \/>\n            throw new RuntimeException(e);<br \/>\n        } catch (NoSuchMethodException e) {<br \/>\n            throw new RuntimeException(e);<br \/>\n        } catch (InstantiationException e) {<br \/>\n            throw new RuntimeException(e);<br \/>\n        } catch (IllegalAccessException e) {<br \/>\n            throw new RuntimeException(e);<br \/>\n        }<br \/>\n        this.getServletContext().setAttribute(Constant.WEB_APPLICATION_CONTEXT, webApplicationContext);<\/p>\n<p>        \/\/\u521d\u59cb\u5316HandlerMapping<br \/>\n        this.handlerMapping&#061;(HandlerMapping)webApplicationContext.getBean(Constant.HANDLER_MAPPING);<br \/>\n        \/\/\u521d\u59cb\u5316HandlerAdapter<br \/>\n        this.handlerAdapter&#061;(HandlerAdapter)webApplicationContext.getBean(Constant.HANDLER_ADAPTER);<br \/>\n        \/\/\u521d\u59cb\u5316\u8bd5\u56fe\u89e3\u6790\u5668<br \/>\n        this.viewResolver&#061;(ViewResolver)webApplicationContext.getBean(Constant.VIEW_RESOLVER);<\/p>\n<p>    }<\/p>\n<p>    private String getSpringMvcXmlPath(String contextConfigLocation) {<br \/>\n        if(contextConfigLocation.startsWith(Constant.CLASSPATH)){<br \/>\n            String path&#061;contextConfigLocation.substring(Constant.CLASSPATH.length()).trim();<br \/>\n            \/\/\u7c7b\u8def\u5f84\u4e2d\u627e<br \/>\n            String springMvcXmlPath&#061;Thread.currentThread().getContextClassLoader().getResource(path).getPath();<br \/>\n            \/\/ \u5bf9\u8def\u5f84\u89e3\u7801&#xff0c;\u9632\u6b62\u8def\u5f84\u4e2d\u6709 % \u7b49\u5b57\u7b26\u3002<br \/>\n            return URLDecoder.decode(springMvcXmlPath, Charset.defaultCharset());<\/p>\n<p>        }<br \/>\n        return null;<br \/>\n    } <\/p>\n<h4>doDispath\u65b9\u6cd5<\/h4>\n<p>protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {<br \/>\n        try {<br \/>\n            doDispatch(req, resp);<br \/>\n        } catch (Exception e) {<br \/>\n            throw new RuntimeException(e);<br \/>\n        }<br \/>\n    }<br \/>\n    \/**<br \/>\n     * \u5904\u7406\u8bf7\u6c42\u7684\u6838\u5fc3\u65b9\u6cd5<br \/>\n     * &#064;param request<br \/>\n     * &#064;param response<br \/>\n     * &#064;throws ServletException<br \/>\n     * &#064;throws IOException<br \/>\n     *\/<br \/>\n    private void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {<br \/>\n        \/\/\u8bbf\u95ee\u6d4f\u89c8\u5668\u624d\u80fd\u65ad\u70b9\u5230\u8fd9<br \/>\n        \/\/\u8bf7\u6c42\u5bf9\u8c61\u83b7\u53d6\u5bf9\u5e94\u7684\u5904\u7406\u5668\u6267\u884c\u8fde\u5bf9\u8c61<br \/>\n        HandlerExecutionChain mappedHandler&#061;handlerMapping.getHandler(request);<br \/>\n        \/\/\u83b7\u53d6\u5904\u7406\u5668\u9002\u914d\u5668\u5bf9\u8c61<br \/>\n        HandlerAdapter adapter &#061; this.handlerAdapter;<\/p>\n<p>        \/\/3.\u6267\u884c\u62e6\u622a\u5668\u4e2d\u7684preHandle\u65b9\u6cd5<br \/>\n        if(!mappedHandler.applyPreHandle(request,response)){<br \/>\n            return;\/\/false\u7684\u8bdd<br \/>\n        }<br \/>\n        \/\/4.\u6267\u884c\u5904\u7406\u5668\u65b9\u6cd5 \u8fd4\u56deModelAndView<br \/>\n        ModelAndView mv&#061;adapter.handle(request,response,mappedHandler.getHandler());<br \/>\n        System.out.println(&#034;\u5904\u7406\u5668\u6267\u884c\u8fde\u5bf9\u8c61&#xff1a;&#034;&#043;mappedHandler);<\/p>\n<p>        \/\/5.\u6267\u884c\u62e6\u622a\u5668\u4e2dpostHanlder<br \/>\n        mappedHandler.applyPostHandle(request,response,mv);<\/p>\n<p>        \/\/6.\u8bd5\u56fe\u89e3\u6790\u5668 \u903b\u8f91\u8bd5\u56fe\u540d\u79f0\u8f6c\u4e3a\u7269\u7406\u89c6\u56fe\u540d\u79f0<br \/>\n        View view&#061;viewResolver.resolveViewName(mv.getView().toString(),request.getLocale());<br \/>\n        view.render(mv.getModel(),request,response);<\/p>\n<p>        \/\/\u6267\u884c\u62e6\u622a\u5668\u6e32\u67d3\u4e4b\u540e\u7684afterCompletetion\u65b9\u6cd5<\/p>\n<p>        mappedHandler.triggerAfterCompletion(request,response,null);<br \/>\n    } <\/p>\n<h5 style=\"background-color:transparent\">\u6839\u636e\u8bf7\u6c42\u83b7\u53d6\u5904\u7406\u5668\u6267\u884c\u94fe<\/h5>\n<p>HandlerExecutionChain mappedHandler&#061;handlerMapping.getHandler(request);<\/p>\n<h6>HandlerMapping<\/h6>\n<p>public interface HandlerMapping {<br \/>\n    \/**<br \/>\n     * ClassName: HandlerMapping<br \/>\n     * Description: \u4e3b\u8981\u662f\u901a\u8fc7\u8bf7\u6c42\u83b7\u53d6\u5bf9\u5e94\u7684\u5904\u7406\u5668\u6267\u884c\u94fe\u3002<br \/>\n     * Datetime: 2024\/4\/2 8:50<br \/>\n     * Author: \u8001\u675c&#064;\u52a8\u529b\u8282\u70b9<br \/>\n     * Version: 1.0<br \/>\n     *\/<br \/>\n    HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception;<br \/>\n}<\/p>\n<h6>\u00a0HandlerMethod<\/h6>\n<p>package org.springmvc.web.method;<\/p>\n<p>import java.lang.reflect.Method;<\/p>\n<p>\/*<br \/>\n    \u5904\u7406\u5668\u65b9\u6cd5 controller<br \/>\n *\/<br \/>\npublic class HandlerMethod {<br \/>\n    \/*<br \/>\n        \u5904\u7406\u5668\u5bf9\u8c61<br \/>\n     *\/<br \/>\n    private Object handler;<br \/>\n    \/**<br \/>\n     * \u8981\u6267\u884c\u7684\u65b9\u6cd5<br \/>\n     *\/<br \/>\n    private Method method;<\/p>\n<p>    public HandlerMethod() {<br \/>\n    }<\/p>\n<p>    public HandlerMethod(Object handler, Method method) {<br \/>\n        this.handler &#061; handler;<br \/>\n        this.method &#061; method;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return handler<br \/>\n     *\/<br \/>\n    public Object getHandler() {<br \/>\n        return handler;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param handler<br \/>\n     *\/<br \/>\n    public void setHandler(Object handler) {<br \/>\n        this.handler &#061; handler;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return method<br \/>\n     *\/<br \/>\n    public Method getMethod() {<br \/>\n        return method;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param method<br \/>\n     *\/<br \/>\n    public void setMethod(Method method) {<br \/>\n        this.method &#061; method;<br \/>\n    }<\/p>\n<p>    public String toString() {<br \/>\n        return &#034;HandlerMethod{handler &#061; &#034; &#043; handler &#043; &#034;, method &#061; &#034; &#043; method &#043; &#034;}&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<h6>RequestMappingInfo<\/h6>\n<p>package org.springmvc.web.servlet;<\/p>\n<p>import java.util.Objects;<\/p>\n<p>public class RequestMappingInfo {<br \/>\n    private String requestURI;<\/p>\n<p>    &#064;Override<br \/>\n    public boolean equals(Object o) {<br \/>\n        if (this &#061;&#061; o) return true;<br \/>\n        if (o &#061;&#061; null || getClass() !&#061; o.getClass()) return false;<br \/>\n        RequestMappingInfo that &#061; (RequestMappingInfo) o;<br \/>\n        return Objects.equals(requestURI, that.requestURI) &amp;&amp; Objects.equals(method, that.method);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public int hashCode() {<br \/>\n        return Objects.hash(requestURI, method);<br \/>\n    }<\/p>\n<p>    \/\/GET POST PUT DELETE<br \/>\n    private String method;<\/p>\n<p>    public RequestMappingInfo() {<br \/>\n    }<\/p>\n<p>    public RequestMappingInfo(String requestURI, String method) {<br \/>\n        this.requestURI &#061; requestURI;<br \/>\n        this.method &#061; method;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return requestURI<br \/>\n     *\/<br \/>\n    public String getRequestURI() {<br \/>\n        return requestURI;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param requestURI<br \/>\n     *\/<br \/>\n    public void setRequestURI(String requestURI) {<br \/>\n        this.requestURI &#061; requestURI;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return method<br \/>\n     *\/<br \/>\n    public String getMethod() {<br \/>\n        return method;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param method<br \/>\n     *\/<br \/>\n    public void setMethod(String method) {<br \/>\n        this.method &#061; method;<br \/>\n    }<\/p>\n<p>    public String toString() {<br \/>\n        return &#034;RequestMappingInfo{requestURI &#061; &#034; &#043; requestURI &#043; &#034;, method &#061; &#034; &#043; method &#043; &#034;}&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<h6>RequestMappingHandlerMapping<\/h6>\n<p>package org.springmvc.web.servlet.mvc.method.annotation;<\/p>\n<p>import jakarta.servlet.http.HttpServletRequest;<br \/>\nimport org.springmvc.web.constant.Constant;<br \/>\nimport org.springmvc.web.context.WebApplicationContext;<br \/>\nimport org.springmvc.web.method.HandlerMethod;<br \/>\nimport org.springmvc.web.servlet.*;<\/p>\n<p>import java.util.List;<br \/>\nimport java.util.Map;<\/p>\n<p>\/*<br \/>\n    \u5904\u7406\u5668\u6620\u5c04\u5668 \u4e13\u95e8\u4e3a &#064;RequestMapping \u6ce8\u89e3\u7684\u7c7b\u670d\u52a1\u7684\u5904\u7406\u5668\u6620\u5c04\u5668<br \/>\n    \u901a\u8fc7\u524d\u7aef\u63d0\u4ea4\u7684\u8bf7\u6c42 \u6765\u5e95\u5c42\u6620\u5c04\u8981\u6267\u884c\u7684HandlerMethod<br \/>\n *\/<br \/>\npublic class RequestMappingHandlerMapping implements HandlerMapping {<\/p>\n<p>    private Map&lt;RequestMappingInfo, HandlerMethod&gt; map;<\/p>\n<p>    \/*<br \/>\n        \u521b\u5efaHandlerMapping\u5bf9\u8c61\u65f6 \u7ed9map\u8d4b\u503c<br \/>\n     *\/<\/p>\n<p>    public RequestMappingHandlerMapping(Map&lt;RequestMappingInfo, HandlerMethod&gt; map) {<br \/>\n        this.map &#061; map;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {<\/p>\n<p>        \/\/\u901a\u8fc7request\u5bf9\u8c61 \u83b7\u53d6\u8bf7\u6c42\u8def\u5f84&#xff0c;\u8bf7\u6c42\u65b9\u5f0f \u5c01\u88c5\u7a0bRequestMappingInfo<br \/>\n        RequestMappingInfo requestMappingInfo&#061;new RequestMappingInfo(request.getServletPath(),request.getMethod());<br \/>\n        \/\/******************\u5fc5\u987b\u91cd\u5199 \u5982\u679c\u8865\u5145\u5199 key\u662f\u5bf9\u8c61 \u5bf9\u4e8e\u76f8\u540c\u8bf7\u6c42\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61 \u53ef\u80fd\u8ba4\u4e3a\u662f\u4e0d\u540c\u7684<br \/>\n        HandlerExecutionChain handlerExecutionChain &#061;new HandlerExecutionChain();<br \/>\n        \/\/\u7ed9\u6267\u884c\u8fde\u8bbe\u7f6ehandlerMethod<br \/>\n        handlerExecutionChain.setHandler(map.get(requestMappingInfo));<br \/>\n        \/\/\u7ed9\u6267\u884c\u8fde\u8bbe\u7f6e\u62e6\u622a\u5668<\/p>\n<p>        \/\/\u901a\u8fc7Spring\u5bb9\u5668<br \/>\n        WebApplicationContext webApplicationContext&#061;(WebApplicationContext)request.getServletContext().getAttribute(Constant.WEB_APPLICATION_CONTEXT);<br \/>\n        List&lt;HandlerInterceptor&gt; interceptors&#061;(List&lt;HandlerInterceptor&gt;)webApplicationContext.getBean(Constant.INTERCEPROTS);<br \/>\n        handlerExecutionChain.setInterceptors(interceptors);<br \/>\n        return handlerExecutionChain;<br \/>\n    }<br \/>\n}<\/p>\n<h6>\u5904\u7406\u5668\u6267\u884c\u94fe\u5bf9\u8c61\u00a0<\/h6>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"253\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023800-68103b88ada8f.png\" width=\"1295\" \/><\/p>\n<h6>\u00a0\u6ce8\u610f&#xff1a;<\/h6>\n<p> public RequestMappingHandlerMapping(Map&lt;RequestMappingInfo, HandlerMethod&gt; map) { \u00a0 \u00a0 \u00a0 \u00a0 this.map &#061; map; \u00a0 \u00a0 }<\/p>\n<p>\u5305\u626b\u63cf\u7684\u65f6\u5019\u521d\u59cb\u5316RequestMappingHandlerMapping\u65f6 \u8981\u4f20\u5165Map\u5bf9\u8c61 \u6240\u4ee5\u5305\u626b\u63cf\u8981\u6539\u4e3a\u4e0b\u9762\u7684\u4ee3\u7801<\/p>\n<p>private Map&lt;RequestMappingInfo, HandlerMethod&gt; componentScan(String basePackage) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {<\/p>\n<p>        Map&lt;RequestMappingInfo, HandlerMethod&gt;handlerMethodMap&#061;new HashMap&lt;&gt;();<br \/>\n        \/\/\u83b7\u53d6\u7edd\u5bf9\u8def\u5f84<br \/>\n        String dirPath&#061;Thread.currentThread().getContextClassLoader().getResource(basePackage<br \/>\n                .replace(&#034;.&#034;,&#034;\/&#034;)).getPath();<br \/>\n        File file&#061;new File(URLDecoder.decode(dirPath));\/\/\u5c01\u88c5\u4e3a\u6587\u4ef6<br \/>\n        \/\/\u83b7\u53d6\u91cc\u9762\u6240\u6709\u7684\u5b50\u6587\u4ef6<br \/>\n        if(file.isDirectory()){<br \/>\n            File[] files &#061; file.listFiles();<br \/>\n            for(File classFile:files){<br \/>\n                if(classFile.getName().endsWith(Constant.SUFFER_CLASS)){<br \/>\n                    String className&#061;basePackage&#043;&#034;.&#034;&#043;classFile.getName().substring(0,classFile.getName().lastIndexOf(&#034;.&#034;));<\/p>\n<p>                    Class&lt;?&gt; clazz &#061; Class.forName(className);<br \/>\n                    Constructor&lt;?&gt;defaultCon&#061; clazz.getDeclaredConstructor();<br \/>\n                    \/\/System.out.println(&#034;\u7b80\u5355\u7c7b\u540d&#034;&#043;clazz.getSimpleName());<br \/>\n                    Object bean&#061;defaultCon.newInstance();<br \/>\n                    beanMap.put(firstCharLowerCase(clazz.getSimpleName()), bean);<br \/>\n                    \/\/UserController \u9ed8\u8ba4beanName \u9996\u5b57\u6bcd\u5c0f\u5199<\/p>\n<p>                    \/\/\u83b7\u53d6\u7c7b\u4e0a\u9762\u7684\u6ce8\u89e3 &#064;Controller\u6ce8\u89e3\u5305\u56f4<br \/>\n                    if(clazz.isAnnotationPresent(Controller.class)){\/\/\u88abController\u6ce8\u89e3\u5305\u88f9<br \/>\n                        \/\/\u83b7\u53d6\u7c7b\u4e2d\u6240\u6709\u7684\u65b9\u6cd5<br \/>\n                        Method []methods&#061;clazz.getDeclaredMethods();<br \/>\n                        for(Method method:methods){<br \/>\n                            \/\/\u5982\u679c\u8be5\u6ce8\u89e3\u88abRequestMapping\u6ce8\u89e3\u5305\u88f9<br \/>\n                            if(method.isAnnotationPresent(RequestMapping.class)){<br \/>\n                                RequestMapping requestMapping&#061;method.getAnnotation(RequestMapping.class);<br \/>\n                                String requestURI&#061;requestMapping.value()[0];<br \/>\n                                String requestMethod&#061;requestMapping.method().toString();\/\/  GET<br \/>\n                                RequestMappingInfo requestMappingInfo&#061;new RequestMappingInfo(requestURI,requestMethod);<br \/>\n                                HandlerMethod handlerMethod&#061;new HandlerMethod();\/\/\u8bf7\u6c42\u7684\u65b9\u6cd5<br \/>\n                                handlerMethod.setHandler(bean);<br \/>\n                                handlerMethod.setMethod(method);<br \/>\n                                handlerMethodMap.put(requestMappingInfo,handlerMethod);<br \/>\n                            }<br \/>\n                        }<br \/>\n                    }<br \/>\n                }<br \/>\n            }<\/p>\n<p>        } <\/p>\n<h6>\u6267\u884c\u62e6\u622a\u5668\u7684\u4e2dpreHandler\u65b9\u6cd5<\/h6>\n<p> public boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {<br \/>\n        for (int i &#061; 0; i &lt;interceptors.size() ; i&#043;&#043;) {<br \/>\n            HandlerInterceptor handlerInterceptor &#061; interceptors.get(i);<br \/>\n            boolean result&#061;handlerInterceptor.preHandle(request,response,handler);<\/p>\n<p>            if(!result){<br \/>\n                \/\/\u6267\u884c\u62e6\u622a\u5668\u7684AfterCompletion<br \/>\n                triggerAfterCompletion(request,response,handler);<br \/>\n                return false;<br \/>\n            }<br \/>\n        }<br \/>\n        return true;<br \/>\n    } <\/p>\n<h6>.\u6267\u884c\u5904\u7406\u5668\u65b9\u6cd5 \u8fd4\u56deModelAndView<\/h6>\n<p>public interface HandlerAdapter {<br \/>\n    ModelAndView handle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception;<br \/>\n}<\/p>\n<p>public class RequestMappingHandlerAdapter implements HandlerAdapter {<br \/>\n    &#064;Override<br \/>\n    public ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {<br \/>\n        \/\/\u9700\u8981\u8c03\u7528\u5904\u7406\u5668\u65b9\u6cd5<br \/>\n        HandlerMethod handlerMethod &#061; (HandlerMethod)handler;<\/p>\n<p>        \/\/\u83b7\u53d6\u63a7\u5236\u5668<br \/>\n        Object controller&#061;handlerMethod.getHandler();<\/p>\n<p>        \/\/\u83b7\u53d6\u8c03\u7528\u7684\u65b9\u6cd5<br \/>\n        Method method&#061;handlerMethod.getMethod();<br \/>\n        ModelMap modelMap&#061;new ModelMap();<br \/>\n        \/\/\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u8c03\u7528<br \/>\n        String viewName&#061;(String)method.invoke(controller,modelMap);<br \/>\n        \/\/\u81ea\u5df1\u5199\u7684\u76ee\u524d\u9700\u8981 Controller\u6307\u5b9aModelMap<br \/>\n        \/\/\u8fd8\u6709\u4e00\u4e2a\u7279\u6b8a\u8981\u6c42 \u8981\u6c42\u8fd4\u56de\u903b\u8f91\u8bd5\u56fe\u540d\u79f0<\/p>\n<p>        ModelAndView modelAndView &#061; new ModelAndView();<br \/>\n        modelAndView.setViewName(viewName);<br \/>\n        modelAndView.setModel(modelMap);<br \/>\n        return modelAndView;<br \/>\n    }<br \/>\n}<\/p>\n<p>package org.springmvc.ui;<\/p>\n<p>import java.util.LinkedHashMap;<\/p>\n<p>public class ModelMap extends LinkedHashMap&lt;String, Object&gt; {<br \/>\n    public ModelMap() {<br \/>\n    }<br \/>\n    public ModelMap addAttribute(String name, String value) {<br \/>\n        this.put(name,value);<br \/>\n        return this;<br \/>\n    }<br \/>\n}<\/p>\n<p>package org.springmvc.oa.controller;<\/p>\n<p>import org.springmvc.stereotype.Controller;<br \/>\nimport org.springmvc.ui.ModelMap;<br \/>\nimport org.springmvc.web.bind.annotation.RequestMapping;<br \/>\nimport org.springmvc.web.bind.annotation.RequestMethod;<\/p>\n<p>&#064;Controller<br \/>\npublic class UserController {<br \/>\n    &#064;RequestMapping(value &#061;&#034;\/index&#034;, method &#061; RequestMethod.GET)<br \/>\n    public String index(ModelMap modelMap){<br \/>\n        modelMap.put(&#034;name&#034;,&#034;liwu&#034;);<br \/>\n        return &#034;index&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<h6><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"312\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023800-68103b88f051f.png\" width=\"843\" \/><\/h6>\n<h6>\u6267\u884c\u62e6\u622a\u5668\u4e2dpostHanlder<\/h6>\n<p>public void applyPostHandle(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) throws Exception {<br \/>\n        for(int i&#061;interceptors.size()-1;i&gt;&#061;0;i&#8211;){<br \/>\n            HandlerInterceptor handlerInterceptor &#061; interceptors.get(i);<br \/>\n            handlerInterceptor.postHandle(request,response,handler,mv);<br \/>\n        }<br \/>\n    } <\/p>\n<h6>\u89c6\u56fe\u89e3\u6790\u5668 \u903b\u8f91\u8bd5\u56fe\u540d\u79f0\u8f6c\u4e3a\u7269\u7406\u89c6\u56fe\u540d\u79f0<\/h6>\n<p>public interface ViewResolver {<br \/>\n    \/*<br \/>\n        \u89e3\u6790\u903b\u8f91\u89c6\u56fe\u540d\u79f0&#xff0c;\u8fd4\u56de\u89c6\u56fe\u5bf9\u8c61<br \/>\n     *\/<br \/>\n    View resolveViewName(String viewName, Locale locale) throws Exception;<\/p>\n<p>}<br \/>\n public View resolveViewName(String viewName, Locale locale) throws Exception {<\/p>\n<p>        return new InternalResourceView(&#034;text\/html;charset-UTF-8&#034;, prefix&#043;viewName&#043;suffix);<br \/>\n   }<\/p>\n<p> public void render(Map&lt;String, ?&gt; model, HttpServletRequest request, HttpServletResponse response) throws Exception {<br \/>\n        \/\/\u8bbe\u7f6e\u54cd\u5e94\u7c7b\u578b<br \/>\n        response.setContentType(contentType);<br \/>\n        \/\/\u5c06model\u6570\u636e\u5b58\u50a8\u5230request\u57df\u4e2d<br \/>\n        \/\/\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8df3\u8f6c\u5230\u89c6\u56fe\u4ee5\u8f6c\u53d1\u65b9\u5f0f<br \/>\n        model.forEach((key,value)-&gt;{<br \/>\n            request.setAttribute(key,value);<br \/>\n        });<br \/>\n        request.getRequestDispatcher(path).forward(request,response);<br \/>\n    } <\/p>\n<h6>\u6267\u884c\u62e6\u622a\u5668\u6e32\u67d3\u4e4b\u540e\u7684afterCompletetion\u65b9\u6cd5<\/h6>\n<p> public void triggerAfterCompletion(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {<br \/>\n        for(int i&#061;interceptors.size()-1;i&gt;&#061;0;i&#8211;){<br \/>\n            HandlerInterceptor handlerInterceptor &#061; interceptors.get(i);<br \/>\n            handlerInterceptor.afterCompletion(request, response,handler, (Exception) o);<br \/>\n        }<br \/>\n    } <\/p>\n<h2> SSM\u6574\u5408<\/h2>\n<h3>\u5f15\u5165\u76f8\u5173\u4f9d\u8d56<\/h3>\n<p>&lt;?xml version&#061;&#034;1.0&#034; encoding&#061;&#034;UTF-8&#034;?&gt;<br \/>\n&lt;project xmlns&#061;&#034;http:\/\/maven.apache.org\/POM\/4.0.0&#034;<br \/>\n         xmlns:xsi&#061;&#034;http:\/\/www.w3.org\/2001\/XMLSchema-instance&#034;<br \/>\n         xsi:schemaLocation&#061;&#034;http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd&#034;&gt;<br \/>\n    &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;<\/p>\n<p>    &lt;groupId&gt;org.springmvc&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;ssm-annotation&lt;\/artifactId&gt;<br \/>\n    &lt;version&gt;1.0-SNAPSHOT&lt;\/version&gt;<br \/>\n    &lt;packaging&gt;war&lt;\/packaging&gt;<br \/>\n    &lt;dependencies&gt;<br \/>\n        &lt;!&#8211;springmvc&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.springframework&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;spring-webmvc&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;6.1.4&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<\/p>\n<p>        &lt;!&#8211;spring jdbc&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.springframework&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;spring-jdbc&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;6.1.4&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;!&#8211;mybatis&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.mybatis&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;mybatis&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;3.5.15&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;!&#8211;mybatis spring&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.mybatis&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;mybatis-spring&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;3.0.3&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;!&#8211;mysql\u9a71\u52a8&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;com.mysql&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;mysql-connector-j&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;8.3.0&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;!&#8211;\u5fb7\u9c81\u4f0a\u8fde\u63a5\u6c60&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;com.alibaba&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;druid&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;1.2.22&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;!&#8211;jackson&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;com.fasterxml.jackson.core&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;jackson-databind&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;2.17.0&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;!&#8211;servlet api&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;jakarta.servlet&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;jakarta.servlet-api&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;6.0.0&lt;\/version&gt;<br \/>\n            &lt;scope&gt;provided&lt;\/scope&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;!&#8211;logback&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;ch.qos.logback&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;logback-classic&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;1.5.3&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;!&#8211;thymeleaf\u548cspring6\u7684\u6574\u5408\u4f9d\u8d56&#8211;&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.thymeleaf&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;thymeleaf-spring6&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;3.1.2.RELEASE&lt;\/version&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n        &lt;dependency&gt;<br \/>\n            &lt;groupId&gt;org.projectlombok&lt;\/groupId&gt;<br \/>\n            &lt;artifactId&gt;lombok&lt;\/artifactId&gt;<br \/>\n            &lt;version&gt;RELEASE&lt;\/version&gt;<br \/>\n            &lt;scope&gt;compile&lt;\/scope&gt;<br \/>\n        &lt;\/dependency&gt;<br \/>\n    &lt;\/dependencies&gt;<br \/>\n    &lt;properties&gt;<br \/>\n        &lt;maven.compiler.source&gt;21&lt;\/maven.compiler.source&gt;<br \/>\n        &lt;maven.compiler.target&gt;21&lt;\/maven.compiler.target&gt;<br \/>\n        &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;<br \/>\n    &lt;\/properties&gt;<\/p>\n<p>&lt;\/project&gt; <\/p>\n<h3>\u521b\u5efa\u5305\u7ed3\u6784<\/h3>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"121\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023801-68103b8941b33.png\" width=\"160\" \/><\/p>\n<h3>\u00a0\u521b\u5efawebapp\u76ee\u5f55<\/h3>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"51\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023801-68103b89d56a6.png\" width=\"200\" \/><\/p>\n<h3 style=\"background-color:transparent\">Spring\u6574\u5408Mybatis\u00a0<\/h3>\n<h4>\u7f16\u5199mybatis.properties<\/h4>\n<p>mybatis.driverClassName&#061;com.mysql.cj.jdbc.Driver<br \/>\nmybatis.url&#061;jdbc:mysql:\/\/localhost:3306\/db03?useSSL&#061;false&amp;allowPublicKeyRetrieval&#061;true<br \/>\nmybatis.username&#061;root<br \/>\nmybatis.password&#061;123456<br \/>\nmybatis.mapperLocations&#061;classpath*:com\/gao\/ssm\/dao\/*Mapper.xml <\/p>\n<h4>MybatisProperties\u00a0<\/h4>\n<p>package com.gao.ssm.dao;<\/p>\n<p>import org.springframework.beans.factory.annotation.Value;<br \/>\nimport org.springframework.context.annotation.PropertySource;<br \/>\nimport org.springframework.stereotype.Component;<\/p>\n<p>&#064;Component<br \/>\n&#064;PropertySource(&#034;classpath:mybatis.properties&#034;)<br \/>\npublic class MybatisProperties {<br \/>\n    &#064;Value(&#034;${mybatis.driverClassName}&#034;)<br \/>\n    private String driverClassName;<br \/>\n    &#064;Value(&#034;${mybatis.url}&#034;)<br \/>\n    private String url;<br \/>\n    &#064;Value(&#034;${mybatis.username}&#034;)<br \/>\n    private String username;<br \/>\n    &#064;Value(&#034;${mybatis.password}&#034;)<br \/>\n    private String password;<br \/>\n    &#064;Value(&#034;${mybatis.mapperLocations}&#034;)<br \/>\n    private String mapperLocations;<\/p>\n<p>    public MybatisProperties() {<br \/>\n    }<\/p>\n<p>    public MybatisProperties(String driverClassName, String url, String username, String password, String mapperLocations) {<br \/>\n        this.driverClassName &#061; driverClassName;<br \/>\n        this.url &#061; url;<br \/>\n        this.username &#061; username;<br \/>\n        this.password &#061; password;<br \/>\n        this.mapperLocations &#061; mapperLocations;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return driverClassName<br \/>\n     *\/<br \/>\n    public String getDriverClassName() {<br \/>\n        return driverClassName;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param driverClassName<br \/>\n     *\/<br \/>\n    public void setDriverClassName(String driverClassName) {<br \/>\n        this.driverClassName &#061; driverClassName;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return url<br \/>\n     *\/<br \/>\n    public String getUrl() {<br \/>\n        return url;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param url<br \/>\n     *\/<br \/>\n    public void setUrl(String url) {<br \/>\n        this.url &#061; url;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return username<br \/>\n     *\/<br \/>\n    public String getUsername() {<br \/>\n        return username;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param username<br \/>\n     *\/<br \/>\n    public void setUsername(String username) {<br \/>\n        this.username &#061; username;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return password<br \/>\n     *\/<br \/>\n    public String getPassword() {<br \/>\n        return password;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param password<br \/>\n     *\/<br \/>\n    public void setPassword(String password) {<br \/>\n        this.password &#061; password;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u83b7\u53d6<br \/>\n     * &#064;return mapperLocations<br \/>\n     *\/<br \/>\n    public String getMapperLocations() {<br \/>\n        return mapperLocations;<br \/>\n    }<\/p>\n<p>    \/**<br \/>\n     * \u8bbe\u7f6e<br \/>\n     * &#064;param mapperLocations<br \/>\n     *\/<br \/>\n    public void setMapperLocations(String mapperLocations) {<br \/>\n        this.mapperLocations &#061; mapperLocations;<br \/>\n    }<\/p>\n<p>    public String toString() {<br \/>\n        return &#034;MybatisProperties{driverClassName &#061; &#034; &#043; driverClassName &#043; &#034;, url &#061; &#034; &#043; url &#043; &#034;, username &#061; &#034; &#043; username &#043; &#034;, password &#061; &#034; &#043; password &#043; &#034;, mapperLocations &#061; &#034; &#043; mapperLocations &#043; &#034;}&#034;;<br \/>\n    }<br \/>\n}<\/p>\n<h4>MybatisConfig<\/h4>\n<p>package com.gao.ssm.config;<\/p>\n<p>import com.alibaba.druid.pool.DruidDataSource;<br \/>\nimport com.gao.ssm.dao.MybatisProperties;<br \/>\nimport org.mybatis.spring.SqlSessionFactoryBean;<br \/>\nimport org.mybatis.spring.annotation.MapperScan;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.context.annotation.Bean;<br \/>\nimport org.springframework.context.annotation.ComponentScan;<br \/>\nimport org.springframework.context.annotation.Configuration;<br \/>\nimport org.springframework.context.annotation.Import;<br \/>\nimport org.springframework.core.io.Resource;<br \/>\nimport org.springframework.core.io.support.PathMatchingResourcePatternResolver;<br \/>\nimport org.springframework.core.io.support.ResourcePatternResolver;<br \/>\nimport org.springframework.jdbc.datasource.DataSourceTransactionManager;<br \/>\nimport org.springframework.transaction.PlatformTransactionManager;<\/p>\n<p>import javax.sql.DataSource;<br \/>\nimport java.io.IOException;<\/p>\n<p>&#064;Configuration<br \/>\n&#064;ComponentScan(&#034;com.gao.ssm.dao&#034;)<br \/>\n&#064;MapperScan(&#034;com.gao.ssm.dao&#034;)<br \/>\npublic class MybatisConfig {<br \/>\n    &#064;Autowired<br \/>\n    private MybatisProperties mybatisProperties;<\/p>\n<p>    &#064;Bean<br \/>\n    public DataSource dataSource(){<br \/>\n        DruidDataSource dataSource&#061;new DruidDataSource();<br \/>\n        dataSource.setUsername(mybatisProperties.getUsername());<br \/>\n        dataSource.setPassword(mybatisProperties.getPassword());<br \/>\n        dataSource.setUrl(mybatisProperties.getUrl());<br \/>\n        dataSource.setDriverClassName(mybatisProperties.getDriverClassName());<br \/>\n        return dataSource;<br \/>\n    }<\/p>\n<p>    &#064;Bean<br \/>\n    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) throws IOException {<br \/>\n        SqlSessionFactoryBean sqlSessionFactoryBean&#061;new SqlSessionFactoryBean();<br \/>\n        sqlSessionFactoryBean.setDataSource(dataSource);<\/p>\n<p>        ResourcePatternResolver resolver&#061;new PathMatchingResourcePatternResolver();<br \/>\n        Resource[]resources&#061;resolver.getResources(mybatisProperties.getMapperLocations());<br \/>\n        sqlSessionFactoryBean.setMapperLocations(resources);<br \/>\n        return sqlSessionFactoryBean;<br \/>\n    }<br \/>\n    &#064;Bean<br \/>\n    public PlatformTransactionManager platformTransactionManager(DataSource dataSource){<br \/>\n        DataSourceTransactionManager dataSourceTransactionManager &#061; new DataSourceTransactionManager();<br \/>\n        dataSourceTransactionManager.setDataSource(dataSource);<br \/>\n        return dataSourceTransactionManager;<br \/>\n    }<br \/>\n}<\/p>\n<h4>SpringConfig<\/h4>\n<p>package com.gao.ssm.config;<\/p>\n<p>import org.springframework.context.annotation.ComponentScan;<br \/>\nimport org.springframework.context.annotation.Configuration;<br \/>\nimport org.springframework.context.annotation.Import;<br \/>\nimport org.springframework.transaction.annotation.EnableTransactionManagement;<\/p>\n<p>&#064;Configuration<br \/>\n&#064;ComponentScan(&#034;com.gao.ssm.service&#034;)<br \/>\n&#064;EnableTransactionManagement<br \/>\n&#064;Import({MybatisConfig.class})<br \/>\npublic class SpringConfig {<br \/>\n}<\/p>\n<h3>Spring\u6574\u5408Spring MVC<\/h3>\n<p>package com.gao.ssm.config;<\/p>\n<p>import jakarta.servlet.Filter;<br \/>\nimport org.springframework.context.annotation.Configuration;<br \/>\nimport org.springframework.web.filter.CharacterEncodingFilter;<br \/>\nimport org.springframework.web.filter.HiddenHttpMethodFilter;<br \/>\nimport org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;<\/p>\n<p>&#064;Configuration<br \/>\npublic class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {<br \/>\n    \/*<br \/>\n    \u914d\u7f6eSpring\u7684\u914d\u7f6e\u6587\u4ef6<br \/>\n     *\/<br \/>\n    &#064;Override<br \/>\n    protected Class&lt;?&gt;[] getRootConfigClasses() {<br \/>\n        return new Class[0];<br \/>\n    }<\/p>\n<p>    \/*<br \/>\n        SpringMVC\u914d\u7f6e\u6587\u4ef6<br \/>\n     *\/<br \/>\n    &#064;Override<br \/>\n    protected Class&lt;?&gt;[] getServletConfigClasses() {<br \/>\n        return new Class[]{SpringMvcConfig.class};<br \/>\n    }<\/p>\n<p>    \/*<br \/>\n            \u914d\u7f6eDispatcherServlet\u7684\u6620\u5c04\u89c4\u5219&#xff0c;\u5373url-pattern<br \/>\n         *\/<br \/>\n    &#064;Override<br \/>\n    protected String[] getServletMappings() {<br \/>\n        return new String[]{&#034;\/&#034;};<br \/>\n    }<br \/>\n    \/**<br \/>\n     * Spring\u7684\u914d\u7f6e<br \/>\n     * &#064;return<br \/>\n     *\/<\/p>\n<p>    &#064;Override<br \/>\n    protected Filter[] getServletFilters() {<br \/>\n        CharacterEncodingFilter characterEncodingFilter &#061; new CharacterEncodingFilter();<br \/>\n        characterEncodingFilter.setEncoding(&#034;UTF-8&#034;);<br \/>\n        characterEncodingFilter.setForceRequestEncoding(true);<br \/>\n        characterEncodingFilter.setForceResponseEncoding(true);<br \/>\n        \/\/!&#8211;\u9690\u85cf\u7684HTTP\u8bf7\u6c42\u65b9\u5f0f\u8fc7\u6ee4\u5668 \u5b57\u7b26\u7f16\u7801\u8fc7\u6ee4\u5668\u4e4b\u540e&#8211;&gt;<br \/>\n        HiddenHttpMethodFilter hiddenHttpMethodFilter &#061; new HiddenHttpMethodFilter();<\/p>\n<p>        return new Filter[]{characterEncodingFilter, hiddenHttpMethodFilter};<br \/>\n    }<\/p>\n<p>}<\/p>\n<h4>SpringMvcConfig<\/h4>\n<p>package com.gao.ssm.config;<\/p>\n<p>import com.gao.ssm.interceptor.MyInterceptor;<br \/>\nimport org.springframework.context.ApplicationContext;<br \/>\nimport org.springframework.context.annotation.Bean;<br \/>\nimport org.springframework.context.annotation.ComponentScan;<br \/>\nimport org.springframework.context.annotation.Configuration;<br \/>\nimport org.springframework.context.annotation.Import;<br \/>\nimport org.springframework.web.servlet.HandlerExceptionResolver;<br \/>\nimport org.springframework.web.servlet.config.annotation.*;<br \/>\nimport org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;<br \/>\nimport org.thymeleaf.spring6.SpringTemplateEngine;<br \/>\nimport org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;<br \/>\nimport org.thymeleaf.spring6.view.ThymeleafViewResolver;<br \/>\nimport org.thymeleaf.templatemode.TemplateMode;<br \/>\nimport org.thymeleaf.templateresolver.ITemplateResolver;<\/p>\n<p>import java.util.List;<br \/>\nimport java.util.Properties;<\/p>\n<p>&#064;Configuration<br \/>\n&#064;ComponentScan(&#034;com.gao.ssm.handler&#034;)<br \/>\n&#064;Import({SpringConfig.class})<br \/>\n&#064;EnableWebMvc<br \/>\n\/\/\u5f00\u542f\u6ce8\u89e3\u9a71\u52a8<br \/>\npublic class SpringMvcConfig implements WebMvcConfigurer {<br \/>\n    &#064;Override<br \/>\n    public void configureHandlerExceptionResolvers(List&lt;HandlerExceptionResolver&gt; resolvers) {<br \/>\n        SimpleMappingExceptionResolver simpleHandlerExceptionResolver&#061;new SimpleMappingExceptionResolver();<br \/>\n        Properties prop &#061; new Properties();<br \/>\n        prop.setProperty(&#034;java.lang.Exception&#034;, &#034;tip&#034;);<br \/>\n        simpleHandlerExceptionResolver.setExceptionMappings(prop);<br \/>\n        simpleHandlerExceptionResolver.setExceptionAttribute(&#034;e&#034;);<br \/>\n        resolvers.add(simpleHandlerExceptionResolver);<\/p>\n<p>    }<\/p>\n<p>    &#064;Bean<br \/>\n    public ThymeleafViewResolver getViewResolver(SpringTemplateEngine springTemplateEngine) {<br \/>\n        ThymeleafViewResolver resolver &#061; new ThymeleafViewResolver();<br \/>\n        resolver.setTemplateEngine(springTemplateEngine);<br \/>\n        resolver.setCharacterEncoding(&#034;UTF-8&#034;);<br \/>\n        resolver.setOrder(1);<br \/>\n        return resolver;<br \/>\n    }<\/p>\n<p>    &#064;Bean<br \/>\n    public SpringTemplateEngine templateEngine(ITemplateResolver iTemplateResolver) {<br \/>\n        SpringTemplateEngine templateEngine &#061; new SpringTemplateEngine();<br \/>\n        templateEngine.setTemplateResolver(iTemplateResolver);<br \/>\n        return templateEngine;<br \/>\n    }<br \/>\n    public void addInterceptors(InterceptorRegistry registry) {<br \/>\n        MyInterceptor myInterceptor &#061; new MyInterceptor();<br \/>\n        registry.addInterceptor(myInterceptor).addPathPatterns(&#034;\/**&#034;).excludePathPatterns(&#034;\/test&#034;);<br \/>\n    }<br \/>\n    &#064;Override<br \/>\n    public void addViewControllers(ViewControllerRegistry registry) {<br \/>\n        registry.addViewController(&#034;\/index&#034;).setViewName(&#034;index&#034;);<br \/>\n    }<\/p>\n<p>    &#064;Bean<br \/>\n    public ITemplateResolver templateResolver(ApplicationContext applicationContext) {<br \/>\n        SpringResourceTemplateResolver resolver &#061; new SpringResourceTemplateResolver();<br \/>\n        resolver.setApplicationContext(applicationContext);<br \/>\n        resolver.setPrefix(&#034;\/WEB-INF\/thymeleaf\/&#034;);<br \/>\n        resolver.setSuffix(&#034;.html&#034;);<br \/>\n        resolver.setTemplateMode(TemplateMode.HTML);<br \/>\n        resolver.setCharacterEncoding(&#034;UTF-8&#034;);<br \/>\n        resolver.setCacheable(false);\/\/\u5f00\u53d1\u65f6\u5173\u95ed\u7f13\u5b58&#xff0c;\u6539\u52a8\u5373\u53ef\u751f\u6548<br \/>\n        return resolver;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    \/\/\u5f00\u542f\u9759\u6001\u8d44\u6e90\u5904\u7406<br \/>\n    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {<br \/>\n        configurer.enable();<br \/>\n    }<br \/>\n}<\/p>\n<h4>\u6dfb\u52a0\u4e8b\u52a1\u63a7\u5236<\/h4>\n<p>&#064;Configuration<br \/>\n&#064;ComponentScan(&#034;com.gao.ssm.service&#034;)<br \/>\n&#064;EnableTransactionManagement<br \/>\n&#064;Import({MybatisConfig.class})<br \/>\npublic class SpringConfig {<br \/>\n}<\/p>\n<p>&#064;Transactional()<br \/>\n&#064;Service<br \/>\npublic class EmpServiceImpl implements EmpService{ <\/p>\n<h4>pojo\u7c7b\u7f16\u5199<\/h4>\n<p>package com.gao.ssm.bean;<\/p>\n<p>import lombok.AllArgsConstructor;<br \/>\nimport lombok.Data;<br \/>\nimport lombok.NoArgsConstructor;<\/p>\n<p>import java.time.LocalDate;<br \/>\nimport java.time.LocalDateTime;<\/p>\n<p>\/**<br \/>\n * \u5458\u5de5\u5b9e\u4f53\u7c7b<br \/>\n *\/<br \/>\n&#064;Data<br \/>\n&#064;NoArgsConstructor<br \/>\n&#064;AllArgsConstructor<br \/>\npublic class Emp {<br \/>\n    private Integer id; \/\/ID<br \/>\n    private String username; \/\/\u7528\u6237\u540d<br \/>\n    private String password; \/\/\u5bc6\u7801<br \/>\n    private String name; \/\/\u59d3\u540d<br \/>\n    private Short gender; \/\/\u6027\u522b , 1 \u7537, 2 \u5973<br \/>\n    private String image; \/\/\u56fe\u50cfurl<br \/>\n    private Short job; \/\/\u804c\u4f4d , 1 \u73ed\u4e3b\u4efb , 2 \u8bb2\u5e08 , 3 \u5b66\u5de5\u4e3b\u7ba1 , 4 \u6559\u7814\u4e3b\u7ba1 , 5 \u54a8\u8be2\u5e08<br \/>\n    private LocalDate entrydate; \/\/\u5165\u804c\u65e5\u671f<br \/>\n    private Integer deptId; \/\/\u90e8\u95e8ID<br \/>\n    private LocalDateTime createTime; \/\/\u521b\u5efa\u65f6\u95f4<br \/>\n    private LocalDateTime updateTime; \/\/\u4fee\u6539\u65f6\u95f4<br \/>\n}<\/p>\n<h4>dao\u7f16\u5199\u00a0<\/h4>\n<p>package com.gao.ssm.dao;<\/p>\n<p>import com.gao.ssm.bean.Emp;<br \/>\nimport org.apache.ibatis.annotations.Mapper;<\/p>\n<p>import java.util.List;<br \/>\npublic interface EmpDao {<br \/>\n    List&lt;Emp&gt; listAll();<\/p>\n<p>    Emp listOne(Emp emp);<\/p>\n<p>    void add(Emp emp);<br \/>\n}<\/p>\n<h4>service\u7f16\u5199<\/h4>\n<p>package com.gao.ssm.service;<\/p>\n<p>import com.gao.ssm.bean.Emp;<\/p>\n<p>import java.util.List;<\/p>\n<p>public interface EmpService{<br \/>\n    public void register(Emp emp);<\/p>\n<p>    public List&lt;Emp&gt; listAll();<\/p>\n<p>}<br \/>\npackage com.gao.ssm.service;<\/p>\n<p>import com.gao.ssm.bean.Emp;<br \/>\nimport com.gao.ssm.dao.EmpDao;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.stereotype.Service;<br \/>\nimport org.springframework.transaction.annotation.Transactional;<\/p>\n<p>import java.util.List;<\/p>\n<p>&#064;Transactional()<br \/>\n&#064;Service<br \/>\npublic class EmpServiceImpl implements EmpService{<br \/>\n    &#064;Autowired<br \/>\n    private EmpDao empDao;<\/p>\n<p>    &#064;Override<br \/>\n    public void register(Emp emp) {<br \/>\n        empDao.add(emp);<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public List&lt;Emp&gt; listAll() {<br \/>\n        return empDao.listAll();<br \/>\n    }<br \/>\n}<\/p>\n<h4>handler\u7f16\u5199<\/h4>\n<p>package com.gao.ssm.handler;<\/p>\n<p>import com.gao.ssm.bean.Emp;<br \/>\nimport com.gao.ssm.service.EmpService;<br \/>\nimport org.springframework.beans.factory.annotation.Autowired;<br \/>\nimport org.springframework.web.bind.annotation.RequestMapping;<br \/>\nimport org.springframework.web.bind.annotation.RestController;<\/p>\n<p>import java.util.List;<\/p>\n<p>&#064;RestController<br \/>\n&#064;RequestMapping(&#034;\/emp&#034;)<br \/>\npublic class EmpController {<br \/>\n    &#064;Autowired<br \/>\n    private EmpService empService;<br \/>\n    &#064;RequestMapping(&#034;\/list&#034;)<br \/>\n    public String list(){<br \/>\n        return empService.listAll().toString();<br \/>\n    }<br \/>\n}<\/p>\n<h4>EmpMapper.xml<\/h4>\n<p>&lt;?xml version&#061;&#034;1.0&#034; encoding&#061;&#034;UTF-8&#034;?&gt;<br \/>\n&lt;!DOCTYPE mapper PUBLIC &#034;-\/\/MyBatis\/\/DTD Mapper 3.0\/\/EN&#034;<br \/>\n        &#034;http:\/\/mybatis.org\/dtd\/mybatis-3-mapper.dtd&#034;&gt;<\/p>\n<p>&lt;mapper namespace&#061;&#034;com.gao.ssm.dao.EmpDao&#034;&gt;<\/p>\n<p>    &lt;!&#8211; \u67e5\u8be2\u6240\u6709\u5458\u5de5 &#8211;&gt;<br \/>\n    &lt;select id&#061;&#034;listAll&#034; resultType&#061;&#034;com.gao.ssm.bean.Emp&#034;&gt;<br \/>\n        SELECT * FROM emp<br \/>\n    &lt;\/select&gt;<\/p>\n<p>    &lt;!&#8211; \u6839\u636e ID \u67e5\u8be2\u5355\u4e2a\u5458\u5de5 &#8211;&gt;<br \/>\n    &lt;select id&#061;&#034;listOne&#034; resultType&#061;&#034;com.gao.ssm.bean.Emp&#034; parameterType&#061;&#034;java.lang.Integer&#034;&gt;<br \/>\n        SELECT * FROM emp WHERE id &#061; #{id}<br \/>\n    &lt;\/select&gt;<\/p>\n<p>    &lt;!&#8211; \u6dfb\u52a0\u5458\u5de5&#xff0c;\u8fd4\u56de\u4e3b\u952e ID &#8211;&gt;<br \/>\n    &lt;insert id&#061;&#034;add&#034; parameterType&#061;&#034;com.gao.ssm.bean.Emp&#034; useGeneratedKeys&#061;&#034;true&#034; keyProperty&#061;&#034;id&#034;&gt;<br \/>\n        INSERT INTO emp(username, name, gender, image, job, entrydate, dept_id, create_time, update_time)<br \/>\n        VALUES (#{username}, #{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})<br \/>\n    &lt;\/insert&gt;<\/p>\n<p>&lt;\/mapper&gt;<\/p>\n<h4>\u6d4b\u8bd5\u00a0<\/h4>\n<p>&lt;!DOCTYPE html&gt;<br \/>\n&lt;html lang&#061;&#034;en&#034; xmlns:th&#061;&#034;http:\/\/www.thymeleaf.org&#034;&gt;<br \/>\n&lt;head&gt;<br \/>\n    &lt;meta charset&#061;&#034;UTF-8&#034;&gt;<br \/>\n    &lt;title&gt;ssm\u6574\u5408&lt;\/title&gt;<br \/>\n    &lt;!&#8211;\u5f15\u5165vue&#8211;&gt;<br \/>\n    &lt;script th:src&#061;&#034;&#064;{\/static\/js\/vue3.4.21.js}&#034;&gt;&lt;\/script&gt;<br \/>\n    &lt;!&#8211;\u5f15\u5165axios&#8211;&gt;<br \/>\n    &lt;script th:src&#061;&#034;&#064;{\/static\/js\/axios.min.js}&#034;&gt;&lt;\/script&gt;<br \/>\n&lt;\/head&gt;<br \/>\n&lt;body&gt;<br \/>\n&lt;div id&#061;&#034;app&#034;&gt;<br \/>\n    &lt;button &#064;click&#061;&#034;getMessage&#034;&gt;\u67e5\u8be2\u6240\u6709\u7528\u6237&lt;\/button&gt;<br \/>\n    &lt;h1&gt;{{message}}&lt;\/h1&gt;<br \/>\n&lt;\/div&gt;<br \/>\n&lt;script th:inline&#061;&#034;javascript&#034;&gt;<br \/>\n    Vue.createApp({<br \/>\n        data(){<br \/>\n            return {<br \/>\n                message : &#039;\u5f88\u591a\u4fe1\u606f&#039;<br \/>\n            }<br \/>\n        },<br \/>\n        methods : {<br \/>\n            async getMessage(){<br \/>\n                console.log(&#034;&#8212;&#8212;&#8212;&#8212;\u67e5\u8be2&#034;)<br \/>\n                let response &#061; await axios.get(&#034;\/ssm\/emp\/list&#034;)<br \/>\n                console.log(response)<br \/>\n                this.message &#061; response.data<br \/>\n            }<br \/>\n            }<br \/>\n        }).mount(&#034;#app&#034;)<br \/>\n&lt;\/script&gt;<br \/>\n&lt;\/body&gt;<br \/>\n&lt;\/html&gt; <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb820\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf7\u6b21\u3002Spring\u5bb9\u5668\uff1aApplicationContextSpringWeb\u5bb9\u5668\uff1aWebApplicationContext\u3002<\/p>\n","protected":false},"author":2,"featured_media":34339,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,101,190],"topic":[],"class_list":["post-34347","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-java","tag-spring","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/34347.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb820\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf7\u6b21\u3002Spring\u5bb9\u5668\uff1aApplicationContextSpringWeb\u5bb9\u5668\uff1aWebApplicationContext\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/34347.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-29T02:38:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023759-68103b87475f6.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/34347.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/34347.html\",\"name\":\"\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-29T02:38:03+00:00\",\"dateModified\":\"2025-04-29T02:38:03+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/34347.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/34347.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/34347.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/34347.html","og_locale":"zh_CN","og_type":"article","og_title":"\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb820\u6b21\uff0c\u70b9\u8d5e11\u6b21\uff0c\u6536\u85cf7\u6b21\u3002Spring\u5bb9\u5668\uff1aApplicationContextSpringWeb\u5bb9\u5668\uff1aWebApplicationContext\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/34347.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-29T02:38:03+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250429023759-68103b87475f6.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"19 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/34347.html","url":"https:\/\/www.wsisp.com\/helps\/34347.html","name":"\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-29T02:38:03+00:00","dateModified":"2025-04-29T02:38:03+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/34347.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/34347.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/34347.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u624b\u5199SpringMVC(\u57fa\u672c\u6846\u67b6)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/34347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=34347"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/34347\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/34339"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=34347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=34347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=34347"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=34347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}