網站首頁 編程語言 正文
@Autowired 注解屬于 org.springframework.beans.factory. annotation 包,可以對類成員變量、方法及構造函數進行標注,完成自動裝配的工作。
@Service 注解屬于 org.springframework.stereotype 包,會將標注類自動注冊到 Spring 容器中。
在配置文件中需要添加 元素來掃描依賴基本包。
<context:component-scan base-package=“net.biancheng.service”/>
User 實體類
public class User{
private String name;
private String pwd;
public String setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
}
創建 UserService 接口
public interface UserService{
boolean login(User user);
}
創建 UserServiceImpl 類,實現 UserService 接口?
@Service
public class UserServiceImpl implements UserService{
@Override
public boolean login(User user){
if("yyyjjj".equals(user.getName()) && "12345".equals(user.getPwd())){
return ture;}
return false;
}
}
為了使類能被 Spring 掃描到,必須為其標注 @Service。
創建 UserController 類?
@Controller
@RequestMapping("/users")
public class UserController{
@Autowired
private UserService userService;
@RequestMapping("/login")
public String getLogin(Model model) {
User us = new User();
us.setName("bianchengbang");
userService.login(us);
model.addAttribute("user", us);
return "login";
}
}
在 UserService 上添加 @Autowired 注解會使 UserService 的一個實例被注入到 UserController 實例中。
springmvc-servlet.xml 代碼
<context:component-scan base-package="com.biancheng"/>
<mvc:annotation-driven>
<bean id="viewResolver"
class="com.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="?WEB_INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
web.xml 代碼
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>springMVC</display-name>
<!-- 部署 DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springmvc-servlet.xml</param-value>
</init-param>
<!-- 表示容器再啟動時立即加載servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- 處理所有URL -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
index.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>
未注冊的用戶,請
<a href="${pageContext.request.contextPath }/user/register"> 注冊</a>!
<br /> 已注冊的用戶,去
<a href="${pageContext.request.contextPath }/user/login"> 登錄</a>!
</body>
</html>
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>Insert title here</title>
</head>
<body>
登錄頁面! 歡迎 ${user.name} 登錄
</body>
</html>
原文鏈接:https://blog.csdn.net/qq_44174346/article/details/125779229
相關推薦
- 2022-05-22 Nginx的基本概念和原理_nginx
- 2023-02-23 一文詳解C語言char類型中的存儲_C 語言
- 2023-03-21 Mongodb?用戶權限管理及配置詳解_MongoDB
- 2022-11-24 Django?ORM?F對象和Q對象查詢_python
- 2022-08-02 C#中DateTime函數的詳細用法_C#教程
- 2022-05-20 nginx刷新頁面出現404解決方案(親測有效)_nginx
- 2022-04-04 使用uniapp封裝簡易通用的ajax請求以及攜帶token去請求 (代碼優化新增請求日志和響應日志
- 2022-09-08 Python?避免字典和元組的多重嵌套問題_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支