springmvc注解开发
@RequestMapping定义请求规则
此注解就是来映射服务器访问的路径 此注解可加在方法上,是为此方法注册一个可以访问的名称(路径)
package com.peihj.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoAction2 {
@RequestMapping("demo.action")
public String demo(){
System.out.println("user服务器被访问到了.............");
return "main";
}
}
<a href="${pageContext.request.contextPath}/demo.action">访问服务器</a>
此注解可以加在类上,相当于是包名(虚拟路径),区分不同类中相同的action的名称。
package com.peihj.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("user")
public class DemoAction2 {
@RequestMapping("demo.action")
public String demo(){
System.out.println("user服务器被访问到了.............");
return "main";
}
}
<a href="${pageContext.request.contextPath}/user/demo.action">访问服务器</a>
此注解可区分get请求和post请求
@Controller
public class ReqAction {
@RequestMapping(value = "/req",method = RequestMethod.GET)
public String req(){
System.out.println("我是处理get请求的........");
return "main";
}
@RequestMapping(value = "/req" ,method = RequestMethod.POST)
public String req1(){
System.out.println("我是处理post请求的........");
return "main";
}
}
五种数据提交方式的优化
我们首先建立一个工程文件,具体流程见:https://blog.csdn.net/weixin_45522528/article/details/124672914 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>com.peihj</groupId>
<artifactId>springmvc-002-datasubmit</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--添加Spring-MVC依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!--添加servlet依赖-->
<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>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>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml 文件都会被扫描到-->
<include>***.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>***.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>7070</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
Springmvc编写
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--添加包扫描-->
<context:component-scan base-package="com.peihj"></context:component-scan>
<!--添加视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/admin/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
创建admin包文件以及main.jsp文件夹,删除重新创建index.jsp文件,删除并重新编写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">
<!--注册springmvc框架-->
<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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
五种数据注入
<%--
Created by IntelliJ IDEA.
User: peihj
Date: 2022/5/9
Time: 14:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<br><br><br>
<h2>测试不同的请求提交方式</h2>
<form action="${pageContext.request.contextPath}/req.action" method="post">
<input type="submit" value="提交">
</form><br><br><br>
<h2>1.单个数据的提交</h2>
<form action="${pageContext.request.contextPath}/one.action" method="post">
姓名:<input name="myname"><br>
年龄:<input name="age"><br>
<input type="submit" value="提交">
</form>
<%-- private String name;
private int age;
保证名称一致以便于自动注入
--%>
<h2>2.对象封装提交</h2>
<form action="${pageContext.request.contextPath}/two.action" method="post">
姓名:<input name="name"><br>
年龄:<input name="age"><br>
<input type="submit" value="提交">
</form>
<br>
<h2>3.动态占位符提交</h2>
<a href="${pageContext.request.contextPath}/three/张三/22.action">动态提交</a>
<br><br><br>
<h2> 4.参数名称不一致解决方案></h2>
<form action="${pageContext.request.contextPath}/four.action" method="post">
姓名:<input name="name"><br>
年龄:<input name="age"><br>
<input type="submit" value="提交">
</form>
<h2> 5.手工提取数据></h2>
<form action="${pageContext.request.contextPath}/five.action" method="post">
姓名:<input name="name"><br>
年龄:<input name="age"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
package com.peihj.controller;
import com.peihj.pojo.Users;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
@Controller
public class DataSubmitAction {
@RequestMapping("/one.action")
public String one(String myname,int age){
System.out.println("myname="+myname+",age="+(age+0));
return "main";
}
@RequestMapping("/two.action")
public String two(Users u){
System.out.println(u);
return "main";
}
@RequestMapping("/three/{uname}/{uage}.action")
public String three(@PathVariable("uname") String name, @PathVariable("uage") int age){
System.out.println("myname="+name+",age="+(age+0));
return "main";
}
@RequestMapping("four.action")
public String four(@RequestParam("name") String uname,@RequestParam("age") int uage){
System.out.println("myname="+uname+",age="+(uage+0));
return "main";
}
@RequestMapping("five.action")
public String five(HttpServletRequest httpServletRequest){
String name = httpServletRequest.getParameter("name");
Integer age = Integer.parseInt(httpServletRequest.getParameter("age"));
System.out.println(name);
System.out.println(age);
return "main";
}
}
单个提交数据
在方法中声明一个和表单提交的参数名称相同的参数,由框架按照名称直接注入,只要表单name属性的值与action方法参数的名称一致就可以实现自动注入
对象封装注入
在提交请求中,保证请求参数的名称与实体类中成员变量的名称一致,则可以自动创建对象,则可以自动提交数据,自动类型转换,自动封装数据到对象中.
首先我们先创建对象类
package com.peihj.pojo;
public class Users {
private String name;
private int age;
public Users() {
}
public Users(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Users{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
动态占位符提交(仅用于超链接)
仅限于超链接或地址拦提交数据.它是一杠一值,一杠一大括号,使用注解@PathVariable来解析.
请求参数名称与形参名称不一致
请求与形参中的名字不对应,可以使用 @RequestParam(value=“name1”,required=true)String namea来进行参数名称绑定
手工提取数据
中文乱码解决方案
配置过滤器
过滤器代码
<!--配置中文编码过滤器的配置-->
<!--配置字符集编码过滤器-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--设置字符集编码-->
<!--配置参数
private String encoding;
private boolean forceRequestEncoding;
private boolean forceResponseEncoding;
-->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>
完整
<?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>
<!--设置字符集编码-->
<!--配置参数
private String encoding;
private boolean forceRequestEncoding;
private boolean forceResponseEncoding;
-->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>
参考
https://www.bilibili.com/video/BV1oP4y1K7QT?p=25&t=96.0
|