其實沒有完整的9天,就是連續每天花點時間,過程so frustrated,踩坑無數。。。下麵是學習過程的記錄 第1天 開始正式學習JavaEE,已完成: 1. Tomcat安裝; 2. Tomcat配置。完成了IIS占用的80埠停用,將Tomcat監聽埠改為80埠,然後修改hosts,增加解析 ...
其實沒有完整的9天,就是連續每天花點時間,過程so frustrated,踩坑無數。。。下麵是學習過程的記錄
第1天
開始正式學習JavaEE,已完成:
1. Tomcat安裝;
2. Tomcat配置。完成了IIS占用的80埠停用,將Tomcat監聽埠改為80埠,然後修改hosts,增加解析www.javademo.dev到127.0.0.1。然後寫了一個最簡單hello-world html頁面,在瀏覽器輸入前面的網址可以訪問。Tomcat/conf/server.xml配置如下:
<Host name="www.javademo.dev" appBase="H:\2016\java\TestWebApp" unpackWARs="true" autoDeploy="true" debug="0" xmlValidation="false" xmlNamespaceAware="false" > <Context path="/" docBase="H:\2016\java\TestWebApp"></Context> </Host>
3. MySql安裝,創建資料庫,創建表,設置主鍵、自增列等。
4. java基本語法熟悉,已收藏:幸福框架-java入門
第2天
受不了百度,花了158購買了greenvpn一年套餐,從此Google是最愛
第3天
配置好struts,但是怎麼在瀏覽器中運行都是404錯誤,折騰了好久才發現是地址欄中將“HelloWorld”寫成了“helloworld”。
第4天
如將:/gcz564539969/article/details/7952415.html 配置成:/{username}/article/details/{articleId}.html,其中username和articleId為參數?
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 <struts> 6 <constant name="struts.action.extension" value="" /> 7 <constant name="struts.devMode" value="true" /> 8 <constant name="struts.mapper.action.prefix.enabled" value="true" /> 9 <constant name="struts.enable.SlashesInActionNames" value="true" /> 10 <constant name="struts.mapper.alwaysSelectFullNamespace" 11 value="false" /> 12 <constant name="struts.patternMatcher" value="regex" /> 13 <package name="helloworld" namespace="/" extends="struts-default"> 14 <action name="home" class="web.publics.actions.HomeAction" 15 method="execute"> 16 <result name="index">/home.jsp</result> 17 </action> 18 </package> 19 <package name="shop" namespace="/shop" extends="struts-default"> 20 <action name="/product/{productId}.fuck" class="web.publics.actions.ProductDetailsAction"> 21 <result name="index">/views/shop/product/details.jsp</result> 22 </action> 23 </package> 24 </struts>View Code
折騰了挺久,終於配置成功。訪問URL: http://localhost:8080/HelloWorld/shop/product/58.fuck
繼續折騰tiles3,始終出現下麵的錯誤無法解決:
org.apache.tiles.definition.NoSuchDefinitionException: Cannot find definition named 'art-read'
第5天
繼續折騰tiles,嘗試用maven創建項目,但是maven創建的項目連運行都運行不起來。未果。
第6天
繼續用maven創建項目,希望可以運行簡單的hello world。
然而測試項目建了四五個了,還是沒有辦法。然後又回到不要maven的模式,又新建項目。順利運行到struts2 hello world,然而一旦使用tiles,又回到之前的cannot find the definition of "homeIndex",也就是程式進入struts.xml後,找不到tiles.xml。
不過在崩潰的過程中,收穫了對eclipse環境配置的熟悉,以及tomcat server配置的熟悉。
直到快6點了,再再再一次看官方文檔,再再再一次一行一行比對,直到增加了“org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG”配置,一下子就運行起來啦!為什麼之前沒有增加這個配置呢?因為官方文檔說 “As from Struts 2.3.28, the plugin automatically loads all Tiles definitions matching the following pattern tiles*.xml
- you don't have to specify them via org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG
in web.xml
”。你妹!
回想struts2.5.5+tiles3.0.7的辛路歷程,非常有必要把相關的配置備份在這裡。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="MyStrutsApp" version="2.4" 3 xmlns="http://java.sun.com/xml/ns/j2ee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 7 <display-name>Fuckdy</display-name> 8 <context-param> 9 <param-name>org.apache.tiles.definition.DefinitionsFactory.DEFINITIONS_CONFIG</param-name> 10 <param-value>/WEB-INF/tiles.xml</param-value> 11 </context-param> 12 <filter> 13 <filter-name>struts2</filter-name> 14 <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> 15 </filter> 16 17 <filter-mapping> 18 <filter-name>struts2</filter-name> 19 <url-pattern>/*</url-pattern> 20 </filter-mapping> 21 22 <listener> 23 <listener-class> 24 org.apache.struts2.tiles.StrutsTilesListener 25 </listener-class> 26 </listener> 27 28 </web-app>View Code
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE tiles-definitions PUBLIC 4 "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" 5 "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> 6 7 <tiles-definitions> 8 9 <definition name="mainTemplate" template="/pages/pc.jsp"> 10 <put-attribute name="title" value="demo title here" /> 11 <put-attribute name="body" value=".bodyp" /> 12 </definition> 13 14 <definition name="homeIndex" extends="mainTemplate"> 15 <put-attribute name="title" value="Article Index Page" /> 16 <put-attribute name="body" value="/pages/home/index2.jsp" /> 17 </definition> 18 19 </tiles-definitions>View Code
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 4 "http://struts.apache.org/dtds/struts-2.5.dtd"> 5 <struts> 6 <constant name="struts.enable.DynamicMethodInvocation" value="false"/> 7 <constant name="struts.devMode" value="false"/> 8 9 <package name="fuckdy" namespace="/pc" extends="struts-default"> 10 <result-types> 11 <result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/> 12 </result-types> 13 <action name="home" class="fuckdy.HomeAction" method="execute"> 14 <result name="index" type="tiles">homeIndex</result> 15 </action> 16 </package> 17 </struts>View Code
tiles運行成功的同時,也就是成功實現了母版頁的功能。
第7天
開始折騰spring mvc。首先看了官網的文檔,才發現這才是我一直想要找的構建web應用程式的框架。
然而運行一個hello world談何容易。按照官網以及各種tutorial嘗試,始終啟動tomcat時報錯找不到“org.springframework.web.servlet.DispatcherServlet”,當然在瀏覽器中輸入地址也就是404. 哎,javaee太多404了,又不說具體一點的原因。
後來想要給eclipse裝個resharper插件,因為eclipse寫個代碼幾乎跟記事本差不多,比resharper差1萬倍。才發現原來jetbrains公司開發了一個intelij IDE。簡單試用了一下,果然跟VS 裡面的resharper一樣的好用。
然後嘗試在intelij裡面建立項目,結果還是404,未果。
第 8 天
在CSDN博客上面找到一篇tutorial,基於spring 4.3.3.release版本,終於沒有折騰多久就成功了。還有就是該篇博文後面附的幾個常見錯誤非常有用,特別是需要專門下載jakarta-taglibs-standard-1.1.2.zip, 解壓後放到tomcat安裝目錄/lib裡面。不知道為什麼安裝tomcat時缺失這2個jar,也是太坑了。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 5 version="3.1"> 6 <!--<context-param> 7 <param-name>contextConfigLocation</param-name> 8 <param-value>/WEB-INF/applicationContext.xml</param-value> 9 </context-param> 10 <listener> 11 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 12 </listener>--> 13 <servlet> 14 <servlet-name>dispatcher</servlet-name> 15 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 16 <load-on-startup>1</load-on-startup> 17 </servlet> 18 <servlet-mapping> 19 <servlet-name>dispatcher</servlet-name> 20 <url-pattern>/</url-pattern> 21 </servlet-mapping> 22 </web-app>web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 6 <context:component-scan base-package="springhello"/> 7 <!--<mvc:default-servlet-handler/>--> 8 <!--<mvc:annotation-driven/>--> 9 10 <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 11 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 12 <property name="prefix" value="/views"/> 13 <property name="suffix" value=".jsp"/> 14 </bean> 15 16 </beans>dispatcher-servlet.xml
1 package springhello.web.pc.controllers; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.ModelMap; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 import org.springframework.web.servlet.ModelAndView; 8 9 /** 10 * Created by leo on 2016-12-15. 11 */ 12 @Controller 13 @RequestMapping(value = "/home", method = RequestMethod.GET) 14 public class HomeController { 15 16 @RequestMapping("/say") 17 public String say(ModelMap model){ 18 model.addAttribute("msg", "Spring MVC Hello World-2"); 19 model.addAttribute("name", "leo"); 20 return "index"; 21 } 22 23 @RequestMapping("/index") 24 public ModelAndView index(){ 25 ModelAndView mav = new ModelAndView(); 26 mav.setViewName("/pc/home/index"); 27 mav.addObject("msg","hello message"); 28 mav.addObject("name", "leo"); 29 return mav; 30 } 31 32 }HomeController.java
之前一直失敗報404,就是因為在dispatcher-servlet.xml中沒有加context配置,看了官網這麼久文檔,幾乎一行一行的閱讀,就是沒有看到4.3.4版本需要配置這個context。
還有在controller中,其實也可以像asp.net mvc那樣,直接返回view path。
用了spring之後,肯定要放棄struts2毫無疑問了。但spring並沒有提供母版頁的功能,於是開始研究spring+tiles。
在百度里隨便找了個tutorial,幾乎一次成功! 唯一折騰的就是maven管理的包需要在intelij裡面添加到lib編譯目錄。操作:file - project structure - artifacts,選中左邊web-inf/lib,點擊“+”,將project library裡面的包全部選中,然後點擊OK。重新編譯,刷新瀏覽器,成功!
web.xml不需要做任何修改,註入tiles是在dispatcher-servlet.xml裡面配置。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.hellos</groupId> 8 <artifactId>springhello</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <dependencies> 12 <dependency> 13 <groupId>org.springframework</groupId> 14 <artifactId>spring-context</artifactId> 15 <version>4.3.4.RELEASE</version> 16 </dependency> 17 <dependency> 18 <groupId>org.springframework</groupId> 19 <artifactId>spring-web</artifactId> 20 <version>4.3.4.RELEASE</version> 21 </dependency> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-webmvc</artifactId> 25 <version>4.3.4.RELEASE</version> 26 </dependency> 27 <dependency> 28 <groupId>org.apache.tiles</groupId> 29 <artifactId>tiles-core</artifactId> 30 <version>3.0.7</version> 31 </dependency> 32 <dependency> 33 <groupId>org.apache.tiles</groupId> 34 <artifactId>tiles-api</artifactId> 35 <version>3.0.7</version> 36 </dependency> 37 <dependency> 38 <groupId>commons-digester</groupId> 39 <artifactId>commons-digester</artifactId> 40 <version>2.0</version> 41 </dependency> 42 <dependency> 43 <groupId>org.apache.tiles</groupId> 44 <artifactId>tiles-jsp</artifactId> 45 <version>3.0.7</version> 46 </dependency> 47 </dependencies> 48 49 </project>pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> 6 <context:component-scan base-package="springhello"/> 7 <!--<mvc:default-servlet-handler/>--> 8 <!--<mvc:annotation-driven/>--> 9 <bean id="tilesConfigur" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer"> 10 <property name="definitions"> 11 <list> 12 <value>/WEB-INF/tiles.xml</value> 13 </list> 14 </property> 15 </bean> 16 17 <bean id="tilesView" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 18 <property name="order" value="1"/> 19 <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/> 20 </bean> 21 22 <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 23 <property name="order" value="2"/> 24 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> 25 <property name="prefix" value="/views"/> 26 <property name="suffix" value=".jsp"/> 27 </bean> 28 29 </beans>dispatcher-servlet.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 <!DOCTYPE tiles-definitions PUBLIC 4 "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" 5 "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> 6 7 <tiles-definitions> 8 9 <definition name="mainTemplate" template="/views/pc/_layout/pc.jsp"> 10 <put-attribute name="title" value="demo title here" /> 11 <put-attribute name="body" value=".bodyp" /> 12 </definition> 13 14 <definition name="homeIndex" extends="mainTemplate"> 15 <put-attribute name="title" value="Article Index Page" /> 16 <put-attribute name