intellij 对 Spring MVC + tomcat 7简单配置
来源:互联网 发布:网络集成商有哪些 编辑:程序博客网 时间:2024/05/22 15:53
文章目的:尝试一下用 intellij 配置Spring MVC + tomcat 7,简单地记录一下步骤以及遇到的坑。
Step 1. create a new empty maven project.
Step 2. specify packaging type and add dependencies to pom file(minimum dependencies for Spring MVC are as follows).
Important Note: A war file is needed for tomcat deployment, as when tomcat is launched, class loader will load the jar files from the lib folder in tomcat. However, in maven project, the jar files are in maven repository and will not import to the lib folder. Packaging the war file and deploying it to tomcat will be needed.
<packaging>war</packaging> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.5.RELEASE</version> </dependency>
Step 3. project structure -> Modules to add a new web module.
Step 4. project structure -> Artifacts to create an artifact of current module. (Check the checkbox of Include in project build). After this, we have the web module project structure.
Step 5. in web.xml, add a sample servlet config as follows
<servlet> <servlet-name>sample</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>sample</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
Step 6. add a servlet config file sample-servlet.xml in folder WEB-INF
<?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" 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.xsd"> <mvc:annotation-driven/> <context:component-scan base-package="com.warrantli.demo"/></beans>
Step 7. create package and class SampleController as follows
package com.warrantli.demo.controller;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/** * Created by warrantli on 2017/6/18. */@Controllerpublic class SampleController { private final Log logger = LogFactory.getLog(SampleController.class); @RequestMapping(value = {"/"}, method = {RequestMethod.HEAD}) public String head() { return "sample.jsp"; } @RequestMapping(value = {"/"}, method = {RequestMethod.GET}) public String sample(Model model) { logger.info("==== processing sample method ===="); model.addAttribute("title", "Sample Title"); model.addAttribute("message", "This is a sample page!"); return "sample.jsp"; }}
Step 8. create sample.jsp file under web folder as follows
<%-- Created by IntelliJ IDEA. User: warrantli Date: 2017/6/18 Time: 16:33 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>${message}</body></html>
Step 9. Run -> Edit Configurations -> +(Left upper) -> Tomcat Server -> Local to create a tomcat server. Add an artifact to the deployment by clicking Fix button at the bottom of the popup window(or in the deployment tab).
Step 10. Run tomcat and launch browser to visit http://localhost:8080/ ~!
Important Note: If below error occurs, you may check your project structure -> modules -> Web -> Deployment Descriptor & Web Resource Directories settings whether the web.xml location is correct or the .iml file contains below code.
<component name="FacetManager"> <facet type="web" name="Web"> <configuration> <descriptors> <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" /> </descriptors> <webroots> <root url="file://$MODULE_DIR$/web" relative="/" /> </webroots> <sourceRoots> <root url="file://$MODULE_DIR$/src/main/java" /> <root url="file://$MODULE_DIR$/src/main/resources" /> </sourceRoots> </configuration> </facet> <facet type="Spring" name="Spring"> <configuration /> </facet> </component>
- intellij 对 Spring MVC + tomcat 7简单配置
- Java Spring MVC 入门- HelloWord+配置IntelliJ idea的Tomcat服务器
- Tomcat下Spring MVC配置
- Embedded Jetty+Spring MVC + Maven (IntelliJ 14) [三种简单启动方法 未完待续复杂配置]:
- IntelliJ IDEA 最简单配置搭建Spring MVC Java web Maven项目
- Intellij环境配置(Java+Tomcat+Maven+Spring)
- spring mvc rocketMq简单配置
- Spring Boot对Spring MVC的配置
- activeMQ+spring+tomcat简单配置
- Eclipse+tomcat+maven 配置Spring mvc
- Tomcat + Spring-framework MVC配置简介
- Spring mvc 对Controller配置AOP
- Spring mvc系列一之 Spring mvc简单配置
- Spring mvc系列一之 Spring mvc简单配置
- 从零开始的spring mvc,spring mvc简单配置
- Intellij idea13配置tomcat,并实现一个简单的servlet
- Spring MVC第一个简单配置实例
- 简单配置Spring MVC里面的UEditor
- Hello World感知机,懂你我心才安息 (深度学习入门系列之五)
- Java 7 新的 try-with-resources 语句,自动资源释放
- SQLAlchemy中ResultProxy的正确使用
- Windows+tomcat+Nginx集群
- C++代码风格养成之路(一)
- intellij 对 Spring MVC + tomcat 7简单配置
- iText7根据html表格(table)代码生成表格、解决跨行跨列问题
- Redis学习——Redis持久化之RDB备份方式保存数据
- 学习Linux命令(32)
- Turing Tree(线段树区间不同数字)
- LeetCode 100. Same Tree
- 损失函数减肥用,神经网络调权重(深度学习入门系列之六)
- acm_step1.1.6 1.1.7 1.1.8
- h264编码速度质量参数设置