Intellij idea 开发第一个springMVC demo

来源:互联网 发布:淘宝鲜花网 编辑:程序博客网 时间:2024/06/05 09:21

第一个springMVC例子
主要目的是熟悉intellij的web项目操作和springMVC的基本概念

具体操作如下:

1.创建intellij idea web module
这里写图片描述

然后填写module名 点击确认 module创建完毕

2.在module的web-WEB-INF下新建 classes和lib文件夹
这里写图片描述

2.修改项目的配置 点击file->Project Struture 修改Modules下的paths 修改完毕点击OK
这里写图片描述

3.配置tomcat 点击run->edit configurations
点绿色的十字图标 创建tomcat并配置
这里写图片描述
这里写图片描述
这里写图片描述

4.首先配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"         version="3.1">    <!--配置dispatcherservlet 以及mapping-->    <servlet>        <servlet-name>mvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>mvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping></web-app>

5.配置mvc-servelet

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="        http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:component-scan base-package="com.jsu.mvc" />    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix">            <!-- 这个配置是配置JSP页面的位置,按照你自己的配置来配 -->            <!--jsp文件夹需要自己手动创建-->            <value>/WEB-INF/jsp/</value>        </property>        <property name="suffix">            <value>.jsp</value>        </property>    </bean></beans>

6.在jsp目录下新建hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %><html><head>    <title>hello mvc</title></head><body>${msg }</body></html>

7.在src中编写controller类

@Controllerpublic class HelloController { @RequestMapping(value="/hello",method = RequestMethod.GET)    public String printWelcome(ModelMap model) {        model.addAttribute("msg", "Spring 3 MVC Hello World");        return "hello";    }}

最后进行测试
这里写图片描述

第一个小demo到此结束!

0 0