Spring MVC接收前台信息,并在页面返回

目录

题目:

1、pom.xml中配置依赖:

2、在resources根目录下创建springmvc-config.xml核心配置文件

3、设置webapp目录结构:

4、在web.xml中配置:

5、创建login.jsp页面,要在webapp下才可以直接访问

6、创建Signon实体

7、编写控制层SignonController.java

8、编写自定义类型转换器:

9、在springmvc-config.xml中配置自定义类型转换器:

10、在指定路径下创建welcome.jsp:

11、配置Tomcat,运行项目:

12、提交后,会跳转到welcome.jsp页面,此时中文字符出现乱码问题

13、为解决乱码问题,在web.xml中配置字符过滤器:

14、此时再次运行项目:能够正常显示中文字符


题目:

已知前台界面和程序如下:

<form action="login6" method="post">

用户信息(用户信息包括用户名和密码):<br/>

录入时用户名和密码之间以英文逗号隔开<br/><input type="text" name="signon" size="30"/><br/>

<br/>

<input type="submit" value="提交"/><input type="reset" value="重置"/>

</form>

另外已知用户信息类Signon的代码如下:

public class Signon {

String uname;

String pwd;

//缺省set/get方法

}

请利用Spring MVC框架完成如下操作:

  1. 编写Controller,接收前台发送的用户信息,要求处理函数的参数类型为Signon类,并将接收到的用户信息在欢迎界面welcome.jsp中进行显示(5分)。

2)编写自定义类型转换器,实现前台用户信息和后台处理函数参数之间的数据绑定(7分)。

  1. 配置Spring MVC的核心配置文件springmvc-config.xml,重点配置注解扫描包和视图解析器(5分)。

4)编写welcome.jsp页面,实现用户信息的显示(3分)。

1、pom.xml中配置依赖:

XML 复制代码
<?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>com.haust</groupId>
    <artifactId>test34_1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <!--    mybatis-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.28</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.11</version>
        </dependency>
        <!--    数据源-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>
        <!--    spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <!--    springmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--    spring和mybatis整合-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.7</version>
        </dependency>
        <!--    单元测试-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!--    jstl-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8080</port>
                    <path>/chapter34_1</path>
                    <uriEncoding>utf-8</uriEncoding>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

2、在resources根目录下创建springmvc-config.xml核心配置文件

java 复制代码
<?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:tx="http://www.springframework.org/schema/tx" 
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.haust.controller"/>

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

3、设置webapp目录结构:

4、在web.xml中配置:

java 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    <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-config.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>

5、创建login.jsp页面,要在webapp下才可以直接访问

java 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <form action="login6" method="post">
        用户信息(用户信息包括用户名和密码):<br/>
        录入时用户名和密码之间以英文逗号隔开<br/>
        <input type="text" name="signon" size="30"/><br/>
        <br/>
        <input type="submit" value="提交"/>
        <input type="reset" value="重置"/>
    </form>
</body>
</html>

6、创建Signon实体

java 复制代码
package com.haust.pojo;

public class Signon {
    private String uname;
    private String pwd;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
}

7、编写控制层SignonController.java

java 复制代码
package com.haust.controller;

import com.haust.pojo.Signon;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class SignonController {
    @PostMapping("/login6")
    public String sendRequest(@ModelAttribute("signon") Signon signon, Model model){
        model.addAttribute("username",signon.getUname());
        model.addAttribute("password",signon.getPwd());
        return "welcome";
    }
}

8、编写自定义类型转换器:

java 复制代码
package com.haust.converter;

import com.haust.pojo.Signon;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

@Component
public class SignonConverter implements Converter<String, Signon> {
    public Signon convert(String s) {
        String[] split = s.split(",");
        Signon signon = new Signon();
        signon.setUname(split[0]);
        signon.setPwd(split[1]);
        return signon;
    }
}

9、在springmvc-config.xml中配置自定义类型转换器:

java 复制代码
<?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:tx="http://www.springframework.org/schema/tx" 
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.haust.controller"/>

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

    <bean id="conversionService2" class="org.springframework.context.support.ConversionServiceFactoryBean">
        <property name="converters">
            <bean class="com.haust.converter.SignonConverter"/>
        </property>
    </bean>

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

10、在指定路径下创建welcome.jsp:

XML 复制代码
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>welcome.jsp页面</h1>
<h1>用户名:${username}</h1>
<h1>密码:${password}</h1>
</body>
</html>

11、配置Tomcat,运行项目:

12、提交后,会跳转到welcome.jsp页面,此时中文字符出现乱码问题

13、为解决乱码问题,在web.xml中配置字符过滤器:

XML 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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_4_0.xsd"
         version="4.0">
    <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-config.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>

14、此时再次运行项目:能够正常显示中文字符

相关推荐
LeonNo111 小时前
k8s, deployment
java·容器·kubernetes
娶个名字趴2 小时前
事务的传播机制
java
学习中的问题2 小时前
同步数据至ES时,数据丢失问题处理
java·elasticsearch
☼YJLH☾3 小时前
第二章,SQL映射文件
java·sql·tomcat
八月五3 小时前
Tomcat项目本地部署
java·tomcat
MuYan~3 小时前
kafka java 小记录
java·分布式·kafka
customer084 小时前
【开源免费】基于SpringBoot+Vue.JS购物推荐网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
Dong雨4 小时前
ShardingSphere-JDBC
java·开发语言·数据库
快乐就好ya5 小时前
ShardingSphere 数据库中间件
java·数据库·spring boot·spring cloud·中间件
m0_748238635 小时前
【SQL实验】高级查询(二)
java