<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                本文演示如何使用Spring MVC做出最簡單的Hello World應用。 ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#示例1)示例1 項目創建和之前一樣,不過在最后一步要選擇Spring Web MVC: [![](https://box.kancloud.cn/2015-10-17_5621d5e45f242.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-02/01.png) 項目結構如下: [![](https://box.kancloud.cn/2015-10-17_5621d5e473952.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-02/02.png) ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#webxml源碼)web.xml源碼: ~~~ <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> <welcome-file-list> <welcome-file>redirect.jsp</welcome-file> </welcome-file-list> </web-app> ~~~ 如果遇到匹配*.htm的URL,會使用`org.springframework.web.servlet.DispatcherServlet`來處理。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#applicationcontextxml源碼)applicationContext.xml源碼: ~~~ <?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <!--bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" p:location="/WEB-INF/jdbc.properties" /> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" /--> <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) --> </beans> ~~~ applicationContext.xml是Spring的配置文件。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#dispatcher-servletxml源碼)dispatcher-servlet.xml源碼: ~~~ <?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/> <!-- Most controllers will use the ControllerClassNameHandlerMapping above, but for the index controller we are using ParameterizableViewController, so we must define an explicit mapping for it. --> <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> </props> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> <!-- The index controller. --> <bean name="indexController" class="org.springframework.web.servlet.mvc.ParameterizableViewController" p:viewName="index" /> </beans> ~~~ 在`<bean id="viewResolver" .../>`定義了JSP模板文件的位置和后綴(這樣其他地方就可以省略后綴了)。 URL為`index.htm`時,對應的控制器是`indexController`,其調用了`/WEB-INF/jsp/`下的模板`index.jsp`。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#redirectjsp源碼)redirect.jsp源碼 ~~~ <%@page contentType="text/html" pageEncoding="UTF-8"%> <% response.sendRedirect("index.htm"); %> ~~~ ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#indexjsp源碼)index.jsp源碼 ~~~ <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Welcome to Spring Web MVC project</title> </head> <body> <p>Hello! This is the default welcome page for a Spring Web MVC project.</p> <p><i>To display a different welcome page for this project, modify</i> <tt>index.jsp</tt> <i>, or create your own welcome page then change the redirection in</i> <tt>redirect.jsp</tt> <i>to point to the new welcome page and also update the welcome-file setting in</i> <tt>web.xml</tt>.</p> </body> </html> ~~~ 運行項目,打開瀏覽器訪問`http://localhost:8084/Project_0102/`,會自動跳轉到`http://localhost:8084/Project_0102/index.htm`,并顯示`index.jsp`的內容。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#hello-world)Hello World 修改dispatcher-servlet.xml,將`<bean id="urlMapping" .../>`修改為: ~~~ <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> <prop key="hello.htm">helloController</prop> </props> </property> </bean> ~~~ 并添加: ~~~ <bean name="helloController" class="me.letiantian.controller.HelloController" /> ~~~ HelloController.java的源碼如下: ~~~ package me.letiantian.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject("message", "Hello World!你好"); mv.setViewName("hello"); return mv; } } ~~~ 模板`hello.jsp`的源碼如下: ~~~ <html> <head> <title>Hello world</title> </head> <body> <h1>${message}</h1> </body> </html> ~~~ 創建static目錄,在static目錄下創建test.js,內容如下: ~~~ console.log("hello world"); ~~~ 在`web.xml`中添加: ~~~ <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> ~~~ 好了,現在的項目結構如下: [![](https://box.kancloud.cn/2015-10-17_5621d5e480c87.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-02/03.png) 瀏覽器訪問結果: [![](https://box.kancloud.cn/2015-10-17_5621d5e4905c9.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-02/04.png) [![](https://box.kancloud.cn/2015-10-17_5621d5e49b8d1.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-02/05.png) 亂碼了~囧~ **解決方法:** 在`HelloController.java`加入`response.setContentType("text/html;charset=UTF-8");`: ~~~ package me.letiantian.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=UTF-8"); // 新加入的內容 ModelAndView mv = new ModelAndView(); mv.addObject("message", "Hello World!你好"); mv.setViewName("hello"); return mv; } } ~~~ ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#示例2)示例2 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#換種方法配置靜態資源)換種方法配置靜態資源 刪掉在`web.xml`中的: ~~~ <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.jpg</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.png</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>default</servlet-name> <url-pattern>*.css</url-pattern> </servlet-mapping> ~~~ 在`dispatcher-servlet.xml`中增加以下內容: ~~~ <?xml version='1.0' encoding='UTF-8' ?> <!-- was: <?xml version="1.0" encoding="UTF-8"?> --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 其他內容 --> <mvc:resources mapping="/static/**" location="/static/"/> </beans> ~~~ 注意,在beans的屬性中增加了`xmlns:mvc="http://www.springframework.org/schema/mvc"`,屬性`xsi:schemaLocation`中增加了`http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd`。 ### [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#不再使用任何后綴例如htmljsp)不再使用任何后綴(例如.html,.jsp) 將`redirect.jsp`修改為: ~~~ <%@page contentType="text/html" pageEncoding="UTF-8"%> <% response.sendRedirect("index"); %> ~~~ 將web.xml中的: ~~~ <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.htm</url-pattern> </servlet-mapping> ~~~ 修改為: ~~~ <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> ~~~ 將`dispatcher-servlet.xml`中的: ~~~ <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index.htm">indexController</prop> <prop key="hello.htm">helloController</prop> </props> </property> </bean> ~~~ 修改為: ~~~ <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="index">indexController</prop> <prop key="hello">helloController</prop> </props> </property> </bean> ~~~ 然后,瀏覽器訪問`http://localhost:8084/Project_0102/hello`。 ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#示例3)示例3 這個示例展示如何獲取URL中的數據。 修改`HelloController.java`: ~~~ package me.letiantian.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; public class HelloController implements Controller{ @Override public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("text/html;charset=UTF-8"); ModelAndView mv = new ModelAndView(); mv.addObject("name", request.getParameter("name")); mv.setViewName("hello"); return mv; } } ~~~ 修改`hello.jsp`: ~~~ <%@page contentType="text/html" pageEncoding="UTF-8"%> <html> <head> <title>Hello world</title> </head> <body> <h1>${pageContext.request.contextPath}</h1> <form action="${pageContext.request.contextPath}/hello" method="GET"> <input name="name" /> <input type="submit" value="提交"/> </form> <h2>提交的數據: ${name}</h2> </body> </html> ~~~ `${pageContext.request.contextPath}`的輸出是`/Project_0102`。 瀏覽器訪問: [![](https://box.kancloud.cn/2015-10-17_5621d5e4a65bd.png)](https://github.com/someus/another-tutorial-about-java-web/blob/master/img/01-02/06.png) 再編輯JSP文件時候遇到了這樣的問題: > The header.jspf contains characters which will probably be damaged during conversion to the ISO-8859-1 character set. Do you want to save the file using this character set? 解決辦法見[http://stackoverflow.com/questions/15499182/netbeans-forces-me-to-save-in-specific-encoding](http://stackoverflow.com/questions/15499182/netbeans-forces-me-to-save-in-specific-encoding)。 ## [](https://github.com/someus/another-tutorial-about-java-web/blob/master/01-02.md#資料)資料 * [Chapter 13\. Web MVC framework](http://docs.spring.io/spring/docs/2.5.x/reference/mvc.html) * [Spring MVC – How to include JS or CSS files in a JSP page](http://www.mkyong.com/spring-mvc/spring-mvc-how-to-include-js-or-css-files-in-a-jsp-page/) * [dispatcher-servlet.xml and application-context.xml的區別](http://stackoverflow.com/questions/4549034/dispatcher-servlet-xml-and-application-context-xml) * [Spring MVC SimpleUrlHandlerMapping example](http://www.mkyong.com/spring-mvc/spring-mvc-simpleurlhandlermapping-example/) * [springMVC中文亂碼問題](http://blog.csdn.net/xuechongyang/article/details/8283924) * [SpringMVC 基于注解的Controller @RequestMapping @RequestParam..](http://blog.csdn.net/lufeng20/article/details/7598801) * urlMapping也可以通過注解來定義,例如[Spring 4 MVC Hello World Tutorial – Full Example](http://javahash.com/spring-4-mvc-hello-world-tutorial-full-example/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看