Spring 项目中,后端获取前端数据后,会出现得到的数据为?????
这里用了一个简单的例子说明用法,并且加了注释,方便初学者阅读。
项目结构:
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: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>
<!--编码过滤器,前段数据为String类型,后端获取后为?????,
也应当在web.xml文件中配置编码过滤器-->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
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"
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">
<!-- more bean definitions go here -->
<!--开启注解扫描 -->
<context:component-scan base-package="com.spring"/>
<bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
<property name="characterEncoding" value="UTF-8"/>
<property name="templateEngine">
<bean class="org.thymeleaf.spring5.SpringTemplateEngine">
<property name="templateResolver">
<bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
<property name="characterEncoding" value="UTF-8"/>
<property name="prefix" value="WEB-INF/hello/"/>
<property name="templateMode" value="HTML"/>
<property name="suffix" value=".html"/>
</bean>
</property>
</bean>
</property>
</bean>
</beans>
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello</h1>
<a th:href="@{/test1/login}">login</a>
</body>
</html>
login.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>login.html</h1>
<!--form表单的action路径用thymeleaf渲染 -->
<form th:action="@{/test1/login}" method="post">
<input type="text" name="name">
<input type="submit">
</form>
你输入的是:
<p th:text="${model}"> model</p>
<!-- 这里使用了thymeleaf渲染技术-->
</body>
</html>
控制器
test.java
package com.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//用来打开主页面的控制器
@Controller
public class test {
@RequestMapping("/")
public String t(){
return "index";
}
}
test1.java
package com.spring;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
//用来打开子页面的控制器
@Controller
@RequestMapping("/test1")
public class test1 {
@RequestMapping("/login")
public String test2(HttpServletRequest request,String name){
/*
方法参数的参数名称必须与html的<input name=""/>的name的内容相同,
否则不能识别,不能接收前端数据
*/
System.out.println(name);
//这里只做简单输出,不涉及数据库操作
request.setAttribute("model",name);
return "login";
}
}