三大框架之SpringMVC :一个小的登录注册项目的网页实现

今天学习了 SpringMVC ,最后做了一个小的项目,熟悉的登录注册

1.项目结构:

这里写图片描述
这里写图片描述

源代码传送门

2 . 导包介绍:

5 个 Spring 基础包 : beans, context, core, expression + commons 日志包

1 个 aop 包

2 个 web 包 :web webmvc

2个 html 的 jstl 包:jstl , standard ;

3. 项目结构说明:

1. User的model类

2. UserDao/UserDaoiml

3. UserService/UserServiceImpl 

4. Util工具类

5. controller类

6. 3个配置文件: applicationContext.xml    spring-mvc.xml    web.xml

4. 三个配置文件的代码 :

@ applicationContext.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: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
    http://www.springframework.org/schema/context/spring-context.xsd">


    <context:component-scan base-package="com.zhiyou100"/>
    <!-- 为了对注解的支持 -->


</beans>

@ spring-mvc.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: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
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">

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

    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/view/"></property>
        <property name="suffix" value=".jsp"></property>

    </bean>

</beans>

@ 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>SSTesst1_Stu</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>


  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>

5 . 关于配置的解析说明

-------------------------------------------
------------- spring ----------------------
-------------------------------------------

2. 在 src 下创建 spring 的配置文件 

    2.1 复制粘贴

<?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: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
    http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

    2.2 <context:component-scan base-package="com.zhiyou100"/>
        增加对注解的支持

3. 在 web.xml 中增加监听器读取 spring 的配置文件
    3.1 增加监听器 listener 和 listener-class 两个标签
    3.2 class 是 ContextLoaderListener 的全名 = 包名 + 类名


4. 在 web.xml 增加标签设置 spring 配置文件的位置
    4.1 增加 context-param 和 param-name、param-value 标签
    4.2 param-name:contextConfigLocation
    4.3 param-value:classpath:applicationContext.xml


-----------------------------------------------
------------- spring-mvc ----------------------
-----------------------------------------------

5. 在 src 下创建 spring-mvc 的配置文件
    5.1 复制粘贴
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>

    5.2 使用默认的 handleMapping 和启用 mvc 中的注解
        <mvc:annotation-driven/>

    5.3 增加页面解析类 InternalResourceViewResolver
        5.3.1 增加 bean 标签
        5.3.2 设置 class = 全名 = 包名 + 路径

    5.4 增加 prefix 和 suffix 设置页面路径的前缀和后缀
        设置前缀以后将来所有的 jsp 必须放在 view 文件夹下

    <property name="prefix" value="/view/"></property>
        <property name="suffix" value=".jsp"></property>

6. 去 web.xml 中配置唯一的 Servlet
    6.1 增加 servlet和 servlet-mapping 标签

    6.2 配置 servlet 标签,name 随便起,class 是 DispatcherServlet

    6.3 配置 servlet-mapping,name 和上边保持一致,url-pattern 是 /

    6.4 在 servlet 标签中增加 init-param 标签设置 spring-mvc 配置文件的路径

    6.5 param-name:contextConfigLocation

    6.6 param-value:classpath:spring-mvc.xml

6. 配置完成后,去 实现 功能:

-----------------------------------------------
------------- 登录注册 ------------------------
-----------------------------------------------

1. 在 src 下创建 controller,service,dao,model,util 包

2. 在 WebContent 下创建 view 文件夹,存放我们所有的 jsp 文件

3. 创建 model 类 User,三个属性 username,password,email

4. 创建 UserService 接口,定义方法

5. 创建 UserDao 接口,定义方法

6. 创建 UserDaoImpl 类实现 UserDao 接口,添加 @Repository 注解
    方便将来注入到 Service 中使用

7. 创建 DBUtil 类,添加 @Component
    默认就是单例模式,方便我们使用

8. 回到 UserDaoImpl,增加 DBUtil 属性和对应的 setter 方法。

9. 在 DBUtil 属性上增加 @Autowired 注解
    创建 UserDaoImpl 的时候,IoC 容器会自动把 DBUtil 对象赋值给这个属性

10. 实现 UserDaoImpl 中的所有功能

11. 创建 UserServiceImpl 实现 UserService 接口,添加 @Service 注解
    方便将来注入到 Controller 中使用

12. 增加 UserDao 属性和对应的 setter 方法。

13. 在 UserDao 属性上增加 @Autowired 注解
    创建 UserServiceImpl 的时候,IoC 容器会自动把 UserDaoImpl 对象赋值给这个属性

14.实现 UserServiceImpl 功能

15. 增加 test 源文件夹和 UserServiceTest 测试类,对 Service 进行测试

16. 在 UserServiceTest 中增加 @RunWith(SpringJUnit4ClassRunner.class)

17. 在 UserServiceTest 中增加 @ContextConfiguration("classpath:applicationContext.xml")

18. 创建 UserController 增加 @Controller 注解,注入 UserService,完成功能

19. 导入 jstl,页面中的所有路径都使用绝对路径
    <a href='<c:url value="/login"></c:url>'>登录</a>

7. modelController 的代码:

package com.zhiyou100.controller;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhiyou100.model.User;
import com.zhiyou100.service.UserService;

@Controller
public class UserController {

    @Autowired
    private UserService service;

    @RequestMapping("/login")
    public String login(User user,Model model) {

        if(service.login(user)){

            model.addAttribute("username", user.getUsername());

            return "welcome";
        }else {
            model.addAttribute("error", "登陆失败");

            return "login";
        }

    }

    @RequestMapping("/register")
    public String register(User user,Model model) {

        if(service.register(user)){

            model.addAttribute("username", user.getUsername());

            return "welcome";
        }else {
            model.addAttribute("error", "注册失败");

            return "register";
        }

    }

    @RequestMapping("/reset")
    public String resetPassword(User user,Model model) {

        if(service.resetPassword(user)){

            model.addAttribute("username", user.getUsername());

            return "welcome";
        }else {
            model.addAttribute("error", "重置失败");

            return "reset";
        }

    }

    @RequestMapping("/list")
    public String listUsers(Model model) {

        Collection<User> list = service.listUser();

        model.addAttribute("list", list);

        return "list";
    }
}

8. 效果截图:

1 . 首页

这里写图片描述

2. 注册

这里写图片描述

3. 注册完成

这里写图片描述

4. 登录

这里写图片描述

多注册了几个

这里写图片描述

5. 展示所有

这里写图片描述

  • 13
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
SpringIOC框架上加上SpringMVC框架实现用户登录/注册系统具有以下优点: 1. 松耦合:SpringIOC和SpringMVC框架都是基于面向接口的编程思想,通过依赖注入和控制反转技术实现了组件之间的松耦合。这样可以降低模块之间的耦合度,提高系统的可维护性和扩展性。 2. 清晰分层:SpringIOC负责管理Bean的创建和组装,而SpringMVC负责处理求和响应。通过将业务逻辑和控制逻辑分离,使得系统的架构更加清晰,易于理解和维护。 3. AOP支持:Spring框架提供了强大的面向切面编程(AOP)支持,可以在不修改源代码的情况下,通过配置方式实现事务管理、日志记录等横切关注点。这样可以提高代码的重用性和可维护性。 4. 声明式事务管理:Spring框架提供了声明式事务管理的支持,可以通过配置方式简化事务管理的代码。这样可以减少开发人员对事务处理的关注,提高开发效率。 5. 强大的数据访问支持:Spring框架提供了丰富的数据访问支持,可以方便地集成各种持久化框架(如Hibernate、MyBatis等),并提供了对事务、连接池等的统一管理。这样可以简化数据访问层的开发,提高代码的可测试性和可维护性。 6. MVC模式:SpringMVC框架基于经典的MVC(Model-View-Controller)模式,将系统的不同功能模块进行分离,使得代码更加清晰和可维护。同时,SpringMVC提供了丰富的功能,如求映射、参数绑定、数据验证、视图解析等,可以帮助开发人员快速构建灵活、可扩展的Web应用程序。 综上所述,通过在SpringIOC框架上加上SpringMVC框架实现用户登录/注册系统,可以提高系统的可维护性、扩展性和代码重用性,同时简化开发流程,加快开发速度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值