• SpringMVC--->form简单跳转小实例,中文乱码解决


    预览项目结构

    导包

      <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>servlet-api</artifactId>
                <version>2.5</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>2.2</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.2.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.20</version>
            </dependency>
        </dependencies>
        <build>
            <resources>
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <includes>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>

    1.regist.jsp

    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
     <!-- 首先我们需要在浏览器中访问到这个jsp页面,填写内容后提交表单,浏览器就会收到 /user/param01.do 这个请求,然后转交给tomcat -->
    <form method="post" action="/pa">
        姓名:<input type="text" name="username">
        <br>
        年龄:<input type="text" name="age">
        <br>
        <input type="submit" value="提交">
    </form>
    </body>
    </html>

     2.result.jsp

    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
    姓名:${username}
    <br>
    年龄:${age}
    </body>
    </html>

    3.web.xml(Spring实现乱码控制)

    <?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">
        <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    
            <!--手动指定字符编码-->
            <init-param>
                <!--因为当前filter中的成员变量叫做encoding,所以此处必须也写成encoding-->
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
    
            <!--强制指定字符编码,即使在request或者response中设置了字符编码,那么也会为其强制使用当前设置的字符编码-->
            <init-param>
                <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>springmvc</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>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    </web-app>

    4.SpringMVC.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:p="http://www.springframework.org/schema/p"
           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">
    
        <mvc:annotation-driven/>
    
        <context:component-scan base-package="com.xian.*"/>
    
        <bean
                class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>

    5.Controller

    package com.xian.controller;
    
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @org.springframework.stereotype.Controller
    public class Controller {
    
        @RequestMapping("/pa")
        public ModelAndView getParams01(String username,int age)throws Exception{
            //方法的参数需要和提交表单里面的参数一致
            ModelAndView mv = new ModelAndView();
            mv.addObject("username",username);
            mv.addObject("age",age);
            mv.setViewName("result");
            return mv;
    
        }
    }

    访问地址:http://localhost:8080/jsp/regist.jsp

  • 相关阅读:
    MSI文件的制作
    QPushButton 响应回车 设置默认按钮
    ubuntu 使用apt-fast 安装软件包
    apt命令集使用教程
    如何在windows下使用git及github仓库管理项目
    如何在windows下使用git及github仓库管理项目
    01. struts2介绍
    人生能跨越多少个年代
    联调踩的坑:空和空字符串引发的巨大差异
    我的亲历:一行代码,百万人民币打水漂
  • 原文地址:https://www.cnblogs.com/springxian/p/13734739.html
Copyright © 2020-2023  润新知