zoukankan      html  css  js  c++  java
  • JAVA入门教程

    用的是Idea2017版本。其他大同小异

    1.新建项目

    2.勾选Create from archetype 选中maven-archetype-webapp

    3.输入项目名字。

    4.下一步

    5.点Finish后要等一会,在用maven下载模版,根据网速情况时间不一样。新建完成后结构如下

    6.打开pom.xml文件添加引用包,(如果不知道pom引用方式,打开http://mvnrepository.com/ 示例查找spring-beans,然后点进去,选一个版本,会把maven的xml代码也放出来复制到pom文件里即可)

    pom.xml代码如下:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>zwtest8</groupId>
      <artifactId>zwtest8</artifactId>
      <packaging>war</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>zwtest8 Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- spring版本号 -->
        <spring.version>4.1.1.RELEASE</spring.version>
        <!-- mybatis版本号 -->
        <mybatis.version>3.2.6</mybatis.version>
        <!-- log4j日志文件管理包版本 -->
        <slf4j.version>1.7.7</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <!-- jackson包版本 -->
        <jackson.version>2.5.0</jackson.version>
        <servlet.version>3.0.1</servlet.version>
        <jstl.version>1.2</jstl.version>
      </properties>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
    
        <!-- servlet -->
        <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>${servlet.version}</version>
          <scope>provided</scope>
        </dependency>
    
        <dependency>
          <groupId>jstl</groupId>
          <artifactId>jstl</artifactId>
          <version>${jstl.version}</version>
        </dependency>
    
        <!-- springMVC核心包 -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <!-- spring核心包 -->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context-support</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-tx</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>${spring.version}</version>
        </dependency>
    
    
        <!-- mybatis核心包 -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>${mybatis.version}</version>
        </dependency>
    
        <!--mybatis spring 插件 -->
        <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis-spring</artifactId>
          <version>1.2.2</version>
        </dependency>
    
        <!-- Mysql数据库驱动包 -->
        <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.34</version>
        </dependency>
      </dependencies>
      <build>
        <finalName>zwtest7</finalName>
      </build>
    </project>

    然后右击pom.xml点Maven->Reimport (下载所有包,可能要一会时间)

     

    7.新建几个目录:main/java/(存放java代码,各种java类库),WEB-INF/views(存放jsp页用的)

    8.这时候想要在右击java目录建一个controller包,建不了

     

    9.解决办法,点菜单File-Project Structure,在左边点“Modules”选项.展开选中java目录,点上面的的Sources,选中resources目录,点上面的Resources按钮.->如下,OK

    10.这个时候右击java目录新建 一个pages,名称为controller

    11,在controller包下 新建一个controlle   名为test类

     

    test代码如下

    package controller;
    
    
    import org.springframework.stereotype.Controller;
    import org.springframework.ui.ModelMap;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    @Controller
    @RequestMapping("/test")
    public class test {
    
        @RequestMapping(value = "index.do")
        public String getUser(HttpServletRequest request, HttpServletResponse response, ModelMap mode) {
    
            mode.addAttribute("aaa", "hello v1");//传值到前台测试
            return "test/index";
        }
    
        @RequestMapping("abc.do")
        public String success(){
    
            return "test/abc";
        }
    }



    12.在views目录建一个index.jsp,代码如下:
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    ${aaa}
    <hr>
    Hello Spring MVC
    </body>
    </html>

    12.在resources目录下新建SpringMVC.xml文件内容如下:

     <context:component-scan base-package="controller"/> 指定Controller包名
    <property name="prefix" value="/WEB-INF/views/"/> 指定View页路径
    <?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="controller"/>

    <!-- JSP视图文件解析配置 -->
    <bean id="urlBasedViewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
    <!-- <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView"
    /> -->
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    </bean>

    </beans>

    13.打开web.xml文件代码如下:(其中

    <param-value>classpath:SpringMVC.xml</param-value>为指定SpringMVC的配置文件名
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">这里一定要是2.4,如果2.3使用的EL表达式在jsp页不会解析.
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <display-name>Archetype Created Web Application</display-name>



    <servlet>
    <servlet-name>mvc-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:SpringMVC.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
    <servlet-name>mvc-dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

    14. 以上文件都建好以后项目结构如下:(其中abc.jsp里面就是一个空的页面,用来测试别的,你也可以建一个)

     

    15.调试运行(如没有安装tomcat请选去下载安装tomcat9),点菜单Run->Edit Configurations

    16.点加号选Tomcat Server->Local

    17.输入名称,如下图(Application server 选Tomcat9,如没安装请选去网上下载安装) ,After launch为运行以后启动的浏览器。系统默认为8080,我修改了为8181

     18.点Deployment 选项,点右边加号

    19.选中带exploded这个然后OK

    20.最后运行

    结果如下

    点击这里下载项目源代码

    https://files.cnblogs.com/files/q149072205/zwtest8.rar

  • 相关阅读:
    [NOI2003][bzoj1507] 文本编辑器 editor [splay]
    GDKOI 游记
    [填坑完毕] 寒假作业计划
    省选算法学习-数据结构-splay
    NOIP2017游记
    真·总结
    赛前
    十一黄(xun)金(lian)周感想
    9.17 模拟赛
    9.14 模拟赛
  • 原文地址:https://www.cnblogs.com/q149072205/p/7593318.html
Copyright © 2011-2022 走看看