作者:荆梦梦丶乐园 | 来源:互联网 | 2023-10-13 10:43
最近一直在学习spring MVC。真的很灵活,这次主要做个前台传入数据到控制器并调用服务层来完成数据验证工作并跳转页面。
这次由于没有连接数据库,所以没有设置dao层。直接在service里面做了验证处理。
需求是从前台传入username和password两个参数到后台验证过完成页面跳转。
首先配置web容器的web.xml:
<web-app xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns&#61;"http://java.sun.com/xml/ns/javaee" xmlns:web&#61;"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation&#61;"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"version&#61;"2.5"><servlet><servlet-name>springservlet-name><servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class><init-param><param-name>contextConfigLocationparam-name><param-value>classpath*:springMVC.xmlparam-value>init-param><load-on-startup>1load-on-startup>servlet><servlet-mapping><servlet-name>springservlet-name><url-pattern>/url-pattern>servlet-mapping><welcome-file-list><welcome-file>welcome.jspwelcome-file>welcome-file-list>
web-app>
然后配置springMVC的servlet。这个必须放在根包下面&#xff0c;之前放到包下面&#xff0c;总是无法起作用。
springMVC.xml:
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance" xmlns:p&#61;"http://www.springframework.org/schema/p"xmlns:context&#61;"http://www.springframework.org/schema/context"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:component-scan base-package&#61;"controller">context:component-scan><bean id&#61;"viewResolver"class&#61;"org.springframework.web.servlet.view.InternalResourceViewResolver"><property name&#61;"prefix" value&#61;"/WEB-INF/jsp/">property><property name&#61;"suffix" value&#61;".jsp">property>bean>beans>
然后我们需要写控制器&#xff0c;完成页面跳转与数据存储和业务调用&#xff1a;
&#64;RequestParam(“username”)为前端的username参数放到name对象上。password同理&#xff0c;十分的优雅。
&#64;Controller
&#64;RequestMapping("/login")
public class LoginAction {&#64;RequestMapping("/check")public ModelAndView checkLogin(&#64;RequestParam("username") String name,&#64;RequestParam("password") String password){if(CheckUser.CheckUser(name, password)) return new ModelAndView("success","loginUser","hello micro");return new ModelAndView("error","loginName","you are not allowed into system!");}
}
调用的CheckUser静态构造方法为&#xff1a;
public class CheckUser {public static boolean CheckUser(String username,String password){if(username.equals("micro") && password.equals("123")) return true;return false;}
}
完成业务层的处理。
然后来写welcome.jsp登录页面吧&#xff1a;
<%&#64; page language&#61;"java" contentType&#61;"text/html; charset&#61;UTF-8"pageEncoding&#61;"UTF-8"%>
<html>
<head>
<meta http-equiv&#61;"Content-Type" content&#61;"text/html; charset&#61;UTF-8">
<title>welcome pagetitle>
head>
<body><h1>欢迎h1><form action&#61;"login/check" method&#61;"post">username:<input type&#61;"text" name&#61;"username" value&#61;"micro" /> password:<input type&#61;"text" name&#61;"password" value&#61;"123" /><input type &#61; "submit" value &#61; "提交">form>
body>
html>
如果验证成功跳转到&#xff1a;
<%&#64; page language&#61;"java" contentType&#61;"text/html; charset&#61;UTF-8"pageEncoding&#61;"UTF-8"%>
<html>
<head>
<meta http-equiv&#61;"Content-Type" content&#61;"text/html; charset&#61;UTF-8">
<title>success pagetitle>
head>
<body>
<h1>this is a success pageh1>
${loginUser}
body>
html>
验证失败&#xff1a;
<%&#64; page language&#61;"java" contentType&#61;"text/html; charset&#61;UTF-8"pageEncoding&#61;"UTF-8"%>
<html>
<head>
<meta http-equiv&#61;"Content-Type" content&#61;"text/html; charset&#61;UTF-8">
<title>error pagetitle>
head>
<body>
<h1>this is a error pageh1>
${loginName}
body>
html>
提交就会请求loginAction&#xff0c;然后调用CheckUser完成业务处理 页面跳转和数据存储。
登录成功跳转&#xff1a;
验证失败跳转&#xff1a;