IntelliJ IDEA 15 Create maven project

IntelliJ IDEA 15 Create maven project

illustrate

Create a new Maven project

  • new a project

  • Do not select any Maven template

  • Start a GroupId, ArifactId

  • Come up with a project name. Note: Idea_Project is the workspace where this project is stored, and mavenDemo_idea15 is the subdirectory where this project is stored.

  • After building the project, open it and click Auto-Import

  • Below is the structure of this project

Project deployment

  • click

Project: No need to set (of course, you can click Project complier output to customize the compilation directory)

Modules: You can see that this project does not have any adaptation service components (because Maven is created manually, and no Maven template is selected) - so we need to add it.

  • Select Web (add a web service component to this project, this is a web project)

  • Now set up the resource directory for the web. Double click on Web Resource Directory

  • Select the scr/main directory

  • Add webapp at the back. Well, click OK, and the Web resource directory is set.

  • Now set the directory for the web description file

  • Set in the webapp directory

Facts: Represents the adaptation service components of the current project. You can see that this project is already a web project.

Aftifacts:  The Aftifacts describe the information released by the current project. Now to add, select from Modeles.

说明:A: 现在Artifacts已有了发布的项目了(idea中准确的说应是Modele) B:output root目录描述了当前项目的编译目录及适配服务。

确定之后当前项目的结构:

  • 如有需要,添加lib包

 

部署服务器

  • 添加服务器

  • 部署

注:很多童鞋在这里找不到Arifact,请参考部署项目中的Modules的配置。如果没有为项目配置Web服务组件,那么就没有Artifact。(当前项目连Web项目都不是,哪儿来的Artifact,又部署什么呢?)

  • 注意下面的选择:

编写代码测试

  • 创建一个java类。可看到继承HttpServlet出问题了--这是因为没有把Tomcat的依赖加入Module

  • 在Modules加入Tomcat依赖

添加完毕

  • 现在按快捷键就可以了

  • 代码编辑

Java

package com.wql;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Lenovo on 2016/2/25.
 */
@WebServlet("/myController")
public class Controller extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.err.println("---");
        //解决乱码
        req.setCharacterEncoding("UTF-8");
        String name=req.getParameter("name");
        req.setAttribute("name",name);
        System.out.println(name);
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }

}

Html

<%--
  Created by IntelliJ IDEA.
  User: Lenovo
  Date: 2016/2/25
  Time: 0:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="myController" method="post">
    <input name="name">
    return:${name}
    <input value="提交" type="submit">
</form>
</body>
</html>

Xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           version="3.0">
<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
  • 记得设置默认启动浏览器

  • 启动项目

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324656407&siteId=291194637