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入门 第一课 --.Net Core 使用Docker全程记录
    阿里云 Windows Server 2012 r2 部署asp.net mvc网站 平坑之旅
    Visual studio 2015 Community 安装过程中遇到问题的终极解决
    Activiti6.0 spring5 工作流引擎 java SSM流程审批 项目框架
    java 进销存 库存管理 销售报表 商户管理 springmvc SSM crm 项目
    Leetcode名企之路
    24. 两两交换链表中的节点
    21. 合并两个有序链表
  • 原文地址:https://www.cnblogs.com/dreamzone/p/12485646.html
Copyright © 2011-2022 走看看