javaweb學習路之一--web項目搭建


概述:

  工作閑暇時間想要自己搭建一個web項目玩玩,沒想到大半天才弄了一個springMVC+mybatis的網站,簡直菜的不行,以下記錄所有的步驟加深印象

使用環境 

1.jdk1.8 

2.maven 3.3.9

3.eclipse 4.6

4.tomcat 8.0

5.mysql

源碼放在百度雲

http://pan.baidu.com/s/1c5IlR4

簡單說一下注意事項

1.jdk 注意配置環境變量,配置JAVA_HOME,path等,驗證如下

image

2.maven配置環境變量,配置MAVEN_HOME,path等,驗證如下

image

另:安裝maven后修改conf文件夾的setting.xml

image

添加<localRepository>E:\Maven\Repositories</localRepository>設置本地Repositories的位置

image

profiles節點中加入

<profile> 
    <id>jdk18</id> 
     <activation> 
          <activeByDefault>true</activeByDefault> 
          <jdk>1.8</jdk> 
     </activation> 
     <properties> 
          <maven.compiler.source>1.8</maven.compiler.source> 
          <maven.compiler.target>1.8</maven.compiler.target> 
          <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> 
     </properties>  
</profile>

3.eclipse離線加入maven插件

     i.在eclipse目錄下添加links和myplugins(隨便命名)

     ii.將插件解壓到myplugins中(雲盤有,或者網上下載)

     iii.links下新建maven.link(隨便命名) 內容:path=E:/IDE/eclipse/myplugins    對應上面myplugins的路徑
 

項目構建

1.打開eclipse

    i.File--》New—》other --》輸入maven選擇Maven Project—》next

image

  ii.添加項目路徑(我使用了默認)--》next

image

  iii.輸入webapp 創建一個web項目

image

  iv.填寫GroupId和Artifact Id,Pakage暫時不填—>finished

image

2.問題解決

創建項目后,報錯如下

image

解決方案:

第一步:切換到navigator目錄 修改以下兩個文件夾信息

image

image

  第二步:回到Project Explorer,右擊項目—》build path --》Libraries目錄—》Add Library—》Server Runtime --》選擇一個tomcat 7、8都行 如果沒有的話 需要在eclipse里面添加tomcat(install tomacat到tomcat的根目錄)

image

image

第三步:Order and Export里面勾選tomcat--》apply

image

ok 問題解決 完成后文件結構如下:

image

2.配置文件

   i.添加如下配置文件;每一行都以注釋加以解釋了,可下載源碼來(也在雲盤)

image

  ii.添加jar包,使用maven添加,具體見pom.xml

  iii.修改web.xml 默認使用servelet 2.3 改為3.1,加載配置文件,完整文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mybatis.xml</param-value>
  </context-param>
  <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <async-supported>true</async-supported>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

         3.MVC文件結構

image

不涉及配置,比較簡單,一般項目為以上結構

4.JUnit測試

image

5.jsp測試

jsp如下

image

image

右擊項目—》Run as—》Run on server –》tomcat 8.0

url輸入http://localhost:8080/MyTest/user/index

跳轉如下

image

完成

 

下一步,將項目提交到github,並逐步完善


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM