Create a project
1. Create a folder my-shop, opened with idea
2. Create a pom.xml (Add rely Junit spring sevlet log4j), with the idea Managed
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-shop</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13-beta-3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
</dependencies>
</project>
Hosted idea
3. perfect maven directory structure
Directory Structure
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>my-shop</display-name>
</web-app>
4. Improve schema directory (three-tier architecture + MVC)
The spring and arranged log4j
Create a spring-context.xml log4j.properties in the src / main / resources
spring-context.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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
log4j.properties
#根日志,配置了日志级别为INFO,预定义了console,file两种附加器
log4j.rootLogger=INFO, console, file
#console附加器,日志输出在控制台,采用匹配器布局模式,日志输出格式
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
#file附加器,每天产生一个日志文件,输出在logs/log.log,采用匹配器布局模式,日志文件最大值,最多纪录文件数,日志格式
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.File=logs/log.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.A3.MaxFileSize=1024KB
log4j.appender.A3.MaxBackupIndex=10
log4j.appender.file.layout.ConversionPattern=%d %p [%c] - %m%n
6. Create index.jsp
7. Configure jdk and tomcat
8. Run the project
Bootstrap Admin Template
Download AdminLTE https://github.com/ColorlibHQ/AdminLTE
The things we need to put assets to the project (css img js folder in the list)
Note: After the copy I came brower_components API.md page marked red, like this modification
login interface
index.jsp the login screen a little bit copied (familiarize yourself, modify the appropriate place)
Log in to achieve the background
Write entity class User
package com.example.my.shop.entity;
public class User {
private String email;
private String password;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"email='" + email + '\'' +
", password='" + password + '\'' +
'}';
}
}
写UserDao
package com.example.my.shop.dao;
import com.example.my.shop.entity.User;
public interface UserDao {
public User getUser(String email, String password);
}
Write UserDaoImpl (no connection to the database)
package com.example.my.shop.dao.impl;
import com.example.my.shop.dao.UserDao;
import com.example.my.shop.entity.User;
public class UserDaoImpl implements UserDao {
public User getUser(String email, String password) {
User user=null;
if("[email protected]".equals(email)){
if("admin".equals(password))
user=new User();
user.setEmail(email);
user.setPassword(password);
}
return user;
}
}
Write UserService
package com.example.my.shop.service;
import com.example.my.shop.entity.User;
public interface UserService {
public User login(String email,String password);
}
Write UserServiceImpl (injection service) spring configuration not obtain bean, not the cause, the odd strange
package com.example.my.shop.service.impl;
import com.example.my.shop.commons.context.SpringContext;
import com.example.my.shop.dao.UserDao;
import com.example.my.shop.dao.impl.UserDaoImpl;
import com.example.my.shop.entity.User;
import com.example.my.shop.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UserServiceImpl implements UserService {
/* SpringContext context=new SpringContext();
UserDao userDao=(UserDao) context.getBean("userDao");*/
// ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
// UserDao userDao=(UserDao) context.getBean("userDao");
UserDao userDao=new UserDaoImpl();
public User login(String email, String password) {
return userDao.getUser(email,password);
}
}
spring-context.xml defined bean
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.example.my.shop.dao.impl.UserDaoImpl" />
<bean id="userService" class="com.example.my.shop.service.impl.UserServiceImpl"/>
</beans>
Write LoginController (actually sevlet) spring configuration not obtain bean, not the cause, the odd strange
package com.example.my.shop.Controller;
import com.example.my.shop.commons.context.SpringContext;
import com.example.my.shop.entity.User;
import com.example.my.shop.service.UserService;
import com.example.my.shop.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "LoginController", urlPatterns = "/login")
public class LoginController extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resq) throws ServletException, IOException {
}
protected void doPost(HttpServletRequest req, HttpServletResponse resq) throws ServletException, IOException{
// ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
/* SpringContext context=new SpringContext();
UserService userService=(UserService) context.getBean("userService");*/
UserService userService=new UserServiceImpl();
String email=req.getParameter("email");
String password = req.getParameter("password");
User user=userService.login(email,password);
//登录失败的处理
if(user==null){
req.setAttribute("message","用户名或密码错误");
req.getRequestDispatcher("/index.jsp").forward(req,resq);
}
//登录成功的处理
else{
resq.sendRedirect("/main.jsp");
}
}
}
You can run, but there is a problem is to obtain less than bean, can not find the reason, now is the new object
After completion next spring configuration of the web can get bean, strange