您好,欢迎来到刀刀网。
搜索
您的当前位置:首页SpringMVC系列(一)——IDEA+SpringMVC_HelloWorld

SpringMVC系列(一)——IDEA+SpringMVC_HelloWorld

来源:刀刀网

开发环境:


  • 本篇文章主要是先配置好SpringMVC+Idea的环境,为之后SpringMVC的学习及Idea的使用奠定基础。
  • 下面就按步骤一步一步来敲
第一步:
  • 需要在工程下创建一个Module,命名为SpringMVC_Hello
第二步:导包
  • 在web > WEB_INF目录下创建一个lib目录
  • 将需要用到的jar包放到lib目录下
  • 选中所有jar包右键add as library,将jar包添加到项目的库中
第三步: 配置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">
    <!--contextConfigLocation这个参数的意义是将会去WEB-INF路径下找applicationContext.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--这里的dispacher名称必须与WEB-INF下自己创建的<servlet-name>-servlet.xml这个配置文件的名称一致!-->
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <!--只需要改这个地方将默认的改成 / 即可,用于拦截所有请求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
第四步: 配置dispatcher-servlet.xml
<?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"
       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">

    <!--配置自动扫描的包-->
    <context:component-scan base-package="top.idalin.*"></context:component-scan>

    <!--配置映射解析器-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>
第五步: 写index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
      <a href="${pageContext.request.contextPath}/testSpring">测试SpringMVC_HelloWorld</a>
  </body>
</html>
第六步: 编写一个控制器类

@Controller
public class SpringMVCController {

    @RequestMapping("/testSpringMVC")
    public String testSpringMVC() {

        System.out.println("访问SpringMVC");
        return "success";

    }

}

注意:

  • 控制器类上面@RequstMapping注解中的值应该与index.jsp入口文件中的访问地址一致
  • 返回值是一个字符串,映射解析器会根据配置的前缀加返回值加后缀的形式去访问这个地址(prefix + “success” + suffix)
第七步: 在WEB-INF/pages文件夹下创建一个success.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>测试成功</h1>
</body>
</html>
第八步: 配置Tomcat

本文首发在,喜欢的小伙伴给个赞吧….

项目中涉及的代码及jar包我已同步到,喜欢的小伙伴给个Star吧.

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- gamedaodao.com 版权所有 湘ICP备2022005869号-6

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务