markdown 来自https://www.zybuluo.com/dume2007/note/744825

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 来自https://www.zybuluo.com/dume2007/note/744825相关的知识,希望对你有一定的参考价值。

<div id="wmd-preview" class="wmd-preview wmd-preview-full-reader" data-medium-element="true" style="left: 0px;"><div class="md-section-divider"></div><div class="md-section-divider"></div><h1 data-anchor-id="1a6z" id="eclipse-springmvc-maven-tomcat配置入门图文教程">Eclipse + SpringMvc + Maven + Tomcat配置入门图文教程</h1><p data-anchor-id="79n4"><code>Eclipse</code> <code>springmvc</code> <code>maven</code> <code>Tomcat</code></p><hr><div class="md-section-divider"></div><h2 data-anchor-id="9n2q" id="安装jdk">安装JDK</h2><p data-anchor-id="6o1w">下载地址:<a href="http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html" target="_blank">http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html</a> <br>
我的版本<code>1.8.0_131</code></p><div class="md-section-divider"></div><h2 data-anchor-id="zh80" id="安装tomcat-server">安装Tomcat Server</h2><p data-anchor-id="eiqi">下载地址:<a href="http://tomcat.apache.org/download-80.cgi" target="_blank">http://tomcat.apache.org/download-80.cgi</a> <br>
这里我选择的是<code>8.5.14</code>版本,apache-tomcat-8.5.14.tar.gz,下载完之后解压到随意目录,如D:/Tomcat</p><div class="md-section-divider"></div><h2 data-anchor-id="mgcc" id="安装eclipse">安装Eclipse</h2><p data-anchor-id="rk6d">下载地址:<a href="https://www.eclipse.org/downloads/eclipse-packages/" target="_blank">https://www.eclipse.org/downloads/eclipse-packages/</a> <br>
选择 <code>Eclipse IDE for Java EE Developers</code>,主要做web开发用。</p><p data-anchor-id="rzbz">下载完后在安装过程中也选择Jave EE Develops(如果选择第一个,你会发现创建project中没有Dynamic Web Project工程等选项,所以务必要选对版本), 如图:</p><p data-anchor-id="hdk5"><img src="http://lnpan.b0.upaiyun.com/upload/note/eclipseinstall.png" alt="此处输入图片的描述" title=""></p><div class="md-section-divider"></div><h2 data-anchor-id="btx2" id="新建maven-web工程">新建maven web工程</h2><div class="md-section-divider"></div><h3 data-anchor-id="ztns" id="1新建maven-project">1.新建Maven Project</h3><p data-anchor-id="tlox">File -&gt; New -&gt; Maven Project</p><p data-anchor-id="q6bo"><img src="http://lnpan.b0.upaiyun.com/upload/note/new_maven_project.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="b5ca">选择默认的工作空间目录就好</p><p data-anchor-id="wh3d"><img src="http://lnpan.b0.upaiyun.com/upload/note/new_maven_project2.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="kmbj">选择webapp项目骨架</p><p data-anchor-id="com2"><img src="http://lnpan.b0.upaiyun.com/upload/note/new_maven_project3.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="7pbz">设置项目名称及包名</p><p data-anchor-id="rtmj"><img src="http://lnpan.b0.upaiyun.com/upload/note/new_maven_project5.png" alt="此处输入图片的描述" title=""></p><div class="md-section-divider"></div><h3 data-anchor-id="i66h" id="2配置工程项目属性">2.配置工程项目属性</h3><p data-anchor-id="g4c2">新建工程完之后,出现错误提示: <br>
<code>The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path</code> <br>
根据提示是还没配置http服务器,先创建一个Tomcat服务器</p><p data-anchor-id="dwp0"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc4.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="lhoy">点击错误提示选项卡中的<code>Servers</code>, 点击创建新服务链接</p><p data-anchor-id="zy4x"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc5.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="olxt">搜索Tomcat,选择<code>Tomcat 8.5 Server</code>,Next下一步</p><p data-anchor-id="lbci"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc6.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="u3lg">选择Tomcat解压目录,如上面提到的D:/Tomcat。下面是我本机Tomcat的目录,JRE选择自己安装的。一般先安装Java,后安装eclipse,这里会列出已安装的Java版本。如果没有可选择的JRE,则点击<code>installed JREs...</code>手动添加Java安装目录。</p><p data-anchor-id="9it9"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc7.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="nrd5">选择自己创建的项目mvc,finish完成Server创建</p><p data-anchor-id="bc7h"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc8.png" alt="此处输入图片的描述"></p><p data-anchor-id="2993">接下来配置<code>Java build path</code>,右键点击项目 -&gt; Build Path -&gt; Configuire Build Path...</p><p data-anchor-id="utql"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc2.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="fzox">在Java build path窗口,选择<code>Libraries</code>选项卡,添加刚才创建的Tomcat Server,点击<code>Add Library</code> -&gt; <code>Server Runtime</code> -&gt; 选择<code>Apache Tomcat v8.5</code>, finish完成。</p><p data-anchor-id="36jj"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc10.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="7ic2"><code>JRE System Library</code>默认不是我们自己安装的JDK,需要编辑修改,点击右边<code>Edit</code>,选择第三个<code>Workspace default JRE</code>,Finish完成退出</p><p data-anchor-id="qgzd"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc9.png" alt="此处输入图片的描述" title=""></p><p data-anchor-id="cb7l">最后点击<code>Apply</code> -&gt; <code>OK</code>,完成配置更改。</p><p data-anchor-id="hhmv">这时候再看项目目录,错误红叉消失,<code>Java Resources</code>目录自动增加了<code>src/main/java</code>,<code>src/test/java</code>包的默认资源目录。运行<code>Run</code>,熟悉的Hello World。Maven Project配置第一步成功!</p><p data-anchor-id="urvm"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc12.png" alt="此处输入图片的描述"></p><div class="md-section-divider"></div><h3 data-anchor-id="rb0t" id="3配置spring框架依赖">3.配置spring框架依赖</h3><div class="md-section-divider"></div><h4 data-anchor-id="magu" id="31-配置pomxml">3.1 配置pom.xml</h4><p data-anchor-id="fuvk">maven通过pom.xml管理着各种包的依赖,相当于php的<code>composer.json</code> <br>
配置spring的pom.xml如下:</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="s71v" data-initialized="true" data-gclp-id="0"><ol class="linenums"><li class="L0"><code><span class="tag">&lt;project</span><span class="pln"> </span><span class="atn">xmlns</span><span class="pun">=</span><span class="atv">"http://maven.apache.org/POM/4.0.0"</span><span class="pln"> </span><span class="atn">xmlns:xsi</span><span class="pun">=</span><span class="atv">"http://www.w3.org/2001/XMLSchema-instance"</span></code></li><li class="L1"><code><span class="pln">  </span><span class="atn">xsi:schemaLocation</span><span class="pun">=</span><span class="atv">"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"</span><span class="tag">&gt;</span></code></li><li class="L2"><code><span class="pln">  </span><span class="tag">&lt;modelVersion&gt;</span><span class="pln">4.0.0</span><span class="tag">&lt;/modelVersion&gt;</span></code></li><li class="L3"><code><span class="pln">  </span><span class="tag">&lt;groupId&gt;</span><span class="pln">cn.mark</span><span class="tag">&lt;/groupId&gt;</span></code></li><li class="L4"><code><span class="pln">  </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">mvc</span><span class="tag">&lt;/artifactId&gt;</span></code></li><li class="L5"><code><span class="pln">  </span><span class="tag">&lt;packaging&gt;</span><span class="pln">war</span><span class="tag">&lt;/packaging&gt;</span></code></li><li class="L6"><code><span class="pln">  </span><span class="tag">&lt;version&gt;</span><span class="pln">0.0.1-SNAPSHOT</span><span class="tag">&lt;/version&gt;</span></code></li><li class="L7"><code><span class="pln">  </span><span class="tag">&lt;name&gt;</span><span class="pln">mvc Maven Webapp</span><span class="tag">&lt;/name&gt;</span></code></li><li class="L8"><code><span class="pln">  </span><span class="tag">&lt;url&gt;</span><span class="pln">http://maven.apache.org</span><span class="tag">&lt;/url&gt;</span></code></li><li class="L9"><code><span class="pln">  </span><span class="tag">&lt;dependencies&gt;</span></code></li><li class="L0"><code><span class="pln">    </span><span class="tag">&lt;dependency&gt;</span></code></li><li class="L1"><code><span class="pln">      </span><span class="tag">&lt;groupId&gt;</span><span class="pln">junit</span><span class="tag">&lt;/groupId&gt;</span></code></li><li class="L2"><code><span class="pln">      </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">junit</span><span class="tag">&lt;/artifactId&gt;</span></code></li><li class="L3"><code><span class="pln">      </span><span class="tag">&lt;version&gt;</span><span class="pln">3.8.1</span><span class="tag">&lt;/version&gt;</span></code></li><li class="L4"><code><span class="pln">      </span><span class="tag">&lt;scope&gt;</span><span class="pln">test</span><span class="tag">&lt;/scope&gt;</span></code></li><li class="L5"><code><span class="pln">    </span><span class="tag">&lt;/dependency&gt;</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">    </span><span class="tag">&lt;dependency&gt;</span></code></li><li class="L8"><code><span class="pln">         </span><span class="tag">&lt;groupId&gt;</span><span class="pln">org.springframework</span><span class="tag">&lt;/groupId&gt;</span></code></li><li class="L9"><code><span class="pln">         </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">spring-web</span><span class="tag">&lt;/artifactId&gt;</span></code></li><li class="L0"><code><span class="pln">         </span><span class="tag">&lt;version&gt;</span><span class="pln">3.0.5.RELEASE</span><span class="tag">&lt;/version&gt;</span></code></li><li class="L1"><code><span class="pln">     </span><span class="tag">&lt;/dependency&gt;</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pln">     </span><span class="tag">&lt;dependency&gt;</span></code></li><li class="L4"><code><span class="pln">         </span><span class="tag">&lt;groupId&gt;</span><span class="pln">org.springframework</span><span class="tag">&lt;/groupId&gt;</span></code></li><li class="L5"><code><span class="pln">         </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">spring-webmvc</span><span class="tag">&lt;/artifactId&gt;</span></code></li><li class="L6"><code><span class="pln">         </span><span class="tag">&lt;version&gt;</span><span class="pln">3.0.5.RELEASE</span><span class="tag">&lt;/version&gt;</span></code></li><li class="L7"><code><span class="pln">     </span><span class="tag">&lt;/dependency&gt;</span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pln">     </span><span class="tag">&lt;dependency&gt;</span></code></li><li class="L0"><code><span class="pln">         </span><span class="tag">&lt;groupId&gt;</span><span class="pln">org.apache.geronimo.specs</span><span class="tag">&lt;/groupId&gt;</span></code></li><li class="L1"><code><span class="pln">         </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">geronimo-servlet_2.5_spec</span><span class="tag">&lt;/artifactId&gt;</span></code></li><li class="L2"><code><span class="pln">         </span><span class="tag">&lt;version&gt;</span><span class="pln">1.2</span><span class="tag">&lt;/version&gt;</span></code></li><li class="L3"><code><span class="pln">     </span><span class="tag">&lt;/dependency&gt;</span></code></li><li class="L4"><code></code></li><li class="L5"><code><span class="pln">     </span><span class="tag">&lt;dependency&gt;</span></code></li><li class="L6"><code><span class="pln">       </span><span class="tag">&lt;groupId&gt;</span><span class="pln">javax.servlet</span><span class="tag">&lt;/groupId&gt;</span></code></li><li class="L7"><code><span class="pln">       </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">jstl</span><span class="tag">&lt;/artifactId&gt;</span></code></li><li class="L8"><code><span class="pln">       </span><span class="tag">&lt;version&gt;</span><span class="pln">1.2</span><span class="tag">&lt;/version&gt;</span></code></li><li class="L9"><code><span class="pln">      </span><span class="tag">&lt;/dependency&gt;</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">      </span><span class="tag">&lt;dependency&gt;</span></code></li><li class="L2"><code><span class="pln">       </span><span class="tag">&lt;groupId&gt;</span><span class="pln">taglibs</span><span class="tag">&lt;/groupId&gt;</span></code></li><li class="L3"><code><span class="pln">       </span><span class="tag">&lt;artifactId&gt;</span><span class="pln">standard</span><span class="tag">&lt;/artifactId&gt;</span></code></li><li class="L4"><code><span class="pln">       </span><span class="tag">&lt;version&gt;</span><span class="pln">1.1.2</span><span class="tag">&lt;/version&gt;</span></code></li><li class="L5"><code><span class="pln">      </span><span class="tag">&lt;/dependency&gt;</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">  </span><span class="tag">&lt;/dependencies&gt;</span></code></li><li class="L8"><code><span class="pln">  </span><span class="tag">&lt;build&gt;</span></code></li><li class="L9"><code><span class="pln">    </span><span class="tag">&lt;finalName&gt;</span><span class="pln">mvc</span><span class="tag">&lt;/finalName&gt;</span></code></li><li class="L0"><code><span class="pln">  </span><span class="tag">&lt;/build&gt;</span></code></li><li class="L1"><code><span class="tag">&lt;/project&gt;</span></code></li></ol></pre><div class="md-section-divider"></div><h4 data-anchor-id="ki0t" id="32-配置webxml">3.2 配置web.xml</h4><p data-anchor-id="r62i">这里主要指定Tomcat的URL拦截规则,指定使用springmvc的DispatcherServlet。同时装入spring配置文件<code>/WEB-INF/mvc-servlet.xml</code>。</p><p data-anchor-id="mlhc"><code>DispatcherServlet</code>是前置控制器,拦截匹配的请求,Servlet拦截匹配规则要自己定义,把拦截下来的请求,依据相应的规则分发到目标Controller来处理。</p><blockquote data-anchor-id="4l6c" class="white-blockquote">
  <p>位置:/src/main/webapp/WEB-INF/web.xml</p>
</blockquote><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="757u" data-initialized="true" data-gclp-id="1"><ol class="linenums"><li class="L0"><code><span class="pun">&lt;?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"UTF-8"</span><span class="pun">?&gt;</span></code></li><li class="L1"><code><span class="tag">&lt;web-app</span><span class="pln"> </span><span class="atn">xmlns</span><span class="pun">=</span><span class="atv">"http://java.sun.com/xml/ns/javaee"</span><span class="pln"> </span></code></li><li class="L2"><code><span class="pln">         </span><span class="atn">xmlns:xsi</span><span class="pun">=</span><span class="atv">"http://www.w3.org/2001/XMLSchema-instance"</span></code></li><li class="L3"><code><span class="pln">         </span><span class="atn">xsi:schemaLocation</span><span class="pun">=</span><span class="atv">"http://java.sun.com/xml/ns/javaee </span></code></li><li class="L4"><code><span class="atv">                             http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"</span><span class="pln"> </span></code></li><li class="L5"><code><span class="pln">         </span><span class="atn">version</span><span class="pun">=</span><span class="atv">"2.5"</span><span class="pln"> </span><span class="tag">&gt;</span></code></li><li class="L6"><code></code></li><li class="L7"><code><span class="pln">    </span><span class="com">&lt;!-- 区分项目名称,防止默认重名 --&gt;</span></code></li><li class="L8"><code><span class="pln">    </span><span class="tag">&lt;context-param&gt;</span></code></li><li class="L9"><code><span class="pln">        </span><span class="tag">&lt;param-name&gt;</span><span class="pln">webAppRootKey</span><span class="tag">&lt;/param-name&gt;</span></code></li><li class="L0"><code><span class="pln">        </span><span class="tag">&lt;param-value&gt;</span><span class="pln">mvc</span><span class="tag">&lt;/param-value&gt;</span></code></li><li class="L1"><code><span class="pln">    </span><span class="tag">&lt;/context-param&gt;</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pln">    </span><span class="com">&lt;!-- Spring的log4j监听器 --&gt;</span></code></li><li class="L4"><code><span class="pln">    </span><span class="tag">&lt;listener&gt;</span></code></li><li class="L5"><code><span class="pln">        </span><span class="tag">&lt;listener-class&gt;</span><span class="pln">org.springframework.web.util.Log4jConfigListener</span><span class="tag">&lt;/listener-class&gt;</span></code></li><li class="L6"><code><span class="pln">    </span><span class="tag">&lt;/listener&gt;</span></code></li><li class="L7"><code></code></li><li class="L8"><code><span class="pln">    </span><span class="com">&lt;!-- 默认配置文件/WEB-INF/applicationContext.xml --&gt;</span></code></li><li class="L9"><code><span class="pln">    </span><span class="tag">&lt;listener&gt;</span></code></li><li class="L0"><code><span class="pln">        </span><span class="tag">&lt;listener-class&gt;</span><span class="pln">org.springframework.web.context.ContextLoaderListener</span><span class="tag">&lt;/listener-class&gt;</span></code></li><li class="L1"><code><span class="pln">    </span><span class="tag">&lt;/listener&gt;</span></code></li><li class="L2"><code></code></li><li class="L3"><code><span class="pln">    </span><span class="com">&lt;!-- 字符集 过滤器  --&gt;</span></code></li><li class="L4"><code><span class="pln">    </span><span class="tag">&lt;filter&gt;</span></code></li><li class="L5"><code><span class="pln">        </span><span class="tag">&lt;filter-name&gt;</span><span class="pln">CharacterEncodingFilter</span><span class="tag">&lt;/filter-name&gt;</span></code></li><li class="L6"><code><span class="pln">        </span><span class="tag">&lt;filter-class&gt;</span><span class="pln">org.springframework.web.filter.CharacterEncodingFilter</span><span class="tag">&lt;/filter-class&gt;</span></code></li><li class="L7"><code><span class="pln">        </span><span class="tag">&lt;init-param&gt;</span></code></li><li class="L8"><code><span class="pln">            </span><span class="tag">&lt;param-name&gt;</span><span class="pln">encoding</span><span class="tag">&lt;/param-name&gt;</span></code></li><li class="L9"><code><span class="pln">            </span><span class="tag">&lt;param-value&gt;</span><span class="pln">UTF-8</span><span class="tag">&lt;/param-value&gt;</span></code></li><li class="L0"><code><span class="pln">        </span><span class="tag">&lt;/init-param&gt;</span></code></li><li class="L1"><code><span class="pln">        </span><span class="tag">&lt;init-param&gt;</span></code></li><li class="L2"><code><span class="pln">            </span><span class="tag">&lt;param-name&gt;</span><span class="pln">forceEncoding</span><span class="tag">&lt;/param-name&gt;</span></code></li><li class="L3"><code><span class="pln">            </span><span class="tag">&lt;param-value&gt;</span><span class="pln">true</span><span class="tag">&lt;/param-value&gt;</span></code></li><li class="L4"><code><span class="pln">        </span><span class="tag">&lt;/init-param&gt;</span></code></li><li class="L5"><code><span class="pln">    </span><span class="tag">&lt;/filter&gt;</span></code></li><li class="L6"><code><span class="pln">    </span><span class="tag">&lt;filter-mapping&gt;</span></code></li><li class="L7"><code><span class="pln">        </span><span class="tag">&lt;filter-name&gt;</span><span class="pln">CharacterEncodingFilter</span><span class="tag">&lt;/filter-name&gt;</span></code></li><li class="L8"><code><span class="pln">        </span><span class="tag">&lt;url-pattern&gt;</span><span class="pln">/*</span><span class="tag">&lt;/url-pattern&gt;</span></code></li><li class="L9"><code><span class="pln">    </span><span class="tag">&lt;/filter-mapping&gt;</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">    </span><span class="com">&lt;!-- Spring view分发器 --&gt;</span></code></li><li class="L2"><code><span class="pln">    </span><span class="tag">&lt;servlet&gt;</span></code></li><li class="L3"><code><span class="pln">        </span><span class="tag">&lt;servlet-name&gt;</span><span class="pln">mvc</span><span class="tag">&lt;/servlet-name&gt;</span></code></li><li class="L4"><code><span class="pln">        </span><span class="tag">&lt;servlet-class&gt;</span><span class="pln">org.springframework.web.servlet.DispatcherServlet</span><span class="tag">&lt;/servlet-class&gt;</span></code></li><li class="L5"><code><span class="pln">        </span><span class="tag">&lt;init-param&gt;</span></code></li><li class="L6"><code><span class="pln">            </span><span class="tag">&lt;param-name&gt;</span><span class="pln">contextConfigLocation</span><span class="tag">&lt;/param-name&gt;</span></code></li><li class="L7"><code><span class="pln">            </span><span class="tag">&lt;param-value&gt;</span><span class="pln">/WEB-INF/mvc-servlet.xml</span><span class="tag">&lt;/param-value&gt;</span></code></li><li class="L8"><code><span class="pln">        </span><span class="tag">&lt;/init-param&gt;</span></code></li><li class="L9"><code><span class="pln">        </span><span class="tag">&lt;load-on-startup&gt;</span><span class="pln">1</span><span class="tag">&lt;/load-on-startup&gt;</span></code></li><li class="L0"><code><span class="pln">    </span><span class="tag">&lt;/servlet&gt;</span></code></li><li class="L1"><code><span class="pln">    </span><span class="tag">&lt;servlet-mapping&gt;</span></code></li><li class="L2"><code><span class="pln">        </span><span class="tag">&lt;servlet-name&gt;</span><span class="pln">mvc</span><span class="tag">&lt;/servlet-name&gt;</span></code></li><li class="L3"><code><span class="pln">        </span><span class="tag">&lt;url-pattern&gt;</span><span class="pln">/</span><span class="tag">&lt;/url-pattern&gt;</span></code></li><li class="L4"><code><span class="pln">    </span><span class="tag">&lt;/servlet-mapping&gt;</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="pln">    </span><span class="tag">&lt;welcome-file-list&gt;</span></code></li><li class="L7"><code><span class="pln">        </span><span class="tag">&lt;welcome-file&gt;</span><span class="pln">index.jsp</span><span class="tag">&lt;/welcome-file&gt;</span></code></li><li class="L8"><code><span class="pln">    </span><span class="tag">&lt;/welcome-file-list&gt;</span></code></li><li class="L9"><code></code></li><li class="L0"><code><span class="tag">&lt;/web-app&gt;</span></code></li></ol></pre><div class="md-section-divider"></div><h4 data-anchor-id="mpxx" id="33-配置mvc-servletxml">3.3 配置mvc-servlet.xml</h4><p data-anchor-id="p2zf">这是springmvc的配置文件。</p><ul data-anchor-id="1utt">
<li><code>&lt;context:component-scan&gt;</code>主要指定了要遍历的package包名为<code>cn.mark.mvc.controller</code>,这个在新建controller文件时要对应这个package名称。</li>
<li><code>InternalResourceViewResolver</code>是视图解析器,以下指定视图文件目录为<code>/WEB-INF/views</code>,后缀名<code>.jsp</code></li>
<li><code>&lt;mvc:annotation-driven /&gt;</code>是注解配置,这个允许你在controller类里加上如<code>@controller</code>、<code>@RequestMapping</code>注解来告诉springmvc这个类可以接收用户请求并指定URL地址映射。</li>
</ul><blockquote data-anchor-id="fsxm" class="white-blockquote">
  <p>位置:/src/main/webapp/WEB-INF/mvc-servlet.xml</p>
</blockquote><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="wevj" data-initialized="true" data-gclp-id="2"><ol class="linenums"><li class="L0"><code><span class="pun">&lt;?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"UTF-8"</span><span class="pun">?&gt;</span></code></li><li class="L1"><code><span class="tag">&lt;beans</span><span class="pln"> </span><span class="atn">xmlns</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/beans"</span><span class="pln"> </span></code></li><li class="L2"><code><span class="pln">       </span><span class="atn">xmlns:aop</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/aop"</span><span class="pln"> </span></code></li><li class="L3"><code><span class="pln">       </span><span class="atn">xmlns:context</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/context"</span></code></li><li class="L4"><code><span class="pln">       </span><span class="atn">xmlns:mvc</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/mvc"</span><span class="pln"> </span></code></li><li class="L5"><code><span class="pln">       </span><span class="atn">xmlns:tx</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/tx"</span><span class="pln"> </span></code></li><li class="L6"><code><span class="pln">       </span><span class="atn">xmlns:xsi</span><span class="pun">=</span><span class="atv">"http://www.w3.org/2001/XMLSchema-instance"</span></code></li><li class="L7"><code><span class="pln">       </span><span class="atn">xsi:schemaLocation</span><span class="pun">=</span><span class="atv">"http://www.springframework.org/schema/aop </span></code></li><li class="L8"><code><span class="atv">        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd </span></code></li><li class="L9"><code><span class="atv">        http://www.springframework.org/schema/beans </span></code></li><li class="L0"><code><span class="atv">        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd </span></code></li><li class="L1"><code><span class="atv">        http://www.springframework.org/schema/context </span></code></li><li class="L2"><code><span class="atv">        http://www.springframework.org/schema/context/spring-context-3.0.xsd </span></code></li><li class="L3"><code><span class="atv">        http://www.springframework.org/schema/mvc </span></code></li><li class="L4"><code><span class="atv">        http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd </span></code></li><li class="L5"><code><span class="atv">        http://www.springframework.org/schema/tx </span></code></li><li class="L6"><code><span class="atv">        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"</span><span class="tag">&gt;</span></code></li><li class="L7"><code></code></li><li class="L8"><code><span class="pln">    </span><span class="tag">&lt;mvc:annotation-driven</span><span class="pln"> </span><span class="tag">/&gt;</span></code></li><li class="L9"><code><span class="pln">    </span><span class="tag">&lt;context:component-scan</span><span class="pln"> </span><span class="atn">base-package</span><span class="pun">=</span><span class="atv">"cn.mark.mvc.controller"</span><span class="pln"> </span><span class="tag">/&gt;</span></code></li><li class="L0"><code></code></li><li class="L1"><code><span class="pln">    </span><span class="tag">&lt;bean</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"org.springframework.web.servlet.view.InternalResourceViewResolver"</span><span class="tag">&gt;</span></code></li><li class="L2"><code><span class="pln">        </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"prefix"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"/WEB-INF/views/"</span><span class="pln"> </span><span class="tag">/&gt;</span></code></li><li class="L3"><code><span class="pln">        </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"suffix"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">".jsp"</span><span class="pln"> </span><span class="tag">/&gt;</span></code></li><li class="L4"><code><span class="pln">    </span><span class="tag">&lt;/bean&gt;</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="tag">&lt;/beans&gt;</span></code></li></ol></pre><div class="md-section-divider"></div><h4 data-anchor-id="8plf" id="34-配置applicationcontextxml">3.4 配置applicationContext.xml</h4><p data-anchor-id="bchw">在启动Web容器时,自动装配Spring applicationContext.xml的配置信息,这里可以初始化一些bean,如Hibernate、DataSource等。这里我们先什么也不做。</p><blockquote data-anchor-id="lr35" class="white-blockquote">
  <p>位置:/src/main/webapp/WEB-INF/applicationContext.xml</p>
</blockquote><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="2zic" data-initialized="true" data-gclp-id="3"><ol class="linenums"><li class="L0"><code><span class="pun">&lt;?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"UTF-8"</span><span class="pun">?&gt;</span></code></li><li class="L1"><code><span class="dec">&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;</span></code></li><li class="L2"><code><span class="tag">&lt;beans&gt;</span></code></li><li class="L3"><code><span class="tag">&lt;/beans&gt;</span></code></li></ol></pre><div class="md-section-divider"></div><h4 data-anchor-id="gp7b" id="35-编写第一个控制器类">3.5 编写第一个控制器类</h4><div class="md-section-divider"></div><h5 data-anchor-id="mb0h" id="351-创建controller包名cnmarkmvccontroller">3.5.1 创建controller包名<code>cn.mark.mvc.controller</code></h5><p data-anchor-id="j3rq"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc003.png" alt="此处输入图片的描述"></p><p data-anchor-id="26wv">输入<code>cn.mark.mvc.controller</code>,这个是与<code>mvc-servlet.xml</code>配置中要扫描的package对应的。</p><p data-anchor-id="579m"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc004.png" alt="此处输入图片的描述"></p><div class="md-section-divider"></div><h5 data-anchor-id="oaz1" id="352-创建testcontrollerjava类">3.5.2 创建TestController.java类</h5><p data-anchor-id="6d35"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc005.png" alt="此处输入图片的描述"></p><p data-anchor-id="wc1k">输入控制器名称<code>TestController</code></p><p data-anchor-id="d7za"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc006.png" alt="此处输入图片的描述"></p><p data-anchor-id="miqq">创建完之后,复制以下示例代码到TestController.java</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="udt3" data-initialized="true" data-gclp-id="4"><ol class="linenums"><li class="L0"><code><span class="kwd">package</span><span class="pln"> cn</span><span class="pun">.</span><span class="pln">mark</span><span class="pun">.</span><span class="pln">mvc</span><span class="pun">.</span><span class="pln">controller</span><span class="pun">;</span></code></li><li class="L1"><code></code></li><li class="L2"><code><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">stereotype</span><span class="pun">.</span><span class="typ">Controller</span><span class="pun">;</span></code></li><li class="L3"><code><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">ui</span><span class="pun">.</span><span class="typ">Model</span><span class="pun">;</span></code></li><li class="L4"><code><span class="kwd">import</span><span class="pln"> org</span><span class="pun">.</span><span class="pln">springframework</span><span class="pun">.</span><span class="pln">web</span><span class="pun">.</span><span class="pln">bind</span><span class="pun">.</span><span class="pln">annotation</span><span class="pun">.</span><span class="typ">RequestMapping</span><span class="pun">;</span></code></li><li class="L5"><code></code></li><li class="L6"><code><span class="lit">@Controller</span><span class="pln">  </span></code></li><li class="L7"><code><span class="kwd">public</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">TestController</span><span class="pln"> </span><span class="pun">{</span><span class="pln">  </span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pln">    </span><span class="lit">@RequestMapping</span><span class="pun">(</span><span class="pln">value</span><span class="pun">=</span><span class="str">"test"</span><span class="pun">)</span><span class="pln">  </span></code></li><li class="L0"><code><span class="pln">    </span><span class="kwd">public</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> test</span><span class="pun">(</span><span class="typ">Model</span><span class="pln"> model</span><span class="pun">){</span></code></li><li class="L1"><code><span class="pln">        model</span><span class="pun">.</span><span class="pln">addAttribute</span><span class="pun">(</span><span class="str">"ddc"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"邓莫尔, 你好! 我是Spring!"</span><span class="pun">);</span></code></li><li class="L2"><code><span class="pln">        </span><span class="com">//System.out.println("test.jsp");</span></code></li><li class="L3"><code><span class="pln">    </span><span class="pun">}</span><span class="pln">  </span></code></li><li class="L4"><code><span class="pun">}</span></code></li></ol></pre><div class="md-section-divider"></div><h5 data-anchor-id="c8li" id="353-创建testjsp视图文件">3.5.3 创建test.jsp视图文件</h5><p data-anchor-id="2dbb">在/WEB-INF目录下新建文件夹views,新建/WEB-INF/views/test.jsp文件,文件名对应控制器上的RequestMapping的value值,复制示例代码:</p><div class="md-section-divider"></div><pre class="prettyprint linenums prettyprinted" data-anchor-id="9vjj" data-initialized="true" data-gclp-id="5"><ol class="linenums"><li class="L0"><code><span class="pun">&lt;%</span><span class="lit">@page</span><span class="pln"> language</span><span class="pun">=</span><span class="str">"java"</span><span class="pln"> contentType</span><span class="pun">=</span><span class="str">"text/html; charset=UTF-8"</span><span class="pln"> pageEncoding</span><span class="pun">=</span><span class="str">"UTF-8"</span><span class="pln">%&gt;</span></code></li><li class="L1"><code><span class="pun">&lt;%</span><span class="lit">@taglib</span><span class="pln"> prefix</span><span class="pun">=</span><span class="str">"c"</span><span class="pln"> uri</span><span class="pun">=</span><span class="str">"http://java.sun.com/jsp/jstl/core"</span><span class="pln">%&gt;</span></code></li><li class="L2"><code><span class="dec">&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;</span></code></li><li class="L3"><code><span class="tag">&lt;html&gt;</span></code></li><li class="L4"><code><span class="pln">    </span><span class="tag">&lt;head&gt;</span></code></li><li class="L5"><code><span class="pln">        </span><span class="tag">&lt;meta</span><span class="pln"> </span><span class="atn">http-equiv</span><span class="pun">=</span><span class="atv">"Content-Type"</span><span class="pln"> </span><span class="atn">content</span><span class="pun">=</span><span class="atv">"text/html; charset=UTF-8"</span><span class="tag">&gt;</span></code></li><li class="L6"><code><span class="pln">        </span><span class="tag">&lt;title&gt;</span><span class="pln">SpringMVC测试页面</span><span class="tag">&lt;/title&gt;</span></code></li><li class="L7"><code><span class="pln">    </span><span class="tag">&lt;/head&gt;</span></code></li><li class="L8"><code></code></li><li class="L9"><code><span class="pln">    </span><span class="tag">&lt;body&gt;</span></code></li><li class="L0"><code><span class="pln">        </span><span class="tag">&lt;c:out</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"${ddc}"</span><span class="tag">&gt;&lt;/c:out&gt;</span></code></li><li class="L1"><code><span class="pln">    </span><span class="tag">&lt;/body&gt;</span></code></li><li class="L2"><code><span class="tag">&lt;/html&gt;</span></code></li></ol></pre><div class="md-section-divider"></div><h5 data-anchor-id="x7oo" id="354-测试运行结果">3.5.4 测试运行结果</h5><p data-anchor-id="ihv9">运行<code>Run</code>,访问URL:<a href="http://localhost:8080/mvc/test" target="_blank">http://localhost:8080/mvc/test</a></p><p data-anchor-id="x0gh"><img src="http://lnpan.b0.upaiyun.com/upload/note/mvc007.png" alt="此处输入图片的描述"></p><p data-anchor-id="g0qd">后续会介绍如何配置数据库、Hibernate以及web前端框架,如GWT、SmartGWT、ExtGWT、Vaadin等,正在学习中,待我学成归来,与大家一起分享~</p></div>

以上是关于markdown 来自https://www.zybuluo.com/dume2007/note/744825的主要内容,如果未能解决你的问题,请参考以下文章

算法与数据结构面试题 网盘下载

markdown 来自卑微起点的Yoneda直觉

markdown 区块链入门相关资源推荐来自知

markdown [shopify] - 来自Slate的主题

markdown 来自cacher的新snnipet

css 用于pandoc的CSS,来自markdown语法。