admin管理员组

文章数量:1794759

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

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

 

本机环境:

JDK 1.7

IntelliJ IDEA 2017.2

 

1.新建项目

 

Create New Project

 

选择Spring MVC

 

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

 

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

 

2.修改代码文件

1)修改index.jsp文件

 

%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>$Title$</title> </head> <body> 使用IntelliJ IDEA 搭建 Spring MVC 成功! 部署成功! 运行成功! </body> </html>

 

 

 

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

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

 

然后新增一个 Java Class

 

package com.spring.mvc; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class HelloMVCController { @RequestMapping(value="/hello",method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("msg", "Spring 3 MVC Hello World"); return "index"; } }

 

 

 

3) 修改 dispatcher-servlet.xml 文件

 

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="www.springframework/schema/beans" xmlns:context="www.springframework/schema/context" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation=" www.springframework/schema/beans www.springframework/schema/beans/spring-beans-3.0.xsd www.springframework/schema/context www.springframework/schema/context/spring-context-3.0.xsd"> <!--对应src下的报名,需要自己新建--> <context:component-scan base-package="com.spring.mvc" /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <!-- 这个配置是配置JSP页面的位置,按照你自己的配置来配 --> <value>/WEB-INF</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </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运行成功

项目结构是这样的

 

 

 

源码地址:github/Xupk/HelloMVC

本文标签: 手把手环境MVCspringidea