• ssm整合-编写SpringMVC框架03


    搭建springmvc的环境:

    在web.xml中配置前端控制器:(就是一个servlet)

     servlet的创建一般是访问的时候,配置<load-on-startup>1</load-on-startup>(为1)就是在服务器启动时就创建servlet

    在servlet初始化的时候加载springmvc的配置文件

    <!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>
    <display-name>Archetype Created Web Application</display-name>

    <!--配置前端控制器-->
    <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--加载springmvc.xml配置文件(springmvc的核心配置文件)-->
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!--启动服务器,创建该servlet-->
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--解决中文乱码的过滤器-->
    <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>
    </web-app>
    过滤器的配置是通过这个类CharacterEncodingFilter,拦截的是所有的,编码集是utf-8
    创建springmvc的配置文件:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <!--开启注解扫描,只扫描Controller这个注解-->
    <context:component-scan base-package="cn.itcast">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
    </context:component-scan>
    <!--配置视图解析器对象-->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
    </bean>
    <!--过滤静态资源,映射路径下的不拦截,本地资源不拦截-->
    <mvc:resources location="/css/" mapping="/css/**"/>
    <mvc:resources location="/images/" mapping="/images/**"/>
    <mvc:resources location="/js/" mapping="/js/**"/>

    <!--开启springMvc注解的支持-->
    <mvc:annotation-driven/>
    </beans>
    创建一个测试的index:

     

    <%--
    Created by IntelliJ IDEA.
    User: kc
    Date: 2020/6/1
    Time: 21:01
    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>
    <a href="account/findAll">测试</a>
    </body>
    </html>
    编写controller:

     



    package cn.itcast.controller;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    /*
    * 账户web
    * */
    @Controller
    @RequestMapping("/account")
    public class AccountController {

    @RequestMapping("/findAll")
    public String findAll() {
    System.out.println("表现层:查询所有账户...");
    return "list";
    }
    }

    编写返回页面:

     

  • 相关阅读:
    读取web.xml中设置的参数
    在服务端中,读取properties资源文件中的数据
    window下,nodejs安装http-server,并开启HTTP服务器
    跨域请求资源的方式
    IView 给Submenu增加click事件失效解决方案
    Vue -- mounted方法中调用methods的方法(并取出mounted方法中回调函数的值)
    IView 使用Table组件时实现给某一列添加click事件
    物联网协议CoAP协议学习
    电脑操作
    物联网协议
  • 原文地址:https://www.cnblogs.com/kingchen/p/13027552.html
Copyright © 2020-2023  润新知