0
点赞
收藏
分享

微信扫一扫

搭建springmvc项目(模拟登录功能)

AbrahamW 2022-04-07 阅读 47
mvc

前言

一:搭建SpringMCV项目

1.创建一个动态的Web项目

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

2.导包

在这里插入图片描述

3.创建 SpringMvc.xml文件,并书写相关配置

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

<beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns="http://www.springframework.org/schema/beans" 
		xmlns:context="http://www.springframework.org/schema/context" 
		xmlns:aop="http://www.springframework.org/schema/aop" 
		xmlns:tx="http://www.springframework.org/schema/tx" 
		xmlns:mvc="http://www.springframework.org/schema/mvc" 
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">

</beans>
<context:component-scan base-package="com.fall.*"></context:component-scan>
<!-- 配置处理器映射器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<!-- 配置处理器适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> 

springmvc.xml完整代码

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
		xmlns="http://www.springframework.org/schema/beans" 
		xmlns:context="http://www.springframework.org/schema/context" 
		xmlns:aop="http://www.springframework.org/schema/aop" 
		xmlns:tx="http://www.springframework.org/schema/tx" 
		xmlns:mvc="http://www.springframework.org/schema/mvc" 
		xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.2.xsd 
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
		http://www.springframework.org/schema/tx 
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd 
		http://www.springframework.org/schema/mvc 
		http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd ">
	<!-- 此处输入内容 -->
	<!-- 开启注解扫描功能 -->
	<context:component-scan base-package="com.fall.*"></context:component-scan>
	<!-- 配置处理器映射器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
	<!-- 配置处理器适配器 -->
	<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean> 
</beans>

web.xml中需添加配置

<!-- 配置前端控制器 -->
<!-- 声明springMVC的框架服务 -->
<servlet>
	<servlet-name>springmvc</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 指定Spring MVC的核心配置文件 -->
	<!-- 初始化参数  ContextConfigLocation 上下文配置文件位置
	classpath:springmvc.xml scr目录下的springmvc.xml文件
	 -->
	<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映射的作用,将请求转发至对听的servlet处理  这项配置的目的就是告诉web服务,请求交给DispatcherServlet类-->
<servlet-mapping>
	<servlet-name>springmvc</servlet-name>
	<!-- 拦截所有以do结尾的请求 -->
	<url-pattern>*.do</url-pattern>
</servlet-mapping>

4.测试

在这里插入图片描述

在这里插入图片描述

二:写书模拟登录的逻辑代码

1.创建包

在这里插入图片描述

2.创建User类

package com.fall.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
/**
 * 要求  各个实体类的方法独立  比如用户的相关的方法服务,就在一个Controller一个类里面
 * @author fall
 *
 */
@Controller
@RequestMapping("/user")
public class User {
	@RequestMapping(value = "login", method = RequestMethod.GET)
	public ModelAndView 
	login(@RequestParam("phone") String phone, @RequestParam("password") String pwd) {
		if (phone.equals("123")&&pwd.equals("123")) {
			System.out.println("登录成功");
		}else {
			System.out.println("登录失败");
		}
		/**
		 * login方法要想访问成功 1.保证路径一致 login.do 2.保证请求访问一致get方式
		 * 3.保证参数值包含方法中声明的这几个参数/user/login.do?name=fall&age=18&gender=boy
		 */
		ModelAndView model = new ModelAndView("/login.jsp");// 创建视图,作为请求响应的内容
		return model;// 返回页面给浏览器
	}
}

3.login.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>
	<h1>登录</h1>
	<form action="/login_mvc/user/login.do" method="get">
		账号:<input name="phone" /><br> 密码:<input name="password" /><br>
		<input type="submit" value="登录">
	</form>
</body>
</html>

4.测试

成功
在这里插入图片描述

失败
在这里插入图片描述

举报

相关推荐

0 条评论