IntelliJ IDEA 搭建Spring MVC环境

原文地址:点击

本机环境:

JDK 1.7

IntelliJ IDEA 2017.2

 

1.新建项目

 

Create New Project

 

选择Spring MVC

 

填写项目名和项目存放位置

 

然后点击Finish,项目就新建完成了。

 

2.修改代码文件

1)修改index.jsp文件

 


 
 
  1. %@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>$Title$ </title>
  5. </head>
  6. <body>
  7. 使用IntelliJ IDEA 搭建 Spring MVC 成功!
  8. 部署成功!
  9. 运行成功!
  10. </body>
  11. </html>

 

 

 

2)新增一个控制器HelloMVCController.java

首先要在src目录下新建一个包,我这里把它命名为  com.spring.mvc

 

然后新增一个 Java Class

 


 
 
  1. package com.spring.mvc;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.ModelMap;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. @Controller
  7. public class HelloMVCController {
  8. @RequestMapping(value= "/hello",method = RequestMethod.GET)
  9. public String printWelcome(ModelMap model) {
  10. model.addAttribute( "msg", "Spring 3 MVC Hello World");
  11. return "index";
  12. }
  13. }

 

 

 

3) 修改 dispatcher-servlet.xml 文件

 


 
 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context= "http://www.springframework.org/schema/context"
  4. xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation= "
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10. <!--对应src下的报名,需要自己新建-->
  11. <context:component-scan base-package="com.spring.mvc" />
  12. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  13. <property name="prefix">
  14. <!-- 这个配置是配置JSP页面的位置,按照你自己的配置来配 -->
  15. <value>/WEB-INF </value>
  16. </property>
  17. <property name="suffix">
  18. <value>.jsp </value>
  19. </property>
  20. </bean>
  21. </beans>

 

 

 

 

3.部署项目

1) 部署服务器tomcat

点击菜单 Run-->Edit Configurations

 

点绿色的+号,选择 tomcat-->local

 

配置server

 

新增项目部署

 

部署完成

 

 

4.调试运行

点击左下角或右上角的绿色小三角符号或者臭虫符号,就可以启动服务器了

 

5.报错修改

启动看日志,报错:

严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

 

修正错误:

1) 停掉服务器

 

2) 打开 Project Structure...

 

选择 Artifacts-->Output Layout

 

完成

 

 

6.再次运行

浏览器输入网址:localhost:8080运行成功

项目结构是这样的

 

 

 

源码地址:https://github.com/Xupk/HelloMVC

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值