SpringMVC快速学习

一、SSM框架优化的方向

SpringMVC快速学习_第1张图片 SpringMVC快速学习_第2张图片

目录结构:

SpringMVC快速学习_第3张图片

二、前期文件配置

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.0modelVersion>
  <groupId>com.itheimagroupId>
  <artifactId>springmvc_001_demoartifactId>
  <version>0.0.1-SNAPSHOTversion>
  <packaging>warpackaging>
  
	<dependencies>
		
		<dependency>
			<groupId>org.springframeworkgroupId>
			<artifactId>spring-webmvcartifactId>
			<version>5.2.5.RELEASEversion>
		dependency>
		
		<dependency>
		    <groupId>javax.servletgroupId>
		    <artifactId>javax.servlet-apiartifactId>
		    <version>3.1.0version>
		    <scope>providedscope>
		dependency>
	dependencies>
	
	<build>
    	<finalName>maven.pluginsfinalName>
    	<pluginManagement>
    	<plugins>
    		<plugin>
        		<groupId>org.apache.maven.pluginsgroupId>
        		<artifactId>maven-war-pluginartifactId>
        		<version>3.3.1version>
      		plugin>
    	plugins>    
    	pluginManagement>    
    	
    	<resources>
			<resource>
				<directory>${basedir}/src/main/javadirectory>
				<includes>
					<include>**/*.xmlinclude>
					<include>**/*.propertiesinclude>
				includes>
			resource>
			<resource>
				<directory>${basedir}/src/main/resourcesdirectory>
				<includes>
					<include>**/*.xmlinclude>
					<include>**/*.propertiesinclude>
				includes>
			resource>
		resources> 
	build>
project>

springmvc.xml


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

 
    
    <context:component-scan base-package="com.itheima.controller">context:component-scan>
 

 
    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	
        <property name="prefix" value="/WEB-INF/admin/"/>
        
        <property name="suffix" value=".jsp"/>
    bean>
	
    

beans>

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

image-20230607204053006 SpringMVC快速学习_第4张图片

web.xml


<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>springmvcservlet-name>
 		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
 		<init-param>
 			<param-name>contextConfigLocationparam-name>
 			<param-value>classpath:springmvc.xmlparam-value>
 		init-param>
 	servlet>
 	<servlet-mapping>
 		<servlet-name>springmvcservlet-name>
 		<url-pattern>*.actionurl-pattern>
 	servlet-mapping>
web-app>

三、项目发布

SpringMVC快速学习_第5张图片 SpringMVC快速学习_第6张图片 SpringMVC快速学习_第7张图片

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


<%--${pageContext.request.contextPath}的意思是/项目名称,比如这个项目:/springmvc_001_demo,这个/表示http://localhost:8080/--%>
	访问服务器
	${pageContext.request.contextPath}


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之后SpringMVC快速学习_第8张图片

image-20230607213842863

跳转之后:

SpringMVC快速学习_第9张图片

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

创建javaweb工程

SpringMVC快速学习_第10张图片 SpringMVC快速学习_第11张图片

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

SpringMVC快速学习_第12张图片

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.0modelVersion>

  <groupId>com.itheimagroupId>
  <artifactId>springmvc_002_datasubmitartifactId>
  <version>1.0-SNAPSHOTversion>
  <name>springmvc_002_datasubmitname>
  <packaging>warpackaging>

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

  <dependencies>
      
      <dependency>
          <groupId>javax.servletgroupId>
          <artifactId>javax.servlet-apiartifactId>
          <version>3.1.0version>
          <scope>providedscope>
      dependency>
      
      <dependency>
          <groupId>org.springframeworkgroupId>
          <artifactId>spring-webmvcartifactId>
          <version>5.2.5.RELEASEversion>
      dependency>
    <dependency>
      <groupId>org.junit.jupitergroupId>
      <artifactId>junit-jupiter-apiartifactId>
      <version>${junit.version}version>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.junit.jupitergroupId>
      <artifactId>junit-jupiter-engineartifactId>
      <version>${junit.version}version>
      <scope>testscope>
    dependency>      dependencies>

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

        <resources>
            <resource>
                <directory>${basedir}/src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
            resource>
            <resource>
                <directory>${basedir}/src/main/resourcesdirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
            resource>
        resources>
    build>
project>

resources/springmvc.xml


<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框架


<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>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>
    
    
web-app>
SpringMVC快速学习_第13张图片

五、数据提交方式

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



  JSP - Hello World


<%= "Hello World!" %>


name:
password:

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


    Title


    

hello

2、对象封装提交数据

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>



  JSP - Hello World


<%= "Hello World!" %>


name:
password:

封装成实体类进行数据的提交

<%--要求name的属性值与实体类对象的成员变量的值相同--%>
name:
age:
SpringMVC快速学习_第14张图片

必须一致

实体类: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" %>


    Title


    

hello

3、动态占位符提交

1/1值,1/1{}

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>



  JSP - Hello World


<%= "Hello World!" %>

3、动态占位符提交

<%--1个/1个值,1/一个{}--%> 1/1值

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";
	}
}
SpringMVC快速学习_第15张图片

4、参数名称不一致

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>



  JSP - Hello World


<%= "Hello World!" %>

4、参数名称不一致解决方案

<%--1个/1个值,1/一个{}--%>
name:
age:

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


    Title


    

hello

5、原始方法,手动提取

index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>



  JSP - Hello World


<%= "Hello World!" %>


5、原始方法

name:
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.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" %>


    Title


    

hello

六、中文编码设置

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

配置过滤器

中文编码过滤器靠前放

web.xml


<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>encodefilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceRequestEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
        <init-param>
            <param-name>forceResponseEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodefilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    

    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>
    

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文件中添加注解驱动,它用来解析@ResponseBody

2、配置

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.0modelVersion>

  <groupId>com.itheimagroupId>
  <artifactId>springmvc_003_ajaxartifactId>
  <version>1.0-SNAPSHOTversion>
  <name>springmvc_003_ajaxname>
  <packaging>warpackaging>

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

  <dependencies>
    <dependency>
      <groupId>org.junit.jupitergroupId>
      <artifactId>junit-jupiter-apiartifactId>
      <version>${junit.version}version>
      <scope>testscope>
    dependency>
    <dependency>
      <groupId>org.junit.jupitergroupId>
      <artifactId>junit-jupiter-engineartifactId>
      <version>${junit.version}version>
      <scope>testscope>
    dependency>

    
    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.1.0version>
    dependency>
    
    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webmvcartifactId>
      <version>5.2.5.RELEASEversion>
    dependency>
    
    <dependency>
      <groupId>com.fasterxml.jackson.coregroupId>
      <artifactId>jackson-databindartifactId>
      <version>2.9.8version>
    dependency>
  dependencies>

    
<build>
  <resources>
    <resource>
      <directory>${basedir}/src/main/javadirectory>
      <includes>
        <include>**/*.xmlinclude>
        <include>**/*.propertiesinclude>
      includes>
    resource>
    <resource>
      <directory>${basedir}/src/main/resourcesdirectory>
      <includes>
        <include>**/*.xmlinclude>
        <include>**/*.propertiesinclude>
      includes>
    resource>
  resources>
build>
project>

springmvc.xml


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

    
    <mvc:annotation-driven>mvc:annotation-driven>
beans>

web.xml


<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>encodefilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceRequestEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
        <init-param>
            <param-name>forceResponseEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodefilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    

    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-name>
        <url-pattern>*.actionurl-pattern>
    servlet-mapping>
    
web-app>

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

SpringMVC快速学习_第16张图片

目录结构,导入jQuery文件


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

P45登陆业务实现

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.0modelVersion>

    <groupId>com.powerpointgroupId>
    <artifactId>springmvc_005_webinfartifactId>
    <version>1.0-SNAPSHOTversion>
    <name>springmvc_005_webinfname>
    <packaging>warpackaging>

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

    <dependencies>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>4.0.1version>
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiter-apiartifactId>
            <version>${junit.version}version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.junit.jupitergroupId>
            <artifactId>junit-jupiter-engineartifactId>
            <version>${junit.version}version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>5.2.5.RELEASEversion>
        dependency>
    dependencies>


    
    <build>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-war-pluginartifactId>
                <version>3.3.2version>
            plugin>
        plugins>
    build>
project>

springmvc.xml


<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


<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>encodefilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
        <init-param>
            <param-name>forceRequestEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
        <init-param>
            <param-name>forceResponseEncodingparam-name>
            <param-value>trueparam-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodefilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>
    
    <servlet>
        <servlet-name>springmvcservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>springmvcservlet-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" %>


    登录


    
姓名:
密码:
${msg}

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


    main


用户名:${name}
密码:${pwd}

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页面

你可能感兴趣的:(后端框架,学习)