• springMVC环境搭建


    建立controller层

    UserController.java

    package com.imust.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/user")
    public class UserController {
        @RequestMapping("/findAll")
        public String findAll(){
            return "success";
        }
    }

    配置springMVC配置文件spring-mvc.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 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!--包扫描-->
        <context:component-scan base-package="com.imust.controller"></context:component-scan>
    
        <!--视图解析器|前端控制器-->
        <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!--前缀-->
            <property name="prefix" value="/jsp/"></property>
            <!--后缀-->
            <property name="suffix" value=".jsp"></property>
        <!--匹配前缀后缀-->
        </bean>
        <!--开启注解的支持-->
        <mvc:annotation-driven></mvc:annotation-driven>
    </beans>

    配置web.xml

    <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" metadata-complete="true">
      <!--配置视图解析器-->
      <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
      </servlet-mapping>
    
      <!--配置filter 处理乱码-->
      <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>
        <!--设置强制编码-->
        <init-param>
          <param-name>forceEncoding</param-name>
          <param-value>true</param-value>
        </init-param>
      </filter>
      <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    </web-app>

    编写两个测试jsp

    index.jsp

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <body>
    <h2>Hello World!</h2>
    <a href="${pageContext.request.contextPath}/user/findAll">测试SprngMVC环境</a>
    </body>
    </html>

    success.jsp

    <%--
      Created by IntelliJ IDEA.
      User: SCCQ
      Date: 2020/3/15
      Time: 19:12
      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>
        <h1>执行成功</h1>
    </body>
    </html>

    运行结果

  • 相关阅读:
    阿里云云效技术专家分享:云原生开发、调测及可靠发布解决方案
    对话李飞飞,揭秘国际体育赛事风“云”背后的黑科技
    时序数据库永远的难关 — 时间线膨胀(高基数 Cardinality)问题的解决方案
    当Java遇上机密计算,又一段奇幻之旅开始了!
    内核热补丁,真的安全么?
    在 Dubbo3.0 上服务治理的实践
    CCF-201509-3-生成模板系统
    WPF CommandParameter的使用
    UWP App Data存储和获取
    在WPF中的ItemsControl中使用事件和命令(Using events and Commands within ItemsControl in WPF)
  • 原文地址:https://www.cnblogs.com/SCCQ/p/12499491.html
Copyright © 2020-2023  润新知