黑马ssm学习笔记-SpringMVC-day01

三层架构

入门程序

springmvc_day01_01_start

1. 创建maven-webapp模块

解决创建maven项目过慢问题:
黑马ssm学习笔记-SpringMVC-day01_第1张图片

2. 添加java,recources文件夹

黑马ssm学习笔记-SpringMVC-day01_第2张图片
黑马ssm学习笔记-SpringMVC-day01_第3张图片
黑马ssm学习笔记-SpringMVC-day01_第4张图片

3. 修改编译版本1.8,spring版本号锁定

黑马ssm学习笔记-SpringMVC-day01_第5张图片

4. 导入依赖

<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>2.5version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>jsp-apiartifactId>
<version>2.0version>
<scope>providedscope>
dependency>

5. 前端控制器

在web.xml中配置DispatcherServlet

  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
  servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServletservlet-name>
    <url-pattern>/url-pattern>
  servlet-mapping>

6. 创建springmvc.xml

7. 配置服务器

黑马ssm学习笔记-SpringMVC-day01_第6张图片

8. 创建新的index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>入门程序</h3>
    <a href="">入门程序</a>

</body>
</html>

9. 创建控制器HelloController

黑马ssm学习笔记-SpringMVC-day01_第7张图片

package cn.itcast.controller;

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

// 控制器类
@Controller
public class HelloController {
     
    @RequestMapping(path = "/hello")
    public String sayHello(){
     
        System.out.println("Hello StringMVC");
        return null;
    }
}

10. 配置spring创建容器时要扫描的包


<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.itcast">context:component-scan>

11. 在web.xml的servlet配置中加上全局的初始化参数

  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <init-param>
      <param-name>contextConfigLocationparam-name>
      <param-value>classpath:springmvc.xmlparam-value>
    init-param>
    <load-on-startup>1load-on-startup>
  servlet>

12. 创建success.jsp

黑马ssm学习笔记-SpringMVC-day01_第8张图片

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>入门成功!!!</h3>
</body>
</html>

13. 视图解析器对象

    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    bean>

14. 配置spring开启注解mvc的支持

    
    <mvc:annotation-driven>mvc:annotation-driven>

入门案例流程分析

组件介绍

ResultMapping注解

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h3>入门程序</h3>
<%--    <a href="hello">入门程序</a>--%>
    <a href="user/testRequestMapping">RequestMapping</a>
</body>
</html>

HelloController.java

package cn.itcast.controller;

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

// 控制器类
@Controller
@RequestMapping(path = "/user")
public class HelloController {
     

    /**
     * 入门案例
     * @return
     */
    @RequestMapping(path = "/hello")
    public String sayHello() {
     
        System.out.println("Hello SpringMVC");
        return "success";
    }

    /**
     * RequestMapping注解
     * @return
     */
    @RequestMapping(path = "/testRequestMapping")
    public String testRequestMapping(){
     
        System.out.println("测试RequestMapping注解...");
        return "success";
    }
}

参数绑定

1. 创建parm.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <a href="param/testParam?username=hh&password=123">参数绑定</a>
</body>
</html>

2. ParamController.java

package cn.itcast.controller;

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

@Controller
@RequestMapping("/param")
public class ParamController {
     

    @RequestMapping("/testParam")
    public String testParam(String username, String password){
     
        System.out.println("测试参数绑定...");
        System.out.println("用户名:" + username);
        System.out.println("密码:" + password);
        return "success";
    }
}

传递JavaBean

1. cn.itcast.domain.Account.java实体类

package cn.itcast.domain;

public class Account {
     

    private String username;
    private String password;
    private Double money;

    public String getUsername() {
     
        return username;
    }

    public void setUsername(String username) {
     
        this.username = username;
    }

    public String getPassword() {
     
        return password;
    }

    public void setPassword(String password) {
     
        this.password = password;
    }

    public Double getMoney() {
     
        return money;
    }

    public void setMoney(Double money) {
     
        this.money = money;
    }

    @Override
    public String toString() {
     
        return "Account{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", money=" + money +
                '}';
    }
}

2. param.jsp中建立表单

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--    <a href="param/testParam?username=hh&password=123">参数绑定</a>--%>

    <form action="param/saveAccount" method="get">
        姓名:<input type="text" name="username" /><br/>
        密码:<input type="text" name="password" /><br/>
        金额:<input type="text" name="money" /><br/>
        <input type="submit" value="提交" />
    </form>

</body>
</html>

3. ParamController中写saveAccount()方法

package cn.itcast.controller;

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

@Controller
@RequestMapping("/param")
public class ParamController {
     

    @RequestMapping("/testParam")
    public String testParam(String username, String password){
     
        System.out.println("测试参数绑定...");
        System.out.println("用户名:" + username);
        System.out.println("密码:" + password);
        return "success";
    }

    @RequestMapping("/saveAccount")
    public String saveAccount(Account account) {
     
        System.out.println(account);
        return "success";
    }
}

Account包含User

1. User.java

2. param.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--    <a href="param/testParam?username=hh&password=123">参数绑定</a>--%>

    <form action="param/saveAccount" method="get">
        姓名:<input type="text" name="username" /><br/>
        密码:<input type="text" name="password" /><br/>
        金额:<input type="text" name="money" /><br/>
        用户姓名:<input type="text" name="user.uname" /><br/>
        年龄:<input type="text" name="user.age" /><br/>
        <input type="submit" value="提交" />
    </form>

</body>
</html>

post方法中文乱码的过滤器

web.xml配置解决中文乱码的过滤器

  
  <filter>
    <filter-name>characterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    
    <init-param>
      <param-name>encodingparam-name>
      <param-value>UTF-8param-value>
    init-param>
  filter>
  <filter-mapping>
    <filter-name>characterEncodingFilterfilter-name>
    <url-pattern>/*url-pattern>
  filter-mapping>

Account包含List,Map

param.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%--    <a href="param/testParam?username=hh&password=123">参数绑定</a>--%>

    <form action="param/saveAccount" method="post">
        姓名:<input type="text" name="username" /><br/>
        密码:<input type="text" name="password" /><br/>
        金额:<input type="text" name="money" /><br/>
        用户姓名:<input type="text" name="list[0].uname" /><br/>
        年龄:<input type="text" name="list[0].age" /><br/>
        用户姓名:<input type="text" name="map['one'].uname" /><br/>
        年龄:<input type="text" name="map['one'].age" /><br/>
        <input type="submit" value="提交" />
    </form>

</body>
</html>

自定义类型转换器

1. User中加上日期字段

2. 修改param.jsp

    <form action="param/saveUser" method="post">
        用户姓名:<input type="text" name="uname" /><br/>
        年龄:<input type="text" name="age" /><br/>
        生日:<input type="text" name="birthday" /><br/>
        <input type="submit" value="提交" />
    </form>

3. 增加saveUser()方法

4. cn.itcast.utils.StringToDateConverter.java

package cn.itcast.utils;

import org.springframework.core.convert.converter.Converter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDateConverter implements Converter<String, Date> {
     
    @Override
    public Date convert(String s) {
     
        if (s == null)
            throw new RuntimeException("请输入数据");
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
     
            return df.parse(s);
        } catch (ParseException e) {
     
            throw new RuntimeException("数据类型转换出现错误");
        }
    }
}

5. springmvc.xml配置自定义类型转换器

    
    <bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="cn.itcast.utils.StringToDateConverter" />
            set>
        property>
    bean>

    
    <mvc:annotation-driven conversion-service="conversionService"/>

你可能感兴趣的:(SSM)