其他分享
首页 > 其他分享> > 第一个springmvc项目HelloSpringMVC

第一个springmvc项目HelloSpringMVC

作者:互联网

一、配置方式
1、创建一个父项目,新建一个Module,如springmvc-03,完成后选择已创建的module右击选Add Framwork Support添加web支持

2、确定导入了SpringMVC依赖(在pom.xml文件中添加springmvc和servlet依赖)

说明:若父项目中添加了这些依赖,子项目继承父项目可以不用在自己的pom文件中再次添加

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.12.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
    </dependencies>

3、配置web.xml,注册DispatcherServlet

<?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_4_0.xsd"
         version="4.0">
<!--    配置DispatcherServlet:这个是springmvc的核心;请求分发器,前端控制器-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc-servlet.xml</param-value>
        </init-param>
<!--        启动级别: 跟服务器一起启动 -->
        <load-on-startup>1</load-on-startup>
    </servlet>
<!--    springmvc中/ 与/*是不同的
                  /:匹配除jsp之外的所有请求
                  /*:包括jsp在内的所有请求-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

4.在resoueces编写SpringMVC 的 配置文件!如名称:springmvc-servlet.xml 需同web.xml中配置的中classpath值相同

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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.xsd">
    <!--配置处理器映射器-->
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    <!--配置处理器适配器-->
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
    <!--配置视图解析器:DispatcherServlet给他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

    <bean id="/hello" class="com.zhou.controller.HelloController"/>
</beans>

5.写Contoroller,使用ModelAndView封装数据和视图对象

public class HelloController implements Controller {
    @Override
    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        ModelAndView mv = new ModelAndView();
        //业务代码
        String result="HelloSpringMVC";
        mv.addObject("msg",result);


        //视图跳转
        mv.setViewName("test");


        return mv;
    }
}

6.将Controller交给SpringIOC容器管理(在springmvc-servlet.xml中注册Bean)

<bean id="/hello" class="com.zhou.controller.HelloController"/>

7、编写前端视图页面

<%--
  Created by IntelliJ IDEA.
  User: mm
  Date: 2022/5/7
  Time: 10:58
  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>
${msg}
</body>
</html>

8、发布项目,启动Tomcat后,在浏览器输入localhost:8080/hello访问页面

二、注解方式(推荐)

1、创建一个新的空的maven项目(父项目),新建一个module,如springmvc-02-annotation

2、添加web支持(同方案一),右击新建module,点击 Add Framwork Support添加web支持

3、在pom.xml中导入springmvc和servlet依赖

<!--1.注册DispatcherServlet-->
<servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:springmvc-servlet.xml</param-value>
    </init-param>
    <!--启动级别-1-->
    <load-on-startup>1</load-on-startup>
</servlet>
<!--/ 匹配所有的请求;(不包括.jsp)-->
<!--/* 匹配所有的请求;(包括.jsp)-->
<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

4、在resoueces编写SpringMVC 的 配置文件!如名称:springmvc-servlet.xml 需同web.xml中配置的中classpath值相同

<?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">
<!--    自动扫描包,让指定包下的注解生效 ,由IOC容器统一管理-->
    <context:component-scan base-package="com.zhou.controller"/>
<!--    让springmvc不处理静态资源-->
    <mvc:default-servlet-handler/>
<!--    支持MVC注解驱动-->
    <mvc:annotation-driven/>
    <!--配置视图解析器:DispatcherServlet给他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

5、写controller,使用ModelAndView封装数据模型和对象

<?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">
<!--    自动扫描包,让指定包下的注解生效 ,由IOC容器统一管理-->
    <context:component-scan base-package="com.zhou.controller"/>
<!--    让springmvc不处理静态资源-->
    <mvc:default-servlet-handler/>
<!--    支持MVC注解驱动-->
    <mvc:annotation-driven/>
    <!--配置视图解析器:DispatcherServlet给他的ModelAndView-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver">
        <!--前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

6、编写前端视图页面

<%--
  Created by IntelliJ IDEA.
  User: 周志飞
  Date: 2022/5/7
  Time: 16:57
  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>
${msg}

</body>
</html>

7、发布项目,启动Tomcat后,在浏览器输入localhost:8080/hello访问页面

标签:xml,web,第一个,springmvc,视图,HelloSpringMVC,ModelAndView,servlet
来源: https://www.cnblogs.com/zzhif/p/16243850.html