当前位置: 移动技术网 > IT编程>开发语言>Java > SpringMVC 实现用户登录实例代码

SpringMVC 实现用户登录实例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

springmvc的一个登陆小案例

准备工作

  1. 创建一个dynamic web project(本人是eclipse)
  2. 添加相关的jar包,构建路径
  3. 创建springmvc-servlet.xml,及完善web.xml
  4. 创建代码逻辑

目录结构如下

对于新手而言,有一个项目的完整的目录结构是多么幸福的一件事啊。

目录结构

个人建议:注意其中的springmvc-servlet.xml的位置。以及源代码包的名称。

代码实战

首先是大管家,web.xml:

<?xml version="1.0" encoding="utf-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
  xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xsi:schemalocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  id="webapp_id" version="3.1">
  <display-name>springtest</display-name>
  <welcome-file-list>
    <welcome-file></welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.spring</url-pattern>
  </servlet-mapping>


</web-app>

然后是小管家springmvc-servlet.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 
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
      http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

  <!-- 最简单的配置,让spring自己去探索-->
  <context:component-scan base-package="controller"></context:component-scan>


</beans>

再就是一个登陆界面了,login.jsp:

<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>登陆界面</title>
</head>
<body>

  <form action="login.spring" method="post">
    username:<input type="text" name="username"><br /> password:<input
      type="password" name="password"><br /> <input type="submit"
      value="登陆">
  </form>
</body>
</html>

login.jsp对应的那个action就是要进行处理的后台页面,也就是我们的login.java:

package controller;

import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestparam;

@controller // @controller 代表本java类是controller控制层
public class login {

  /**
   * @requestparam注解的作用是:根据参数名从url中取得参数值
   * @param username
   *      用户名,一定要对应着表单的name才行
   * @param password
   *      用户密码,也应该对应表单的数据项
   * @param model
   *      一个域对象,可用于存储数据值
   * @return
   */
  @requestmapping("/login") // @requestmapping 注解可以用指定的url路径访问本控制层
  public string login(@requestparam("username") string username, @requestparam("password") string password,
      model model) {

    if (username.equals("admin") && password.equals("admin")) {
      model.addattribute("username", username);
      return "ok.jsp";
    } else {
      model.addattribute("username", username);
      return "no.jsp";
    }
  }

}

最后就是ok.jsp和no.jsp了:

<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
</head>
<body>
<font color="green">${username } </font>欢迎你!

</body>
</html>



<%@ page language="java" contenttype="text/html; charset=utf-8"
  pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>insert title here</title>
</head>
<body>

  <font color="red">sorry</font>,没有${username }这个用户!
  <br />
  <a href="login.jsp" rel="external nofollow" >重试一下!</a>

</body>
</html>

测试

在你的浏览器上输入http://localhost:8080/springtest/login.jsp

然后就可以对代码进行测试了。本人亲测好用,这里就不再贴图了。

总结

  1. 在web.xml中配置dispatcherservlet核心控制器
  2. 在web-inf文件夹中创建springmvc-servlet.xml配置文件
  3. @controller、@requestmapping、@requestparam以及model域对象等的使用
  4. 表单以post方式,或者使用get方式都是可以的

下面是注解的小技巧:

@controller就是对应于springmvc-servlet.xml中的

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网