ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring MVC开发(入门级案例建议收藏~)

2021-09-23 21:58:45  阅读:228  来源: 互联网

标签:do plugin springmvc spring maven 入门级 MVC jsp servlet


准备工作

  1. 加入依赖(这里的重点的依赖是jsp和servlet的依赖以及判断是springmvc的核心的springmvc依赖!)
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>ch001-hello-springmvc</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>



  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/javax.servlet.jsp-api -->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>javax.servlet.jsp-api</artifactId>
      <version>2.3.3</version>
      <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

  </dependencies>

  <build>
    <finalName>ch001-hello-springmvc</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

2.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_4_0.xsd"
         version="4.0">
<!--    声明注册-->
    <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.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>



</web-app>

关于参数(准备单独拿出来说),简单说,数字定义为1,表示首先开启这个任务,数字1表示的越小,表示首先被开启。表示要执行的方法,*表示任何的位置,do,表示后缀是do的servlet。

3.springmvc的配置文件`

<?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 https://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.bjpowernode.controller"></context:component-scan>
    <!--注册视图解析器:帮助我们处理视图的路径和扩展名。生成视图对象-->
    <!--注册内部资源视图解析器InternalResourceViewResolver-->
<!--    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
<!--        &lt;!&ndash;前缀:表示视图所在的路径&ndash;&gt;-->
<!--        <property name="prefix" value="/WEB-INF/jsp/"/>-->
<!--        &lt;!&ndash;后缀:表示视图文件的扩展名&ndash;&gt;-->
<!--        <property name="suffix" value=".jsp"/>-->
<!--    </bean>-->
</beans>

其中配置文件的代码是表示扫描器,需要首先先对其进行扫描。特别注意的是,原始的xml文件中没有某个网址。

xmlns:context="http://www.springframework.org/schema/context"

可以直接使用上面的xml代码,直接保存到固定的模板进行。
4.index.jsp文件

<%--
  Created by IntelliJ IDEA.

  Date: 2021/9/22
  Time: 21:40
  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>
    <p>第一个springmvc项目</p>
    <p><a href="some.do">发起some.do的请求</a> </p>

</body>
</html>

其中herf表示要转的servlet。
5.show.jsp文件

<%--
  Created by IntelliJ IDEA.

  Date: 2021/9/23
  Time: 9:47
  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>
    <h3>show.jsp</h3>
    <h3>msg数据:${msg}</h3>
    <h3>fun数据:${fun}</h3>

</body>
</html>

6.主控制文件:

@Controller

public class MyController {
    @RequestMapping("/some.do")
    public ModelAndView doSome(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("msg","欢迎使用springmvc做web开发");
        mv.addObject("fun","执行的是dosome方法");
        mv.setViewName("/show.jsp");
        return mv;
    }


}

使用注解Controller进行控制,使用@RequestMapping注解进行映射,这个注解的作用是将http请求映射到MVC和RES控制器的处理方法上。这个注解的位置是放在类或者方法上面。
其中还包括一个import org.springframework.web.servlet.ModelAndView;ModelAndView是一个包括视图和模型数据类型的对象,需要进行返回。
7.启动tomcat
分别看一下index的页面以及转发的请求。在这里插入图片描述
在这里插入图片描述
显示成功,页面进行了跳转。

标签:do,plugin,springmvc,spring,maven,入门级,MVC,jsp,servlet
来源: https://blog.csdn.net/Mr_young_/article/details/120443044

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有