Spring 入门实例 简易登录系统(精通Spring+4.x++企业应用开发实战 学习笔记一)
来源:互联网 发布:淘宝收藏加购有什么用 编辑:程序博客网 时间:2024/05/21 21:46
论坛登录模块
在持久层有两个DAO类,分别是UserDao和LoginLogDao,在业务层对应一个业务类UserService,在展现层拥有一个LoginController类和两个JSP页面,分别是登录页面login.jsp和登录成功页面main.jsp
DAO为数据访问对象(Data Access Object DAO)设计模式,以便将低级别的数据访问逻辑与高级别的业务逻辑分离。
下载jdk和maven,mysql并配置好环境变量
1.创建库表
(1)登录数据库
mysql -u root -p
设置密码格式为
set password for 用户名@localhost = password('新密码');
(2)创建实例对应的数据库
DROP DATABASE IF EXISTS sampledb;CREATE DATABASE sampledb DEFAULT CHARACTER SET utf8;USE sampledb;
默认字符集采用UTF-8
(3)创建实例所用的两张表
创建用户表
CREATE TABLE t_user( user_id INT AUTO_INCREMENT PRIMARY KEY, user_name VARCHAR(30), credits INT, password VARCHAR(32), last_visit datetime, last_ip VARCHAR(23))ENGINE=InnoDB;
创建用于用户登录日志表
CREATE TABLE t_login_log( login_log_id INT AUTO_INCREMENT PRIMARY KEY, user_id INT, ip VARCHAR(23), login_datetime datetime)ENGINE=InnoDB;
InnoDB引擎支持事务
(4)初始化一条数据
INSERT INTO t_user (user_name,password) VALUES('admin','123456');COMMIT;
也可以直接运行脚本文件sampledb.sql
2.在IDEA中创建一个项目
其中的pom.xml配置从下载的源码中直接使用
类包以分层的方式进行组织
3.持久层
领域对象(Domain Object)也称为实体类,它代表了业务的状态,且贯穿表现层,业务层和持久层,并最终被持久化到数据库中。
持久层的主要工作就是从数据库中加载数据并实例化领域对象,或者将领域对象持久化到数据库表中。
“持久层”,也就是在系统逻辑层面上,专著于实现数据持久化的一个相对独立的领域(Domain),是把数据保存到可掉电式存储设备中。持久层是负责向(或者从)一个或者多个数据存储器中存储(或者获取)数据的一组类和组件。
这个层必须包括一个业务领域实体的模型(即使只是一个元数据模型)。
用户领域对象
用户信息领域对象可以看成t_user表的对象映像,每个字段对应一个对象属性。
有3类信息:userName/password,积分(credits)和最后一次登录的信息(lastIp,lastVisit)
package com.smart.domain;import java.io.Serializable;import java.util.Date;public class User implements Serializable{ private int userId; private String userName; private String password; private int credits; private String lastIp; private Date lastVisit; //省略get/set方法}
登录日志领域对象
用户每次成功登录后都会记录一条登录日志
3类信息:用户ID,登录IP和登录时间。(一般情况下还需退出时间)
package com.smart.domain;import java.io.Serializable;import java.util.Date;public class LoginLog implements Serializable { private int loginLogId; private int userId; private String ip; private Date loginDate; //省略get/set方法}
UserDao
包括三个方法:
①getMatchCount():
根据用户名和密码获取匹配的用户数。等于1表示用户名/密码正确,0则错误(这是最简单的用户身份认证方法,实际中还需很多安全策略)
②findUserByUserName:
根据用户名获取User对象
③updateLoginInfo():
更新用户积分,最后登录IP及最后登录时间
package com.smart.dao;import com.smart.domain.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowCallbackHandler;import org.springframework.stereotype.Repository;import sun.security.krb5.internal.PAData;import java.sql.ResultSet;import java.sql.SQLException;@Repository //通过Spring注解一个DAOpublic class UserDao { private JdbcTemplate jdbcTemplate; //数据库相关操作 private final static String MATCH_COUNT_SQL = " SELECT count(*) FROM t_user " + " WHERE user_name =? and password=? "; private final static String UPDATE_LOGIN_INFO_SQL = " UPDATE t_user SET " + " last_visit=?,last_ip=?,credits=? WHERE user_id =?"; @Autowired //自动注入JdbcTemplate的Bean,@Autowired是一种函数,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作 public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ this.jdbcTemplate=jdbcTemplate; } //返回1表示表示用户名/密码正确,0则错误 public int getMatchCount(String userName, String password) { //第一个参数是传入的mysql语句,第二个是参入的参数,第三个参数指定需要返回什么类型 return jdbcTemplate.queryForObject(MATCH_COUNT_SQL, new Object[]{userName, password}, Integer.class); } public User findUserByUserName(final String userName){ String sqlStr = " SELECT user_id,user_name,credits " + " FROM t_user WHERE user_name =? "; final User user=new User(); jdbcTemplate.query(sqlStr, new Object[]{userName}, //匿名类方式实现的回调函数,RowCallbackHandler只处理单行结果 new RowCallbackHandler() { //rs是查询后返回的结果集 public void processRow(ResultSet rs) throws SQLException { user.setUserId(rs.getInt("user_id")); user.setUserName(userName); user.setCredits(rs.getInt("credits")); } }); return user; } public void updateLoginInfo(User user) { jdbcTemplate.update(UPDATE_LOGIN_INFO_SQL, new Object[] { user.getLastVisit(), user.getLastIp(),user.getCredits(),user.getUserId()}); }}
@Repository用于标注数据访问组件,即DAO组件
@Autowired是一种函数,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作
Spring JDBC通过一个模板类JdbcTemplate封装了样板式的代码,用户通过模板类可以轻松地完成大部分数据访问操作。
query(String sqlStr,Object[] args,RowCallbackHandler rch)有三个参数
①sqlStr:SQL语句,允许使用带”?”的占位符
②args:SQL语句中对应位置的参数数组
③RowCallbackHandler :查询结果的处理回调接口,该接口有一个方法processRow(ResultSet rs),负责将查询的结果从ResultSet装载到类似于领域对象的对象实例中
在DAO中编写SQL语句中,通常将SQL语句写在静态变量中,如果太长则采用多行字符串的方式进行构造。在每行SQL语句的句尾和句前都加一个空格,就可以避免分行SQL组合后的错误。
LoginLogDao
package com.smart.dao;import com.smart.domain.LoginLog;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.stereotype.Repository;@Repository //通过Spring注解一个DAOpublic class LoginLogDao { private JdbcTemplate jdbcTemplate; //保存登录日志SQL private final static String INSERT_LOGIN_LOG_SQL= "INSERT INTO t_login_log(user_id,ip,login_datetime) VALUES(?,?,?)"; public void insertLoginLog(LoginLog loginLog){ Object[] args={loginLog.getUserId(),loginLog.getIp(),loginLog.getLoginDate()}; jdbcTemplate.update(INSERT_LOGIN_LOG_SQL,args); } @Autowired public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ this.jdbcTemplate=jdbcTemplate; }}
在Spring中装配DAO
样板式的操作都被JdbcTemplate封装起来了,JdbcTemplate本身需要一个DataSource,这样它就可以根据需要从DataSource中获取或返回连接。
UserDao和LoginLogDao都提供了一个带@Autowired注解的JdbcTemplate变量,所以我们必须先声明一个数据源,然后定义一个JdbcTemplate Bean,通过Spring容器上下文自动绑定机制进行Bean的注入。
在src\resources(Maven工程中,资源文件统一放置在resources文件夹中)目录下创建一个名为smart-context.xml的Spring配置文件
<?xml version="1.0" encoding="UTF-8" ?><!-- ①引入Spring的多个Schema空间的格式定义文件和引入aop及tx命名空间锁对应的Schema文件 --><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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- ②扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入 --> <context:component-scan base-package="com.smart.dao"/> <context:component-scan base-package="com.smart.service"/> <!--③定义一个使用DBCP 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/sampledb" p:username="root" p:password="123456" /> <!-- ④配置Jdbc模板 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" p:dataSource-ref="dataSource" /> <!--⑤ 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource" /> <!-- ⑥通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务 --> <aop:config proxy-target-class="true"> <aop:pointcut id="serviceMethod" expression="(execution(* com.smart.service..*(..))) and (@annotation(org.springframework.transaction.annotation.Transactional))" /> <aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" /> </aop:config> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="*" /> </tx:attributes> </tx:advice></beans>
在②中,扫描类包,将标注Spring注解的类自动转化Bean,同时完成Bean的注入
这一步中使用Spring的 扫描指定类包中的所有类,这样在类中定义的Spring注解(如Repository,@Autowired等)才能产生作用
在③配置数据源
使用dbcp定义了一个数据源,驱动器类为com.mysql.jdbc.Driver,指定MySQL数据库的服务端口(非默认3306时)
在④配置了JdbcTemplate Bean,将③处声明的DataSource注入JdbcTemplate中,而这个JdbcTemplare将通过@Autowired自动注入LoginDao和UserDao的Bean中,可见Spring可以很好地将注解配置和XML配置统一起来。
业务层
在此实例中仅有一个业务类UserService,负责将持久层的UserDao和LoginlogDao组织起来,完成用户/密码认证,登录日志记录等操作。
有3个业务办法:
①hasMatchUser
用于检查用户名/密码的正确性
②findUserByUserName
以用户名来条件加载User对象
③loginSuccess
在用户登录成功后调用,更新用户最后登录时间和IP信息,同时记录用户登录日志
package com.smart.service;import com.smart.dao.LoginLogDao;import com.smart.dao.UserDao;;import com.smart.domain.LoginLog;import com.smart.domain.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Service //将UserService标注为一个服务层的Beanpublic class UserService { private UserDao userDao; private LoginLogDao loginLogDao; @Autowired // 注入userDao的Bean public void setUserDao(UserDao userDao){ this.userDao=userDao; } @Autowired //注入loginLogDao的Bean public void setLoginLogDao(LoginLogDao loginLogDao){ this.loginLogDao=loginLogDao; } public boolean hasMatchUser(String userName,String paasword){ int matchCount=userDao.getMatchCount(userName,paasword); return matchCount>0; } public User findUserByUserName(String userName){ return userDao.findUserByUserName(userName); } @Transactional //标注事务注解,让该方法运行在事务环境中,否则该方法在事务中无法运行 public void loginSuccess(User user){ user.setCredits(5+user.getCredits()); LoginLog loginLog=new LoginLog(); loginLog.setUserId(user.getUserId()); loginLog.setIp(user.getLastIp()); loginLog.setLoginDate(user.getLastVisit()); userDao.updateLoginInfo(user); loginLogDao.insertLoginLog(loginLog); }}
loginSuccess()方法将两个Dao组织起来,共同完成一个事务性的数据操作。
loginSuccess()方法根据入参user对象构造出LoginLog对象并将user.credits递增5,即每登录一次赚钱5个积分,然后调用userDao更新到t_user中,再调用loginLogDao向t_login_log表中添加一条记录。
在Spring中装配Service
在上面中smart-context.xml配置文件中,可以发现
在①中,在beans声明处添加aop和tx命名空间定义文件的说明,这样,在配置文件中就可以使用这两个空间下的配置标签了
在②中将service添加到上下文扫描路径中,以便使service包中类的Spring注解生效
在⑤中,配置的事务管理器,负责声明式事务的管理,需要应用dataSource Bean
在⑥通过AOP配置提供事务增强,让service包下所有Bean的所有方法拥有事务
通过aop及tx命名空间的语法,以AOP的方式为service包下所有类的所有标注@Transactional注解的方法都添加了事务增强,它们都将工作在事务环境中
单元测试
这里使用的测试采用TestNG框架
在test目录下,创建com.smart.service,并创建UserService对应的测试类UserServiceTest
package com.smart.service;import java.util.Date;import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;import org.testng.annotations.*;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import com.smart.domain.User;import static org.testng.Assert.*;@ContextConfiguration("classpath*:/smart-context.xml") //启动Spring容器,用于指定Spring的配置文件public class UserServiceTest extends AbstractTransactionalTestNGSpringContextTests { @Autowired private UserService userService; @Test //标注测试方法 public void testHasMatchUser(){ boolean b1=userService.hasMatchUser("admin","123456"); boolean b2=userService.hasMatchUser("admin","1111"); assertTrue(b1); assertTrue(!b2); } @Test public void testFindUserByUserName()throws Exception{ for(int i=0;i<100;i++){ User user=userService.findUserByUserName("admin"); assertEquals(user.getUserName(),"admin"); } } @Test public void testAddLoginLog(){ User user = userService.findUserByUserName("admin"); user.setUserId(1); user.setUserName("admin"); user.setLastIp("192.168.12.7"); user.setLastVisit(new Date()); userService.loginSuccess(user); }}
UserServiceTest通过测试基类AbstractTransactionalTestNGSpringContextTests来启动测试运行器。
@ContextConfiguration用于指定Spring的配置文件
右键Run UserServiceTest可以看到3个业务方法已成功执行
展现层
配置Spring MVC框架
对web.xml(放在/WEB-INF目录下)进行配置,以便Web容器启动时能自动启动Spring容器
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- ①从类路径下加载Spring配置文件,classpath关键字特指类路径下加载--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:smart-context.xml</param-value> </context-param> <!-- ②负责启动Spring容器的监听器,将应用①处的context参数获得Spring配置文件的地址--> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- Spring MVC的主控Servlet--> <servlet> <!-- ③--> <servlet-name>smart</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <load-on-startup>3</load-on-startup> </servlet> <!--Spring MVC处理的URL--> <servlet-mapping> <!-- ④--> <servlet-name>smart</servlet-name> <url-pattern>*.html</url-pattern> </servlet-mapping></web-app>
在①处通过Web容器参数指定Spring配置文件的地址
在②处指定Spring所提供的ContextLoaderListener的Web容器监听器,该监听器在Web容器启动时自动运行,它会根据contextConfigLocation Web容器参数获取Spring配置文件,并启动Spring容器。
注意,需要将log4J.propertis日志配置文件放置在类路径下,以便日志引擎自动生效
log4j.rootLogger=INFO,A1log4j.appender.A1=org.apache.log4j.ConsoleAppenderlog4j.appender.A1.layout=org.apache.log4j.PatternLayoutlog4j.appender.A1.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
在③处声明了一个Servlet,名为smart,则在/WEB-INF目录下必须提供一个名为smart-servlet/xml的Spring MVC配置文件,Spring MVC的Servlet会自动将smart-servlet.xml文件和Spring的其他配置文件(smart-dao.xml,smart-service.xml)进行拼装。
在④处对这个Servlet的URL路径映射进行定义,让所有以.html为后缀的URL都能被smart Servlet截获,进而转由Spring MVC框架进行处理。
请求被Spring MVC截获后,首先根据请求的URL查找到目标的处理控制器,并将请求参数封装“命令”对象一起传给控制器处理;然后,控制器调用Spring容器中的业务Bean完成业务处理工作并返回结果视图。
处理登录请求
POJO(Plain Ordinary Java Object,简单的Java对象)控制器类
LoginController负责处理登录请求,完成登录业务,并根据登录成功与否转向成功或失败页面
package com.smart.web;import com.smart.domain.User;import com.smart.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;import java.util.Date;@Controller //标注为一个Spring MVC的Controllerpublic class LoginController { private UserService userService; //②负责处理/index.html的请求 @RequestMapping(value="/index.html") public String loginPage(){ return "login"; } //③负责处理/loginCheck.html的请求 @RequestMapping(value = "/loginCheck.html") public ModelAndView loginCheck(HttpServletRequest request, LoginCommand loginCommand){ boolean isValidUser=userService.hasMatchUser(loginCommand.getUserName(),loginCommand.getPassword()); if(!isValidUser){ return new ModelAndView("login","error","用户名或密码错误"); }else{ User user=userService.findUserByUserName(loginCommand.getUserName()); user.setLastIp(request.getLocalAddr()); user.setLastVisit(new Date()); userService.loginSuccess(user); request.getSession().setAttribute("user",user);//将user放入Session域 return new ModelAndView("main"); } } @Autowired public void setUserService(UserService userService){ this.userService=userService; }}
通过@Controller注解可以将任何一个POJO的类标注为Spring MVC的控制器处理HTTP请求。
通过@RequestMapping指定方法如何映射请求路径
请求参数会根据参数名称默认契约自动绑定到相应方法的入参中。如loginCheck(HttpServletRequest request, LoginCommand loginCommand)方法中,请求参数会按匹配绑定到loginCommand的入参中。
请求响应方法可以返回一个ModelAndView,或直接返回一个字符串,Spring MVC会解析并转向目标响应页面。
LoginCommand是一个POJO,仅包含用户/密码两个属性
package com.smart.web;public class LoginCommand { private String userName; private String password; //get/set方法}
Spring MVC配置文件
编写好LoginCommand后,需要在smart-servlet.xml(在/WEB-INF目录下)声明该控制器,扫描Web路径,指定Spring MVC的视图解析器
<?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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- ①扫描web包,应用Spring的注解 --> <context:component-scan base-package="com.smart.web"/> <!-- ②配置视图解析器,将ModelAndView及字符串解析为具体的页面 --> <!-- 通过prefix指定在视图名前所添加的前缀,通过suffix指定在视图名后添加的后缀 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:viewClass="org.springframework.web.servlet.view.JstlView" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /></beans>
在LoginController中的③处,控制器根据登录处理结果分别返回ModelAndView(“login”,”error”,”用户名或密码错误”)和ModelAndView(“main”)。
第一个参数代表视图的逻辑名,第二为数据模型名称,第三为数据模型对象,数据模型对象将以数据模型名称为参数放入request的属性中
Spring MVC为视图名到具体视图的映射提供了许多可选择的方法,此使用InternalResourceViewResolver,它通过为视图逻辑名添加前缀,后缀的方式进行解析。
如视图逻辑名为”login”,将解析为/WEB-INF/jsp/login.jsp
JSP视图页面
登录页面为login.jsp和欢迎页面为main.jsp
登录页面login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><html> <head> <title>论坛登录</title> </head> <body> <c:if test="${!empty error}"> <font color="red"><c:out value="${error}" /></font> </c:if> <form action="<c:url value="loginCheck.html"/>" method="post"> 用户名: <input type="text" name="userName"> <br> 密 码: <input type="password" name="password"> <br> <input type="submit" value="登录" /> <input type="reset" value="重置" /> </form> </body></html>
login.jsp有两个作用,即作为登录页面,也作为登录失败后的响应页。
在<c:if test=”${!empty error}”>处使用JSTL标签对登录错误返回的信息进行处理。在JSTL标签中引用了error变量,这个变量正是ModelAndView(“login”,”error”,”用户名或密码错误”)对象所声明的error参数
在<form action=”<c:url value=”loginCheck.html”/>” method=”post”>中login.jsp的登录表单提交到/loginController.html,而loginController中@RequestMapping(value = “/loginCheck.html”)负责处理/loginCheck.html的请求。
JSTL标签会在URL前自动加上应用部署根目录。假设应用部署在网站的bbt目录下,则<c:url>标签将输出/bbt/loginController.html
由于login.jsp放在WEB-INF/jsp目录中,无法直接通过URL进行调用,所以由LoginController控制类中标注了 @RequestMapping(value=”/index.html”)的loginPage()进行转发
欢迎页面main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>论坛</title></head><body> ${user.userName},欢迎您进入论坛,您当前积分为${user.credits};<!-- ① --></body></html>
①处访问Session域中的user对象,显示用户名和积分信息
运行Web应用
基于Maven工程,运行Web应用有两种方式:第一种方式是在IDE工具中配置Web应用服务器,第二种方式是在pom.xml文件中配置Web应用服务器插件
在pom.xml中
<build> <plugins> <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.25</version> <configuration> <connectors> <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector"> <port>8000</port> <maxIdleTime>60000</maxIdleTime> </connector> </connectors> <contextPath>/bbs</contextPath> <scanIntervalSeconds>0</scanIntervalSeconds> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.17</version> <configuration> <parallel>methods</parallel> <threadCount>10</threadCount> </configuration> </plugin> </plugins> </build>
Jetty插件常用配置选项说明:
在Connenctors中配置Connector对象,包含Jetty的监听端口。如果不配置连接器,则默认监听端口会被设置为8080.
contextPath可选,用于配置Web应用上下文。如果不配置此项,则默认上下文采用pom.xml中设置的<artfacId>名称,本例将上下文设置为bbs
overridWebXml可选,是一个应用于Web应用的web.xml的备用web.xml文件。这个文件可以放在任何地方。用户可以根据不同的环境(如测试,开发 等),利用它增加或修改一个web.xml配置。
scanIntervalSeconds 可选,在设置间隔内检查Web应用是否有变化,有变则自动热部署。默认为0,表示禁用热部署,任意一个大于0的数字都表示启用
systemPropertie可选,允许用户在设置一个插件的执行操作时配置系统属性
点击
中的
在Plugins下会自动出现安装的Jetty插件。
双击jetty:run或jetty:run-exploded,将以运行模式启动Jetty服务器,也可以使用Debug运行。
浏览器输入”http://localhost:8000/bbs/index.html”
下图是论坛登录的首页面
密码或用户名错误时
这里输入admin/123456(之前已先初始化),登录到欢迎页面中,如图
通过查看MySQL,可以发现数据库发生了变化
- Spring 入门实例 简易登录系统(精通Spring+4.x++企业应用开发实战 学习笔记一)
- Spring boot入门实例 简易登录(精通Spring+4.x++企业应用开发实战 学习笔记二)
- 论坛开发实例 (精通Spring+4.x++企业应用开发实战)
- Spring容器高级主题(精通Spring+4.x++企业应用开发实战 五)
- Spring对DAO的支持(精通Spring+4.x++企业应用开发实战 第十章)
- Spring的事务管理(精通Spring+4.x++企业应用开发实战 第十一章)
- 在IoC容器中装配Bean(精通Spring+4.x++企业应用开发实战 四)
- Spring基础(精通Spring+4.x++企业应用开发实战 第7章)
- Spring SpEL(精通Spring+4.x++企业应用开发实战 第九章)
- Spring的事务管理难点剖析(精通Spring+4.x++企业应用开发实战 第12章)
- 学习Spring3.X 企业应用开发实战笔记(一)
- Ioc容器 (精通Spring+4.x++企业应用开发实战 三)
- 基于@AspectJ和Schema的AOP(精通Spring+4.x++企业应用开发实战 第八章)
- 《Spring.3.x企业应用开发实战》--- 零碎笔记
- Spring容器高级主题(Spring 3.x企业应用开发实战第五章)学习总结
- Spring AOP基础(Spring 3.x企业应用开发实战第六章)学习总结
- Spring 3.x 企业应用开发实战
- Spring 3.x企业应用开发实战
- JZOJ2937. 【NOIP2012模拟8.9】监听还原(2017.9B组)
- 8-30 PHP学习笔记
- Tomcat配置用户名和密码
- yii的CURD(创建、修改、读取、删除)操作
- 数据结构-双向链表
- Spring 入门实例 简易登录系统(精通Spring+4.x++企业应用开发实战 学习笔记一)
- 对象序列化成JSON格式
- SLIC算法(Simple Linear Iterative Clustering)
- 企业互联网转型何以防骗?看中企动力数字化智能经营
- 数据库day02
- MDK中ARM编译工具的原理
- Redis入门
- Media query 详解 响应式页面
- Netty 源码分析之 一 揭开 Bootstrap 神秘的红盖头 (服务器端)