SSM框架整合(3)—— SpringMVC框架

SpringMVC框架

结构目录

  • java
    • controller
      • AccountController.java(表现层)
  • resources
    • applicationContext.xml(Spring配置文件)
    • springmvc.xml(SpringMVC配置文件)
  • webapp
    • WEB-INF
      • pages
        • list.jsp(jsp页面文件)
      • web.xml(web配置文件)
    • index.jsp(jsp页面文件)

web配置文件

  • 前端控制器
    • 配置 前端控制器(DispatcherServlet)
    • 配置 创建前端控制器时,加载配置文件(contextConfigLocation)
    • 配置 服务器启动时,立即创建前端控制器(1)
    • 配置 映射/作用范围(/)
  • 过滤器
    • 配置 过滤器(CharacterEncodingFilter)
    • 配置 编码(UTF-8)
    • 配置 映射/作用范围(/*)
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>
  <display-name>Archetype Created Web Application</display-name>

  <!-- 前端控制器 -->
  <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:springmvc.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-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>

SpringMVC配置文件

  • 开启 注解扫描(context:component-scan)
    • 只扫描表现层注解
  • 配置 视图解析器(InternalResourceViewResolver)
  • 配置 前端控制器(mvc:resource)
  • 开启 SpringMVC注解支持(mvc:annotation-driven)
springmvc.xml
<?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">

    <!-- 开启 注解扫描 -->
    <context:component-scan base-package="cn.water.controller"/>

    <!-- 配置 视图解析器 -->
    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!-- 配置 前端控制器 -->
    <mvc:resources mapping="/js/**" location="/js/"></mvc:resources>
    <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>
    <mvc:resources mapping="/css/**" location="/css/"></mvc:resources>

    <!-- 开启 SpringMVC注解支持 -->
    <mvc:annotation-driven/>

</beans>

表现层

AccountController.java
  • 返回:list
package cn.water.controller;

import cn.water.domain.Account;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("accountController")
public class AccountController {

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

}

jsp页面

index.jsp
  • 执行表现层的方法,获得返回值“/WEB-INF/pages/list.jsp”,跳转页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3><a href="accountController/findAll">findAll</a></h3>
</body>
</html>

jsp页面

  • 展示
list.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>用户信息列表</h1>
</body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值