zoukankan      html  css  js  c++  java
  • Springmvc:(二)第一个程序

    一、创建流程

    1. 创建maven项目,添加web支持

    2. 导入spring-webmvc依赖

    3. 解决maven静态资源过滤的问题

          <build>
              <resources>
                  <resource>
                      <directory>src/main/java</directory>
                      <includes>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>false</filtering>
                  </resource>
                  <resource>
                      <directory>src/main/resources</directory>
                      <includes>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>false</filtering>
                  </resource>
              </resources>
          </build>
      
    4. File---Project Structure---Artifacts-----WEB-INF----右键新建lib文件夹----单击加号,添加jar包

    5. 编写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>
              <!--关联springMVC配置-->
              <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>/</url-pattern>
          </servlet-mapping>
      </web-app>
      
    6. 创建springmvc.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"
             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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
          <!--自动扫描包-->
          <context:component-scan base-package="com.ruanyuan.Controller"/>
          <!-- 让Spring MVC不处理静态资源 js、css、img -->
          <mvc:default-servlet-handler/>
          <!--注册默认梳理请求,返回值,参数的类-->
          <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>
      
    7. 创建HelloController

      package com.ruanyuan.Controller;
      
      import org.springframework.stereotype.Controller;
      import org.springframework.ui.Model;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.servlet.ModelAndView;
      
      import java.lang.annotation.Annotation;
      
      @Controller
      @RequestMapping("/HelloController")
      public class HelloController {
      
          //真实访问地址 : 项目名/HelloController/hello
          @RequestMapping("/hello")
          public String sayHello(Model model) {
              //向模型中添加属性msg与值,可以在JSP页面中取出并渲染
              model.addAttribute("msg", "hello,SpringMVC");
              //web-inf/jsp/hello.jsp
              return "hello";
          }
      }
      
    8. 创建hello.jsp

      <%--
        Created by IntelliJ IDEA.
        User: zbp
        Date: 2020/3/3
        Time: 15:35
        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>
      
      
    9. 执行结果

      image-20200303163733920

    二、拓展

    1. 解决中文乱码问题

      <!-- 配置过滤器,解决中文乱码的问题 -->
      <filter>
      	<filter-name>characterEncodingFilter</filter-name>
      	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-
      class>
      <!-- 指定字符集 -->
      <init-param>
      	<param-name>encoding</param-name>
      	<param-value>UTF-8</param-value>
      </init-param>
      </filter>
      	<filter-mapping>
      	<filter-name>characterEncodingFilter</filter-name>
      	<url-pattern>/*</url-pattern>
      </filter-mapping>
      
  • 相关阅读:
    docker如何将运行中的容器保存为docker镜像?
    java8流的地址
    maven命令package、install、deploy
    windows下设置redis开机自启动
    mysql的安装参考
    service mysql启动失败unit not found
    JAVA中Wait()与Notity()、同步队列与等待队列
    Java8函数式编程
    Groovy ConfigSlurper()读取配置文件简易示例
    SoapUI官方文档
  • 原文地址:https://www.cnblogs.com/dreamzone/p/12485646.html
Copyright © 2011-2022 走看看