当前位置: 首页 > article >正文

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分)。

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

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

1、pom.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核心配置文件

<?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中配置:

<?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下才可以直接访问


<%@ 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实体

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

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、编写自定义类型转换器:

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中配置自定义类型转换器:

<?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:


<%@ 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 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、此时再次运行项目:能够正常显示中文字符


http://www.kler.cn/a/420693.html

相关文章:

  • Cannot resolve symbol ‘ActivityThread‘ | Android 语法
  • leetcode--螺旋矩阵
  • MATLAB —— 机械臂工作空间,可达性分析
  • 如何在 Firefox 中清除特定网站的浏览历史记录
  • 大数据-240 离线数仓 - 广告业务 测试 ADS层数据加载 DataX数据导出到 MySQL
  • Spring AI 框架介绍
  • 人工智能-深度学习-BP算法
  • 【计算机网络】实验3:集线器和交换器的区别及交换器的自学习算法
  • mysql之慢查询设置及日志分析
  • Paper -- 建筑物高度估计 -- 使用街景图像、深度学习、轮廓处理和地理空间数据的建筑高度估计
  • React.memo 和useMemo 的区别
  • Python 调用 Umi-OCR API 批量识别图片/PDF文档数据
  • 【前端】小程序实现预览pdf并导出
  • Argon2-cffi:Python中的密码学哈希库
  • AI 计算基础设施的战略转折点分析
  • C++ 变量和常量:开启程序构建之门的关键锁钥与永恒灯塔
  • Go-MediatR:Go语言中的中介者模式
  • 基于 Vite 封装工具库实践
  • ABE 中的隐藏属性:DIPPE(去中心化内积谓词加密)
  • linux 压缩命令,压缩a目录,但是不压缩a目录下的b目录,zip命令
  • termius mac版无需登录注册直接永久使用
  • Mybatis 复习
  • MySQL 数据库学习教程二:深入 MySQL 数据库世界
  • uniapp进阶技巧:如何优雅地封装request实例
  • 《嵌入式硬件设计》
  • 架构学习第五周--Kubernetes(一)