一、Spring MVC起步——IntelliJ IDEA 搭建Spring MVC环境(手把手搭建)

10 篇文章 0 订阅

 

 

 

本机环境:

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.  
  3. import org.springframework.stereotype.Controller;

  4. import org.springframework.ui.ModelMap;

  5. import org.springframework.web.bind.annotation.RequestMapping;

  6. import org.springframework.web.bind.annotation.RequestMethod;

  7.  
  8. @Controller

  9. public class HelloMVCController {

  10. @RequestMapping(value="/hello",method = RequestMethod.GET)

  11. public String printWelcome(ModelMap model) {

  12. model.addAttribute("msg", "Spring 3 MVC Hello World");

  13. return "index";

  14. }

  15. }

 

 

 

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.  
  13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

  14. <property name="prefix">

  15. <!-- 这个配置是配置JSP页面的位置,按照你自己的配置来配 -->

  16. <value>/WEB-INF</value>

  17. </property>

  18. <property name="suffix">

  19. <value>.jsp</value>

  20. </property>

  21.  
  22. </bean>

  23.  
  24. </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

 

本文链接:https://blog.csdn.net/love_java_cc/article/details/76359815

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

那些年的代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值