1.实现目标
将前端页面输出的内容存储到实体类中,在控制台打印输出
2.实现思路
前端页面根据首页输入框输入相关内容——>读取web.xml配置文件——>同时根据web.xml中的配置,读取到spring-mvc.xml配置文件——>根据MVC.xml进入前端控制器controller层,执行命令,根据controller中的return 跳转到新的页面(因为mvc.中配置试图解析器,配置了前置和后置名称,此处return给浏览器新的地址)
目录概览:
代码部分
1.pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.cfg</groupId>
<artifactId>springMVC_day02_value3</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springMVC_day02_value3 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
<build>
<finalName>springMVC_day02_value3</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
2.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="cn.cfg"></context:component-scan>
<!--试图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前置名称-->
<property name="prefix" value="/WEB-INF/"></property>
<!--后置名称-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--mvc注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
3.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>
<!--编码过滤器-->
<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>
<!--
过滤器会判断request.getCharacterEncoding()是否为null,
如果是null那么就会进行 request.setCharacterEncoding("UTF-8")的操作,
如果不是null那么过滤器什么也不会做
所以下面用true
-->
<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>
<!--前端控制器-->
<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>
</web-app>
4.实体类
package cn.cfg.pojo;
import java.util.List;
public class User {
private Integer id;
private String name;
private Character sex;
private Integer age;
private List<String> list;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Character getSex() {
return sex;
}
public void setSex(Character sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
", list=" + list +
'}';
}
}
5.controller前端控制器:
package cn.cfg.controller;
import cn.cfg.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "/test")
public class UserController {
@RequestMapping(value = "/hello")
public String test1(String name,Integer age,Character sex){
System.out.println(name);
System.out.println(age);
System.out.println(sex);
return "show";
}
@RequestMapping(value = "/hello1")
public String test2(User user){
System.out.println(user);
return "show";
}
}
6.准发的show页面:
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/8/16
Time: 10:00
To change this template use File | Settings | File Templates.
--%>
<%@ page isELIgnored="false" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>从前慢</h1>
<p>记得早前少年时</p>
<p>大家诚诚恳恳</p>
<p>清早上 火车站</p>
<p>长街黑暗无行人</p>
<p>卖豆浆的小店冒着热气</p>
</body>
</html>
7.首页jsp
<%@page isELIgnored="false" contentType="text/html;charset=utf-8" language="java" %>
<html>
<body>
<%--action 相当于a标签的href 后面写跳转路径 method 提交方式--%>
<form action="${pageContext.request.contextPath}/test/hello1" method="post">
姓名:<input type="text" name="name">
年龄:<input type="text" name="ege">
性别:<input type="text" name="sex">
list集合1:<input type="text" name="list[0]">
list集合2:<input type="text" name="list[1]">
<input type="submit">
</form>
</body>
</html>
效果图:
首页:
跳转show:
控制台: