其实没有完整的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>
折腾了挺久,终于配置成功。访问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>
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>
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>
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="body" value="/views/pc/home/index2.jsp" /> 17 </definition> 18 19 </tiles-definitions>
tiles.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 import org.springframework.web.servlet.view.tiles3.TilesView; 9 10 /** 11 * Created by leo on 2016-12-15. 12 */ 13 @Controller 14 @RequestMapping(value = "/home", method = RequestMethod.GET) 15 public class HomeController { 16 17 @RequestMapping("/say") 18 public String say(ModelMap model){ 19 model.addAttribute("msg", "Spring MVC Hello World-2"); 20 model.addAttribute("name", "leo"); 21 return "index"; 22 } 23 24 @RequestMapping("/index") 25 public ModelAndView index(){ 26 ModelAndView mav = new ModelAndView(); 27 mav.setViewName("/pc/home/index"); 28 mav.addObject("msg","hello message"); 29 mav.addObject("name", "leo"); 30 return mav; 31 } 32 33 @RequestMapping("/index2") 34 public String index2(){ 35 //TilesView view = new TilesView(); 36 //view.setUrl("homeIndex"); 37 return "homeIndex"; 38 } 39 40 }
HomeController.java
尝试用ModelAndView作为返回值,因为这个类更利于后期做架构用,一次成功:
@RequestMapping("/index3") public ModelAndView index3(){ ModelAndView mav = new ModelAndView(); mav.setViewName("homeIndex"); mav.addObject("msg", "This is index3 action"); return mav; }
在tiles.xml为每一个页面写一个配置实在不是一个好的实践,于是还得继续尝试通过annotation的方式进行配置。
google了半天似乎只有struts2提供了annotation方式写tiles definition,而spring并未提供。看来只有写xml了。
第9天
尝试使用intellij的module功能对项目进行架构,把java代码全部移到其他的module,而web这个module只保留jsp、css、js和图片等静态资源。一次成功!
将项目share到github,地址为:https://github.com/leotsai/jframe
接下来的工作就是将.NET MVC下面的MvcSolution转移到java平台下的jframe了。
jframe 框架
基于spring mvc搭建的多层级多模块java web应用程序框架。包含:基础设施层、数据库定义规范、数据库访问规范、日志记录规范、多层级异常捕获、标准ajax规范、母版页规范、视图呈现规范、JavaScript框架规范等。
技术交流QQ群:651499479