SpringMVC快速学习

news2024/11/25 7:54:19

一、SSM框架优化的方向

image-20230607195031180 image-20230607195833541

目录结构:

image-20230607200249644

二、前期文件配置

pom.xml

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.itheima</groupId>
  <artifactId>springmvc_001_demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
	<dependencies>
		<!--springmvc依赖-->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>5.2.5.RELEASE</version>
		</dependency>
		<!--servlet依赖-->
		<dependency>
		    <groupId>javax.servlet</groupId>
		    <artifactId>javax.servlet-api</artifactId>
		    <version>3.1.0</version>
		    <scope>provided</scope>
		</dependency>
	</dependencies>
	
	<build>
    	<finalName>maven.plugins</finalName>
    	<pluginManagement>
    	<plugins>
    		<plugin>
        		<groupId>org.apache.maven.plugins</groupId>
        		<artifactId>maven-war-plugin</artifactId>
        		<version>3.3.1</version>
      		</plugin>
    	</plugins>    
    	</pluginManagement>    
    	
    	<resources>
			<resource>
				<directory>${basedir}/src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
			<resource>
				<directory>${basedir}/src/main/resources</directory>
				<includes>
					<include>**/*.xml</include>
					<include>**/*.properties</include>
				</includes>
			</resource>
		</resources> 
	</build>
</project>

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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
  
  
  <context:component-scan base-package="com.itheima.controller" />

 
    <!--配置spring创建容器时要扫描的包-->
    <context:component-scan base-package="com.itheima.controller"></context:component-scan>
 
<!--     处理映射器
    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
    处理器适配器
    <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
 -->
 
    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<!-- 配置前缀 -->
        <property name="prefix" value="/WEB-INF/admin/"/>
        <!-- 配置后缀 -->
        <property name="suffix" value=".jsp"/>
    </bean>
	
    <!--配置spring开启注解mvc的支持  默认就是开启的 ,要想让其他组件(不包含映射器、适配器、处理器)生效就必须需要配置了-->

</beans>

(很重要)为什么要注册DispatcherServlet

image-20230607204053006 image-20230607204540681

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>

三、项目发布

image-20230607212308031 image-20230607212358199 image-20230607212440163

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%--${pageContext.request.contextPath}的意思是/项目名称,比如这个项目:/springmvc_001_demo,这个/表示http://localhost:8080/--%>
	<a href="${pageContext.request.contextPath}/demo.action">访问服务器</a>
	<a>${pageContext.request.contextPath}</a>
</body>
</html>

main.jsp

image-20230607213639014

controller1.java

package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class controller1 {
	@RequestMapping("/demo")
	public String demo() {
		System.out.println("服务器访问到了");
		return "main";
	}
	
}

这里加上springmvc之后image-20230607213808190

image-20230607213842863

跳转之后:

image-20230607213856313

四、springmvc项目搭建流程再来一遍

创建javaweb工程

image-20230607223310144 image-20230607223328189

或者建Maven项目也可以,总之,下面这个目录结构不能错

image-20230607223626153

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.itheima</groupId>
  <artifactId>springmvc_002_datasubmit</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>springmvc_002_datasubmit</name>
  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
      <junit.version>5.9.1</junit.version>
      </properties>

  <dependencies>
      <!--servlet依赖-->
      <dependency>
          <groupId>javax.servlet</groupId>
          <artifactId>javax.servlet-api</artifactId>
          <version>3.1.0</version>
          <scope>provided</scope>
      </dependency>
      <!--springmvc依赖-->
      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-webmvc</artifactId>
          <version>5.2.5.RELEASE</version>
      </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>      </dependencies>

    <build>
        <finalName>maven.plugins</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.3.1</version>
                </plugin>
            </plugins>
        </pluginManagement>

        <resources>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

resources/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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

	<!--包扫描-->
    <context:component-scan base-package="com.itheima.controller"></context:component-scan>
    <!--视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/admin/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

webapp/WEB-INF/web.xml 注册springmvc框架

<?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><!--根据请求的路由,查询是否有处理请求的方法(根据@RequestMapping)-->
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <!--springmvc注册-->
    
</web-app>
image-20230607224518491

五、数据提交方式

1、form表单提交

DataController.java

package com.itheima.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Description:
 * date: 2023/6/8 21:40
 *  参数名与jsp传入的变量名要一致
 * @since JDK 11
 */
@Controller
public class DataController {
	@RequestMapping(value = "/one")
	public String dataSubmit(String name, String pw){
		System.out.printf(name+" "+pw);
		return "login";
	}
}

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
  <title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %></h1>
<br/>
<form action="${pageContext.request.contextPath}/one.action">
  name:<input type="text" name="name"><br/>
  password:<input type="password" name="pw"><br/>
  <input type="submit" value="提交">
</form>
</body>
</html>

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: xiaoshiguang
  Date: 2023/6/7
  Time: 22:25
  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>
    <h2>hello</h2>
</body>
</html>

2、对象封装提交数据

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
  <title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %></h1>
<br/>
<form action="${pageContext.request.contextPath}/one.action">
  name:<input type="text" name="name"><br/>
  password:<input type="password" name="pw"><br/>
  <input type="submit" value="提交">
</form>
<h3>封装成实体类进行数据的提交</h3>
<%--要求name的属性值与实体类对象的成员变量的值相同--%>
<form action="${pageContext.request.contextPath}/two.action" method="get">
  name:<input type="text" name="name"><br/>
  age:<input type="text" name="age"><br/>
  <input type="submit" value="提交">
</form>
</body>
</html>
image-20230608220810061

必须一致

实体类:User.java,需要有get和set方法

package com.itheima.domain;

/**
 * Description:
 * date: 2023/6/8 22:02
 *
 * @since JDK 11
 */
public class User {
	private String name;
	private int age;

	public User() {
	}

	public User(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;
	}
}

DataController.java

package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Description:
 * date: 2023/6/8 21:40
 *  参数名与jsp传入的变量名要一致
 * @since JDK 11
 */
@Controller
public class DataController {
	@RequestMapping(value = "/one")
	public String dataSubmit(String name, String pw){
		System.out.printf(name+" "+pw);
		return "login";
	}

	@RequestMapping(value = "/two")
	public String dataSubmit2(User user){
		System.out.printf(user.getName()+","+user.getAge());
		return "login";
	}
}

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: xiaoshiguang
  Date: 2023/6/7
  Time: 22:25
  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>
    <h2>hello</h2>
</body>
</html>

3、动态占位符提交

1/1值,1/1{}

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
  <title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %></h1>
  <h3>3、动态占位符提交</h3>
  <%--1个/1个值,1/一个{}--%>
  <a href="${pageContext.request.contextPath}/three/张三/18.action">1/1值</a>
</body>
</html>

DataController.java

package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Description:
 * date: 2023/6/8 21:40
 *  参数名与jsp传入的变量名要一致
 * @since JDK 11
 */
@Controller
public class DataController {
	@RequestMapping(value = "/one")
	public String dataSubmit(String name, String pw){
		System.out.printf(name+" "+pw);
		return "login";
	}

	@RequestMapping(value = "/two")
	public String dataSubmit2(User user){
		System.out.printf(user.getName()+","+user.getAge());
		return "login";
	}

    //3、动态占位符,1/1值,1/1{}
	@RequestMapping(value = "/three/{name}/{age}")
	public String dataSubmit3(@PathVariable String name, @PathVariable Integer age){
		System.out.printf(name +","+age);
		return "login";
	}
	@RequestMapping(value = "/three/{name2}/{age2}")
	public String dataSubmit4(@PathVariable("name2") String name, @PathVariable("age2") Integer age){
		System.out.printf(name +","+age);
		return "login";
	}
}
image-20230608223555020

4、参数名称不一致

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
  <title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %></h1>
  <h3>4、参数名称不一致解决方案</h3>
  <%--1个/1个值,1/一个{}--%>
  <form action="${pageContext.request.contextPath}/four.action">
    name:<input type="text" name="name"><br/>
    age:<input type="text" name="age"><br/>
    <input type="submit" value="提交">
  </form>
</body>
</html>

controller.java

	@RequestMapping(value = "/four")
	public String dataSubmit4(@RequestParam("name") String name4,@RequestParam("age") Integer age4){
		System.out.printf(name4 +","+age4);
		return "login";
	}

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: xiaoshiguang
  Date: 2023/6/7
  Time: 22:25
  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>
    <h2>hello</h2>
</body>
</html>

5、原始方法,手动提取

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
  <title>JSP - Hello World</title>
</head>
<body>
<h1><%= "Hello World!" %></h1>
<br/>
<h3>5、原始方法</h3>
<form action="${pageContext.request.contextPath}/five.action">
  name:<input type="text" name="name"><br/>
  age:<input type="text" name="age"><br/>
  <input type="submit" value="提交">
</form>
</body>
</html>

DataController.java

package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
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 DataController {
	@RequestMapping("/five")
	public String dataSubmit5(HttpServletRequest httpServletRequest){
		String name = httpServletRequest.getParameter("name");
		Integer age =  Integer.parseInt(httpServletRequest.getParameter("age"));
		System.out.printf("name"+","+age);
		return "login";
	}
}

login.jsp

<%--
  Created by IntelliJ IDEA.
  User: xiaoshiguang
  Date: 2023/6/7
  Time: 22:25
  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>
    <h2>hello</h2>
</body>
</html>

六、中文编码设置

在发送post请求时,输入汉字,会发生乱码

配置过滤器

中文编码过滤器靠前放

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">

    <!--中文乱码解决配置-->
    <filter>
        <filter-name>encode</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!-- 源码中3个成员变量 ,默认false不编码
        	private String encoding;
            private boolean forceRequestEncoding = false;
            private boolean forceResponseEncoding = false;
        -->
        <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>encode</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--中文乱码解决配置-->

    <!--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><!--根据请求的路由,查询是否有处理请求的方法(根据@RequestMapping)-->
        <servlet-name>springmvc</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
    <!--springmvc注册-->

</web-app>

七、处理器controller返回值

1、String

客户端资源的地址,自动拼接前缀和后缀,还可以屏蔽自动拼接字符串,可以指定返回的路径

2、Object

返回json格式的对象,自动将对象或集合转为json。需要用到jackson工具进行转换,必须要添加jackson依赖,一般用于Ajax请求。

3、void

无返回值,一般用于ajax请求。

4、基本数据类型,用于ajax请求

5、ModelAndView

返回数据和视图对象,现在很少用了。

八、ajax请求

1、流程

完成ajax请求,访问服务器,返回学生集合。

​ 1、添加jackson依赖

​ 2、在webapp目录下新建js目录,添加jQuery函数库

​ 3、在index.jsp页面上导入函数库

​ 4、在action上添加注解@ResponseBody,用来处理ajax请求

​ 5、在springmvc.xml文件中添加注解驱动<mvc:annotationdriven>,它用来解析@ResponseBody

2、配置

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.itheima</groupId>
  <artifactId>springmvc_003_ajax</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>springmvc_003_ajax</name>
  <packaging>war</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
      <junit.version>5.9.1</junit.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>

    <!--servlet依赖-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <!--springmvc依赖-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.5.RELEASE</version>
    </dependency>
    <!--jackson,json转换工具-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.8</version>
    </dependency>
  </dependencies>

    <!--别忘了build-->
<build>
  <resources>
    <resource>
      <directory>${basedir}/src/main/java</directory>
      <includes>
        <include>**/*.xml</include>
        <include>**/*.properties</include>
      </includes>
    </resource>
    <resource>
      <directory>${basedir}/src/main/resources</directory>
      <includes>
        <include>**/*.xml</include>
        <include>**/*.properties</include>
      </includes>
    </resource>
  </resources>
</build>
</project>

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: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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--包扫描-->
    <context:component-scan base-package="com.itheima.controller"></context:component-scan>
    <!--不需要视图解析器,因为处理的是ajax请求-->
<!--    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/ajax/"/>
        <property name="suffix" value=".jsp"/>
    </bean>-->
    <!--必须要添加注解驱动,使@RespondBody生效-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

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">

    <!--添加中文编码解决方案-->
    <filter>
        <filter-name>encode</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--
        	private String encoding;
            private boolean forceRequestEncoding = false;
            private boolean forceResponseEncoding = false;
        -->
        <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>encode</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--添加中文编码解决方案-->

    <!--注册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>
    <!--注册springmvc框架-->
</web-app>

九、ajax请求返回json数组功能实现

image-20230609001432600

目录结构,导入jQuery文件


前面有许多内容没有记笔记,是在company的电脑上完成的。

P45登陆业务实现

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.powerpoint</groupId>
    <artifactId>springmvc_005_webinf</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>springmvc_005_webinf</name>
    <packaging>war</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.9.1</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!--配置springmvc-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
    </dependencies>


    <!--配置资源文件-->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
        </plugins>
    </build>
</project>

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: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 https://www.springframework.org/schema/context/spring-context.xsd">

<!--添加包扫描-->
<context:component-scan base-package="com.powerpoint.controller"></context:component-scan>
<!--添加视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--配置前缀-->
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <!--配置后缀-->
        <property name="suffix" value=".jsp"/>
    </bean>

</beans>

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">

    <!--中文编码过滤器-->
    <filter>
        <filter-name>encode</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <!--	private String encoding;
                private boolean forceRequestEncoding = false;
                private boolean forceResponseEncoding = false;-->
        <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>encode</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--注册springmvc.xml-->
    <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>/</url-pattern>
    </servlet-mapping>
</web-app>

webapp/WEB-INF/jsp/login.jsp

<%--
  Created by IntelliJ IDEA.
  User: xiaoshiguang
  Date: 2023/7/4
  Time: 21:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
    <form action="${pageContext.request.contextPath}/loginShow" method="post">
        姓名:<input type="text" name="name">
        <br>
        密码:<input type="password" name="pwd">
        <br>
        <input type="submit">
        ${msg}
    </form>
</body>
</html>

webapp/WEB-INF/jsp/main.jsp

<%--
  Created by IntelliJ IDEA.
  User: xiaoshiguang
  Date: 2023/7/4
  Time: 21:57
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>main</title>
</head>
<body>
用户名:${name}<br>
密码:${pwd}
</body>
</html>

controller/WebInfAction.java

package com.powerpoint.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Description:
 * date: 2023/7/4 21:52
 *
 * @since JDK 11
 */
@Controller
public class WebInfAction {
	@RequestMapping("/login")
	public String login(){
		System.out.println("请求转发到login页面");
		return "login";
	}
	@RequestMapping("loginShow")
	public String loginShow(String name,String pwd,HttpServletRequest request){
		if("yyds".equalsIgnoreCase(name) && "yyds".equalsIgnoreCase(pwd)){
			request.setAttribute("name",name);
			request.setAttribute("pwd",pwd);
			return "main";
		}
		String msg = "用户名或密码错误";
		request.setAttribute("msg",msg);
		return "login";
	}
}

登陆业务的实现,隐藏了.jsp,安全性提高,只能访问webapp下的index.jsp,通过在springmvc.xml的视图解析器的修改以及在controller层的路径修改,实现在页面输出localhost:8080/login,就可以跳转到login.jsp页面

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/718286.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

USB转串口电路—电源与防倒灌设计

USB转串口芯片和串口负载&#xff08;MCU、CPU、其他串口外设等&#xff09;的供电方式可以分为2个大类&#xff1a;统一供电和独立供电。 一、供电说明 统一供电是指USB芯片和串口负载使用同一电源&#xff0c;上下电同步&#xff0c;此时不会存在彼此之间电流倒灌的问题。 …

ORA-01122 ORA-01200故障处理---惜分飞

由于某种原因客户的数据库启动报ORA-01122 ORA-01200错误 让客户把system01.dbf文件发给我进行分析,发现system01.dbf文件大于32G(在8k的blocksize库中,默认情况system01.dbf文件不会超过32G),这个明显异常 检测坏块情况发现4096000之后的block全部为全0块 通过bbed分析文…

数学建模常用模型(二):插值与拟合

数学建模常用模型&#xff08;二&#xff09;&#xff1a;插值与拟合 在数学建模中&#xff0c;插值和拟合是常用的数据分析技术&#xff0c;用于从给定的离散数据中推断出连续函数或曲线的近似形式。 插值是通过已知数据点之间的插值多项式来估计未知数据点的值。插值方法的目…

SQL之收集SQL Server线程等待信息

要知道线程等待时间是制约SQL Server效率的重要原因&#xff0c;这一个随笔中将学习怎样收集SQL Server中的线程等待时间&#xff0c;类型等信息&#xff0c;这些信息是进行数据库优化的依据。 sys.dm_os_wait_stats 这是一个系统视图&#xff0c;里面存储线程所遇到的所有的等…

smart Spring:自定义注解、拦截器的使用(更新中...)

文章目录 〇、使用自定义注解的好处和工作原理一、如何使用自定义注解1.自定义一个注解2.在类、属性、方法上进行使用3.元注解 二、使用拦截器的好处和工作原理三、如何使用拦截器参考 本博客源码&#xff1a; 〇、使用自定义注解的好处和工作原理 自定义注解是Java语言提供的…

消息中间件应用场景

提高系统性能首先考虑的是数据库的优化&#xff0c;但是数据库因为历史原因&#xff0c;横向扩展是一件非常复杂的工程&#xff0c;所有我们一般会尽量把流量都挡在数据库之前。 不管是无限的横向扩展服务器&#xff0c;还是纵向阻隔到达数据库的流量&#xff0c;都是这个思路。…

JSP网上手机商城系统 用eclipse定制开发mysql数据库BS模式java编程jdbc

一、源码特点 JSP 网上手机商城系统是一套完善的web设计系统&#xff0c;对理解JSP java SERLVET mvc编程开发语言有帮助&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统主要采用B/S模式开发。开发环境为 TOMCAT7.0,eclipse开发&#xff0c;数据库为Mysql5.0&a…

Camera API1 简叙述

目录 一、开启相机 1.1创建项目 1.2注册权限 1.3配置相机特性要求 1.4 获取摄像头的个数 1.5 根据 ID 获取 CameraInfo facing 1.6 开启相机 1.7 关闭相机 二、预览 2.1认识 Parameters 2.2 设置预览尺寸 2.3添加预览 Surface 2.4 开启和关闭预览 2.5 校正预览画…

【电路原理学习笔记】第2章:电压、电流和电阻:2.3 电压

第2章&#xff1a;电压、电流和电阻 2.3 电压 正电荷和负电荷之间存在着吸引力&#xff0c;必须以做功的形式施加一定的能量来克服吸引力&#xff0c;才能使正、负电荷分开一定的距离。所以极性相反的电荷由于它们之间的距离而具有一定的势能。电荷之间的势能之差就称为电位差…

一、枚举类型——新特性(switch 中的 case null)

JDK 17新増了&#xff08;预览&#xff09;功能&#xff0c;可以在 switch 中引入原本非法的 case null。以前只能在 switch 的外部检查是否为 null&#xff0c;如 old() 中所示&#xff1a; CaseNull.java import java.util.function.Consumer;public class CaseNull {static …

Apache Doris 2.0-beta 盲测性能 10 倍提升,更统一的多场景极速分析体验!

亲爱的社区小伙伴们&#xff0c;我们很高兴地向大家宣布&#xff0c;Apache Doris 2.0-beta 版本已于 2023 年 7 月 3 日正式发布&#xff01;在 2.0-beta 版本中有超过 255 位贡献者为 Apache Doris 提交了超过 3500 个优化与修复&#xff0c;欢迎大家下载使用&#xff01; 下…

UE4/5数字人Metahuman与Style3D的使用【三、用数字人进行布料模拟可能出现的两个问题】

目录 接下来我们简单讲解数字人进行的布料模拟 存在的问题一&#xff1a;肩带滑落问题 存在的问题二&#xff1a; 存在的问题三&#xff1a; 关于前面数字人如何操作以及Style3D的相关知识便不在重复&#xff0c;不了解的可以看&#xff1a; UE4/5数字人Metahuman与Style3…

蓝桥杯专题-试题版含答案-【奋斗的小蜗牛】【兰州烧饼】【对决】【画图】

点击跳转专栏>Unity3D特效百例点击跳转专栏>案例项目实战源码点击跳转专栏>游戏脚本-辅助自动化点击跳转专栏>Android控件全解手册点击跳转专栏>Scratch编程案例点击跳转>软考全系列点击跳转>蓝桥系列 &#x1f449;关于作者 专注于Android/Unity和各种游…

GitHub 组织是什么?您应该使用一个吗?

GitHub 作为一个平台,被个人程序员和大型组织所使用。无论您与多少人一起工作,“GitHub Organizations”都为管理多个项目的人员提供了一些不错的工具。 GitHub 组织是什么? GitHub Organizations 是 GitHub 的一项功能,允许您创建一个中心位置,团队成员可以在其中访问和…

中国汽车协会:我国整车出口数量达到43.8万辆,同比增长92.8%

根据中国汽车工业协会发布的数据&#xff0c;今年5月份我国汽车整车出口量达到43.8万辆&#xff0c;较上月增长3.2%&#xff0c;同比增长92.8%。 同时&#xff0c;整车出口金额环比增长8.8%&#xff0c;同比增长1.2倍。而今年1-5月&#xff0c;汽车整车出口量达到193.3万辆&…

杂记 | 使用idea构建SpringBoot项目并远程连接docker运行

文章目录 概述01 连接远程docker02 本地打包03 创建dockerfile文件04 部署并运行 概述 使用docker运行SpringBoot项目是一个不错的选择&#xff0c;传统方式需要手动打包并上传到服务器&#xff0c;在使用docker build构建镜像&#xff0c;再使用docker run启动运行&#xff0…

Flutter 父子组件互调方法

Flutter 父子组件互调方法 文章目录 Flutter 父子组件互调方法一、父组件调用子组件方法1、概述2、代码实现3、效果 二、子组件调用父组件方法1、概述2、代码实现3、效果 一、父组件调用子组件方法 1、概述 使用 GlobalKey&#xff1a;可以为子组件创建一个 GlobalKey 对象&a…

HOT44-二叉搜索树中第K小的元素

leetcode原题链接&#xff1a;二叉搜索树中第K小的元素 题目描述 给定一个二叉搜索树的根节点 root &#xff0c;和一个整数 k &#xff0c;请你设计一个算法查找其中第 k 个最小元素&#xff08;从 1 开始计数&#xff09;。 示例 1&#xff1a; 输入&#xff1a;root [3,1…

U-Boot移植 (2)- LCD 驱动修改和网络驱动修改

文章目录 1. LCD 驱动修改1.1 修改c文件配置1.2 修改h文件配置1.3 编译测试 2. 网络驱动修改2.1 I.MX6U-ALPHA 开发板网络简介2.2 网络 PHY 地址修改2.3 删除 uboot 中 74LV595 的驱动代码2.4 添加开发板网络复位引脚驱动2.5 更新 PHY 的连接状态和速度2.6 烧写调试2.7 测试一下…

BERT模型蒸馏完全指南(原理技巧代码)

BERT模型蒸馏完全指南(原理/技巧/代码) 小朋友,关于模型蒸馏,你是否有很多问号: 蒸馏是什么?怎么蒸BERT?BERT蒸馏有什么技巧?如何调参?蒸馏代码怎么写?有现成的吗?今天rumor就结合Distilled BiLSTM/BERT-PKD/DistillBERT/TinyBERT/MobileBERT/MiniLM六大经典模型,…