日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Spring MVC @Autowired和@Service注解

作者:ezezeez 更新時間: 2022-07-16 編程語言

@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

欄目分類
最近更新