原文链接:http://www.leon0204.com/article/108.html
下载安装 Idea ,支持付费正版,破解码自己去网上。
1 创建 Maven 项目
这里 New 的路径选择到 maven 的 Home 目录:
/Library/Java/JavaVirtualMachines/jdk1.8.0_161.jdk/Contents/Home/
创建出来的maven 项目代码结构:
红色部分是之后我创建的。
- recources文件夹,该文件夹一般用来存放一些资源文件,
webapp文件夹,用来存放web配置文件以及jsp页面等
这里Idea 提示你 Enable-Auto- Import,点击,可以在每次修改pom.xml后,自动的下载并导入jar包。
创建 src/main/java
目录,并且标记为 Sources Root
2 用 Maven 导入 jar 包
Maven项目对象模型(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>com.leon</groupId>
<artifactId>leon.test</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>leon.test Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
<build>
<finalName>leon.test</finalName>
</build>
</project>
因为墙的关系,添加阿里云代理。
3 创建 SpringMvc框架
/Users/leon/IdeaProjects/leontest/src/main/webapp/WEB-INF/web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.jpg</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
</web-app>
/Users/leon/IdeaProjects/leontest/src/main/webapp/WEB-INF/dispatcher-servlet.xml
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"
>
<!-- 自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter 两个bean,是spring MVC为@Controllers分发请求所必须的。-->
<mvc:annotation-driven/>
<!-- 标签是告诉Spring 来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件。-->
<context:component-scan base-package="controller"/>
<!-- mvc view 对应文件的前缀与后缀 action 返回值 为 "index" => "/index.jsp" -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- @ResponseBody 返回 json 格式数据 -->
<bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<!-- Support JSON -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
<bean id="exceptionMessageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver">
<property name="messageConverters">
<list>
<!-- Support JSON -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean>
</beans>
/Users/leon/IdeaProjects/leontest/src/main/java/controller/testpage.java
package controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;
@Controller
/**
* Created by yuwen on 17/2/15.
*/
public class testpage {
@RequestMapping("/helloworld")
public String hello(){
System.out.println("hello world");
return "success";
}
@ResponseBody
@RequestMapping(value="/body/{x}", method = RequestMethod.GET)
public bodytest getBody(@PathVariable("x") String x){
System.out.println("URI Part 1 : " + x);
bodytest bt = new bodytest();
bt.a = x;
bt.b = "123";
return bt;
}
public class bodytest
{
public String a;
public String b;
}
/*
*
@RequestMapping(value = "/user/{userId}/roles/{roleId}", method = RequestMethod.GET)
public String getLogin(@PathVariable("userId") String userId,
@PathVariable("roleId") String roleId) {
System.out.println("User Id : " + userId);
System.out.println("Role Id : " + roleId);
return "success";
}
@RequestMapping(value="/product/{productId}",method = RequestMethod.GET)
public String getProduct(@PathVariable("productId") String productId){
System.out.println("Product Id : " + productId);
return "success";
}
* */
}
/Users/leon/IdeaProjects/leontest/src/main/webapp/success.jsp
<%--
Created by IntelliJ IDEA.
User: leon
Date: 2018/2/28
Time: 上午10:22
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>
<h2>Hello World! success </h2>
</body>
</html>
注意,dispatcher-servlet.xml 中的 base-package 是你创建的包名
运行,tomcat run spirngMvc 项目,需要先配置一下idea 的 tomcat 。
配置这两个地方:
访问 http://localhost:8080/helloworld
访问 http://localhost:8080/body/2918