前言
通过老师搭建springmvc项目演示的模拟注册功能,要求完成模拟登录,首先搭建springmvc项目,后而分析登录功能的逻辑,从而实现模拟登录功能。
一:搭建SpringMCV项目
1.创建一个动态的Web项目
File===>New===>Dynamic Web Project
- Project name:创建项目的名称
- Target runtime:服务 ,教学用的是8.0或者8.5版本的,由于我自己用的是9.0版本的Tomcat因此,选择 Apache Tomcat V9.0的
注意:若没有配置过tomcat服务则默认(None)
3.千万不要直接点击Finish,我们需点击Next
依次点击2次Next后,则会弹出以下界面,我们需勾选 Generate web.xml deoloyment descriptor 自动生成映射 ,勾选后则点击Finish
这样我们就得到了一个动态的Web项目
2.导包
把spring所需的jar复制在 WebContent——WEB-INF——lib目录下。
接着选中lib目录下的所有jar包,右击鼠标 选择 Build Path下的Add to Build Path导入jar包,这样就导入jar包了
3.创建 SpringMvc.xml文件,并书写相关配置
java Resources==>New===>Other
由以上连续点击会弹出以下界面 ,在画线处找到 XML File 点击 然后点击Next
由以上步骤可弹出,以下界面,接着在 File name:输入springmvc.xml,此后点击Finish即可
完成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>
注意:以下爱的代码不能换行,避免,扫描失败,并且一定要放在<·beans>中
base-package=“com.fall.*”,中com.fall.*代指在扫描com.fall下所有的包。换成自己的即可
<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中需添加配置
由于我们之前创建项目的时候,勾选 Generate web.xml deoloyment descriptor自动生成web.xml,因此我们只需在WebContent——WEB-INF下找到,把以下映射添加即可
<!-- 配置前端控制器 -->
<!-- 声明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.测试
在WebContent下创建一个jsp文件,测试 springmvc项目是否搭建成功
注意:以下画线ISO-8859-1均改为UTF-8实质上是改变编码格式为UTF-8
搭建成功会出现以下字样
二:写书模拟登录的逻辑代码
1.创建包
选中Java Resources下的src鼠标右击New===》Package 输入包名
注意:包名要和配置文件中的一致(com.fall.* 其中com.fall,可以根据自身更改,则*代表的是所有,启动项目的时候扫描.com.fall下的所有包),否则搭建的失败。
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.测试
成功
失败