zoukankan      html  css  js  c++  java
  • spring-mvc 练习一则

    一、建项目

    1.1 新建项目

    1.2 项目命名

    1.3 选择maven

    二、创建包以及编写代码

    2.1 pom.xml 添加依赖

    <?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>com.anotgcs</groupId>
      <artifactId>weblearn</artifactId>
      <version>1.0-SNAPSHOT</version>
      <packaging>war</packaging>
    
      <name>weblearn Maven Webapp</name>
      <!-- FIXME change it to the project's website -->
      <url>http://www.example.com</url>
    
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
      </properties>
    
      <dependencies>
    
        <!--web项目自带-->
       <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.11</version>
          <scope>test</scope>
        </dependency>
    
        <!--spring-context-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>5.2.8.RELEASE</version>
        </dependency>
    
        <!--spring-core-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-core</artifactId>
          <version>5.2.8.RELEASE</version>
        </dependency>
    
        <!--spring-webmvc-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.8.RELEASE</version>
        </dependency>
    
        <!--spring-aop-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-aop</artifactId>
          <version>5.2.8.RELEASE</version>
        </dependency>
    
          <!--spring-beans-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-beans</artifactId>
          <version>5.2.8.RELEASE</version>
        </dependency>
    
        <!--aspectjweaver-->
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjweaver</artifactId>
          <version>1.9.6</version>
          <scope>runtime</scope>
        </dependency>
    
        <!--aopalliance-->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
    
          <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
          <dependency>
              <groupId>commons-logging</groupId>
              <artifactId>commons-logging</artifactId>
              <version>1.2</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.jsp.jstl/jstl -->
          <dependency>
              <groupId>javax.servlet.jsp.jstl</groupId>
              <artifactId>jstl</artifactId>
              <version>1.2</version>
          </dependency>
    
          <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api -->
          <dependency>
              <groupId>javax.servlet.jsp</groupId>
              <artifactId>jsp-api</artifactId>
              <version>2.2</version>
              <scope>provided</scope>
          </dependency>
    
          <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
          <dependency>
              <groupId>org.hamcrest</groupId>
              <artifactId>hamcrest-core</artifactId>
              <version>1.3</version>
              <scope>test</scope>
          </dependency>
    
    
      </dependencies>
    
      <build>
        <finalName>weblearn</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>
    
    

    添加完之后可以看到项目中多出来的jar包:

    2.2 创建controller

    创建一个 com.amnotgcs.controller 并在里面创建一个HelloController

    package com.amnotgs.controller;
    
    import com.sun.org.apache.xpath.internal.operations.Mod;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    // Controllor业务类
    @Controller
    public class HelloController {
    
        @RequestMapping("/query")
        public ModelAndView query() throws Exception {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("message","这里是传过去的message");
            modelAndView.setViewName("hello");
    
            return modelAndView;
        }
    
        @RequestMapping("/para")
        public ModelAndView addStu(Integer id, String name, String addr) throws Exception {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("id", id);
            modelAndView.addObject("name", name);
            modelAndView.addObject("addr", addr);
            modelAndView.setViewName("para");
    
            return  modelAndView;
        }
    }
    
    

    2.3 创建 resource_xml 文件

    xml文件

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:p="http://www.springframework.org/schema/p"
           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-4.2.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.2.xsd">
    
        <!--注解扫描-->
        <context:component-scan base-package="com.amnotgcs" />
    
        <!--mvc注解扫描-->
        <mvc:annotation-driven />
    
        <!--url映射-->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </beans>
    

    hello.jsp文件

    hello.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page isELIgnored="false" %>
    <html>
    <head>
        <title>学习mvc</title>
    </head>
    <body>
        <h1>这里是body.</h1>
        <p>视频看到51:55</p>
        <p>${message}</p>
    </body>
    </html>
    

    para.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page isELIgnored="false" %>
    <html>
    <head>
        <meta charset="utf-8" />
        <title>这里是传值测试</title>
        <style type="text/css" >
            body {
                margin-top: 30px;
                margin-left: 10%;
            }
            div {
                color: deepskyblue;
                font-size: 30px;
            }
            span {
                color: #ff0055;
                font-size: 34px;
            }
        </style>
    </head>
    <body>
    
    <div>
        <p>你传递了以下值:</p>
        <p>id: <span>${id}</span></p>
        <P>name: <span>${name}</span></P>
        <p>addr: <span>${addr}</span></p>
    </div>
    
    </body>
    </html>
    
    

    web.xml

    web.xml

    <!DOCTYPE web-app PUBLIC
     "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd" >
    
    <web-app xmlns:mvc="http://java.sun.com/dtd/web-app_2_3.dtd">
      <display-name>Archetype Created Web Application</display-name>
    
      <!--前端控制器 DisthcherServlet-->
      <servlet>
        <servlet-name>springwebmvc</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>
      
      <servlet-mapping>
        <servlet-name>springwebmvc</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
    </web-app>
    
    

    index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page isELIgnored="false" %>
    
    <html>
        <head>
            <meta charset="utf-8">
            <title>这里是主页</title>
            <%--<link rel="stylesheet" type="text/css" href="/css/index.css" />--%>
            <style type="text/css">
                body {
                     100%;
                    margin: 5% 20%;
                }
                h2 {
                    color: #ff0055;
                }
                info {
                    margin-left: 30%;
                }
                form {
                     fit-content;
                    padding: 2rem;
                    border: 0.05rem solid blueviolet;
                }
                span {
                     10rem;
                    color: purple;
                }
                p {
                    color: deepskyblue;
                }
                input {
                    border: 0.1rem solid deeppink;
                }
            </style>
        </head>
    <body>
    <h2>Hello World!</h2>
    <div id="info">
        <p>请填写以下框框并提交:</p>
        <form action="/weblearn_war_exploded/para" method="post">
            <span>id: </span>
            <input name="id" /><br><br>
            <span>name: </span>
            <input name="name" /><br><br>
            <span>addr: </span>
            <input name="addr" /><br><br>
            <input type="submit" value="点我提交" />
        </form>
    </div>
    </body>
    </html>
    
    

    运行配置

    有了计划记得推动,不要原地踏步。
  • 相关阅读:
    !!!最常用正则表达式语法
    RunMessageScript from spy
    已知进程、线程、窗体三者中某一个的句柄,需要查找另外两者的句柄。
    如何抓取一个当前运行软件所使用的内存
    谢谢你的伤害
    游摸底河有感
    九月无诗
    游石人公园有感
    影响35岁前成功的好习惯与坏习惯
    创业经验十二谈,愿有志者共勉(转)
  • 原文地址:https://www.cnblogs.com/amnotgcs/p/13683313.html
Copyright © 2011-2022 走看看