码迷,mamicode.com
首页 > 编程语言 > 详细

Spring综合小练习 - 登陆系统

时间:2015-06-26 00:15:50      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:

需求:

  用户登录,如果登陆成功,则更新用户信息与日志信息

图:

  技术分享技术分享技术分享


 

数据库

 1 CREATE TABLE `t_user` (
 2   `user_id` int(11) NOT NULL AUTO_INCREMENT,
 3   `user_name` varchar(30) DEFAULT NULL,
 4   `credits` int(11) DEFAULT NULL,
 5   `password` varchar(32) DEFAULT NULL,
 6   `last_visit` datetime DEFAULT NULL,
 7   `last_ip` varchar(23) DEFAULT NULL,
 8   PRIMARY KEY (`user_id`)
 9 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
10 
11 CREATE TABLE `t_login_log` (
12   `login_log_id` int(11) NOT NULL AUTO_INCREMENT,
13   `user_id` int(11) DEFAULT NULL,
14   `ip` varchar(23) DEFAULT NULL,
15   `login_datetime` datetime DEFAULT NULL,
16   PRIMARY KEY (`login_log_id`)
17 ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
18 
19 INSERT INTO t_user (user_name,password) 
20              VALUES(admin,123456);
21 COMMIT;

程序目录结构

技术分享

依赖jar包

  Tomcat8

  JDK8
 aspectj-1.8.2.jar
 aspectjrt.jar
 aspectjweaver-1.6.12.jar
 cglib-nodep-2.1_3.jar
 commons-dbcp-1.4.jar
 commons-logging-1.2.jar
 commons-pool.jar
 jstl-1.2.jar
 jstl-impl.jar
 junit-4.4.jar
 mysql-connector-java-5.1.7-bin.jar
 spring-web-2.5.jar
 spring-webmvc-2.5.jar
 spring.jar
 standard-1.1.2.jar


 

Domain对象

技术分享
 1 package com.baobaotao.domain;
 2 
 3 import java.io.Serializable;
 4 import java.util.Date;
 5 
 6 /**
 7  * Created by sherry on 000025/6/25 0:12.
 8  */
 9 public class User implements Serializable {
10     private int userId;
11     private String userName;
12     private String password;
13     private int credits;
14     private String lastIp;
15     private Date lastVisit;
16 
17     public int getUserId() {
18         return userId;
19     }
20 
21     public void setUserId(int userId) {
22         this.userId = userId;
23     }
24 
25     public String getPassword() {
26         return password;
27     }
28 
29     public void setPassword(String password) {
30         this.password = password;
31     }
32 
33     public int getCredits() {
34         return credits;
35     }
36 
37     public void setCredits(int credits) {
38         this.credits = credits;
39     }
40 
41     public String getLastIp() {
42         return lastIp;
43     }
44 
45     public void setLastIp(String lastIp) {
46         this.lastIp = lastIp;
47     }
48 
49     public Date getLastVisit() {
50         return lastVisit;
51     }
52 
53     public void setLastVisit(Date lastVisit) {
54         this.lastVisit = lastVisit;
55     }
56 
57     public String getUserName() {
58         return userName;
59     }
60 
61     public void setUserName(String userName) {
62         this.userName = userName;
63     }
64 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\domain\User.java
技术分享
 1 package com.baobaotao.domain;
 2 
 3 import java.io.Serializable;
 4 import java.util.Date;
 5 
 6 /**
 7  * Created by sherry on 000025/6/25 0:14.
 8  */
 9 public class LoginLog implements Serializable {
10     private int loginLogId;
11     private int userId;
12     private String ip;
13     private Date loginDate;
14 
15     public int getLoginLogId() {
16         return loginLogId;
17     }
18 
19     public void setLoginLogId(int loginLogId) {
20         this.loginLogId = loginLogId;
21     }
22 
23     public int getUserId() {
24         return userId;
25     }
26 
27     public void setUserId(int userId) {
28         this.userId = userId;
29     }
30 
31     public String getIp() {
32         return ip;
33     }
34 
35     public void setIp(String ip) {
36         this.ip = ip;
37     }
38 
39     public Date getLoginDate() {
40         return loginDate;
41     }
42 
43     public void setLoginDate(Date loginDate) {
44         this.loginDate = loginDate;
45     }
46 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\domain\LoginLog.java

Dao接口设计

技术分享
 1 package com.baobaotao.dao;
 2 
 3 import com.baobaotao.domain.User;
 4 
 5 /**
 6  * Created by sherry on 000025/6/25 0:15.
 7  */
 8 public interface UserDao {
 9     /**
10      * 获取用户数目
11      * @param userName 用户名
12      * @param password 密码
13      * @return 用户数
14      */
15     int getMatchCount(String userName,String password);
16 
17     /**
18      * 获取用户
19      * @param userName 姓名
20      * @return 用户
21      */
22     User findUserByUserName(String userName);
23 
24     /**
25      * 更新用户信息 积分 最后登录ip 最后登录时间
26      * @param user 用户
27      */
28     void updateLoginInfo(User user);
29 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\dao\UserDao.java
技术分享
 1 package com.baobaotao.dao;
 2 
 3 import com.baobaotao.domain.LoginLog;
 4 
 5 /**
 6  * Created by sherry on 000025/6/25 0:33.
 7  */
 8 public interface LoginLogDao {
 9     void insertLoginLog(LoginLog loginLog);
10 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\dao\LoginLogDao.java

Dao接口实现

技术分享
 1 package com.baobaotao.dao.jdbc;
 2 
 3 import com.baobaotao.dao.UserDao;
 4 import com.baobaotao.domain.User;
 5 import org.springframework.jdbc.core.JdbcTemplate;
 6 import org.springframework.jdbc.core.RowCallbackHandler;
 7 
 8 import javax.annotation.Resource;
 9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.sql.Types;
12 
13 /**
14  * Created by sherry on 000025/6/25 0:17.
15  */
16 public class UserDaoImpl implements UserDao {
17     //Spring的JDBC模板
18     @Resource
19     private JdbcTemplate jdbcTemplate;
20     /**
21      * 获取用户数目
22      *
23      * @param userName 用户名
24      * @param password 密码
25      * @return 用户数
26      */
27     @Override
28     public int getMatchCount(String userName, String password) {
29         String sqlStr = " SELECT count(1) FROM t_user " +
30                 " WHERE user_name = ? AND password = ? ";
31         int i = jdbcTemplate.queryForInt(sqlStr,new Object[]{userName,password}, new int[]{Types.VARCHAR,Types.VARCHAR});
32         return i;
33     }
34 
35     /**
36      * 获取用户
37      *
38      * @param userName 姓名
39      * @return 用户
40      */
41     @Override
42     public User findUserByUserName(String userName) {
43         String sqlStr = " SELECT user_id " +
44                 " ,user_name " +
45                 " ,credits " +
46                 " FROM t_user " +
47                 " WHERE user_name = ? ";
48         final User user = new User();
49 
50         jdbcTemplate.query(sqlStr, new Object[]{userName}, new RowCallbackHandler() {
51             @Override
52             public void processRow(ResultSet resultSet) throws SQLException {
53                 user.setUserId(resultSet.getInt("user_id"));
54                 user.setUserName(resultSet.getString("user_name"));
55                 user.setCredits(resultSet.getInt("credits"));
56             }
57         });
58 
59         return user;
60     }
61 
62     /**
63      * 更新用户信息 积分 最后登录ip 最后登录时间
64      *
65      * @param user 用户
66      */
67     @Override
68     public void updateLoginInfo(User user) {
69         String sqlStr = " UPDATE t_user " +
70                 " SET last_visit = ?,credits = ?,last_ip = ?" +
71                 " WHERE user_id = ? ";
72         jdbcTemplate.update(sqlStr,new Object[]{user.getLastVisit(),user.getCredits(),user.getLastIp(),user.getUserId()});
73     }
74 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\dao\jdbc\UserDaoImpl.java
技术分享
 1 package com.baobaotao.dao.jdbc;
 2 
 3 import com.baobaotao.dao.LoginLogDao;
 4 import com.baobaotao.domain.LoginLog;
 5 import org.springframework.jdbc.core.JdbcTemplate;
 6 
 7 import javax.annotation.Resource;
 8 
 9 /**
10  * Created by sherry on 000025/6/25 0:34.
11  */
12 public class LoginLogDaoImpl implements LoginLogDao {
13     @Resource
14     private JdbcTemplate jdbcTemplate;
15 
16     @Override
17     public void insertLoginLog(LoginLog loginLog) {
18         String sqlStr = " INSERT INTO t_login_log(user_id,ip,login_datetime) " +
19                 " VALUE(?,?,?) ";
20         jdbcTemplate.update(sqlStr,new Object[]{loginLog.getUserId(),loginLog.getIp(),loginLog.getLoginDate()});
21     }
22 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\dao\jdbc\LoginLogDaoImpl.java

Service接口

技术分享
 1 package com.baobaotao.service;
 2 
 3 import com.baobaotao.domain.User;
 4 
 5 /**
 6  * Created by sherry on 000025/6/25 0:57.
 7  */
 8 public interface UserService {
 9     boolean hasMatchUser(String userName,String password);
10     User findUserByUserName(String userName);
11     void loginSuccess(User user);
12 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\service\UserService.java

Service实现

技术分享
 1 package com.baobaotao.service;
 2 
 3 import com.baobaotao.dao.LoginLogDao;
 4 import com.baobaotao.dao.UserDao;
 5 import com.baobaotao.domain.LoginLog;
 6 import com.baobaotao.domain.User;
 7 import org.springframework.transaction.annotation.Transactional;
 8 
 9 import javax.annotation.Resource;
10 
11 /**
12  * Created by sherry on 000025/6/25 0:58.
13  */
14 @Transactional(rollbackFor = Exception.class)
15 public class UserServiceImpl implements UserService {
16     @Resource
17     private UserDao userDao;
18     @Resource
19     private LoginLogDao loginLogDao;
20 
21     @Override
22     @Transactional(readOnly = true)
23     public boolean hasMatchUser(String userName, String password) {
24         int matchCount = userDao.getMatchCount(userName,password);
25         return matchCount > 0;
26     }
27 
28     @Override
29     @Transactional(readOnly = true)
30     public User findUserByUserName(String userName) {
31         User user = userDao.findUserByUserName(userName);
32         return user;
33     }
34 
35     @Override
36     public void loginSuccess(User user) {
37         user.setCredits(user.getCredits() + 5);
38         LoginLog loginLog = new LoginLog();
39         loginLog.setUserId(user.getUserId());
40         loginLog.setIp(user.getLastIp());
41         loginLog.setLoginDate(user.getLastVisit());
42         userDao.updateLoginInfo(user);
43         loginLogDao.insertLoginLog(loginLog);
44     }
45 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\service\UserServiceImpl.java

配置Dao

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:tx="http://www.springframework.org/schema/tx"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 7        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
 8        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 9 
10     <!--注解注入-->
11     <context:annotation-config/>
12     <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
13         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
14         <property name="url" value="jdbc:mysql://localhost:3306/Lesson8_Spring2_Demo1"/>
15         <property name="username" value="root"/>
16         <property name="password" value="123456"/>
17     </bean>
18     <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
19         <property name="dataSource" ref="dataSource"/>
20     </bean>
21 
22     <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
23         <property name="dataSource" ref="dataSource"/>
24     </bean>
25 
26     <!--使用注解配置事务-->
27     <tx:annotation-driven transaction-manager="txManager"/>
28 
29     <bean id="userDao" class="com.baobaotao.dao.jdbc.UserDaoImpl"/>
30     <bean id="loginLogDao" class="com.baobaotao.dao.jdbc.LoginLogDaoImpl"/>
31 </beans>
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\baobaotao-dao.xml

配置Service

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 6        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 7 
 8     <!--注解注入-->
 9     <context:annotation-config/>
10     <bean id="userService" class="com.baobaotao.service.UserServiceImpl"/>
11 </beans>
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\baobaotao-service.xml

组合Dao与Service

技术分享
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
5 
6     <import resource="baobaotao-dao.xml"/>
7     <import resource="baobaotao-service.xml"/>
8 
9 </beans>
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\baobaotao.xml

日志配置

技术分享
 1 ## LOGGERS ##
 2 #define a logger 
 3 log4j.rootLogger=DEBUG,console,file
 4 #log4j.rootLogger=INFO,console
 5 ## APPENDERS ##
 6 
 7 log4j.appender.console=org.apache.log4j.ConsoleAppender
 8 # define an appender named file, which is set to be a RollingFileAppender
 9 log4j.appender.file=org.apache.log4j.RollingFileAppender
10 #set the logs size
11 log4j.appender.file.MaxFileSize=1000KB
12 log4j.appender.file.MaxBackupIndex=20
13 ## LAYOUTS ##
14 # assign a SimpleLayout to console appender
15 log4j.appender.console.layout=org.apache.log4j.SimpleLayout
16 # assign a PatternLayout to file appender
17 log4j.appender.file.layout=org.apache.log4j.PatternLayout
18 # For debug
19 # log4j.appender.file.layout.ConversionPattern=[%-5p][%t][%C][%d{yyyy-MM-dd HH:mm:ss}] %m%n
20 # For deployment
21 log4j.appender.file.layout.ConversionPattern=[%-5p][%d{yyyy-MM-dd HH:mm:ss}] %m%n
22 # show SQL Where ibatis run
23 log4j.logger.com.ibatis=DEBUG
24 log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
25 log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
26 log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
27 log4j.logger.java.sql.Connection=DEBUG
28 log4j.logger.java.sql.Statement=DEBUG
29 log4j.logger.java.sql.PreparedStatement=DEBUG
30 #打印JdbcTemplate执行的SQL
31 log4j.logger.org.springframework.jdbc.core.JdbcTemplate=debug
32 
33 #hibernate 配置
34 #log4j.appender.stdout.Threshold=trace
35 log4j.category.org.hibernate.SQL=trace
36 log4j.category.org.hibernate.type=trace
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\log4j.properties

测试Service

技术分享
 1 package com.baobaotao.service;
 2 
 3 import com.baobaotao.domain.User;
 4 import org.junit.*;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 import java.util.Date;
 9 
10 import static org.junit.Assert.*;
11 
12 public class UserServiceTest {
13 
14     private static ApplicationContext applicationContext;
15     private static final String[] CONFIG_FILES = {"baobaotao.xml"};
16     private UserService userService;
17 
18     @BeforeClass
19     public static void setUpBeforeClass() throws Exception{
20         applicationContext = new ClassPathXmlApplicationContext(CONFIG_FILES);
21     }
22 
23     @AfterClass
24     public static void ternDownAfterClass() throws Exception{
25 
26     }
27 
28     @Before
29     public void setUp() throws Exception {
30         userService = (UserService)applicationContext.getBean("userService");
31     }
32 
33     @After
34     public void tearDown() throws Exception {
35 
36     }
37 
38     @Test
39     public void testHasMatchUser() throws Exception {
40         boolean b1 = userService.hasMatchUser("admin","123456");
41         boolean b2 = userService.hasMatchUser("admin","1234");
42         assertTrue(b1);
43         assertTrue(!b2);
44     }
45 
46     @Test
47     public void testFindUserByUserName() throws Exception {
48         User user = userService.findUserByUserName("admin");
49         assertEquals(user.getUserName(),"admin");
50     }
51 
52     @Test
53     public void testLoginSuccess() throws Exception {
54         User user = userService.findUserByUserName("admin");
55         user.setUserId(1);
56         user.setUserName("admin");
57         user.setLastIp("192.168.1.6");
58         user.setLastVisit(new Date());
59         userService.loginSuccess(user);
60     }
61 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\test\com\baobaotao\service\UserServiceTest.java

开发视图层

  控制器

技术分享
 1 package com.baobaotao.web;
 2 
 3 import com.baobaotao.domain.User;
 4 import com.baobaotao.service.UserService;
 5 import org.springframework.validation.BindException;
 6 import org.springframework.web.servlet.ModelAndView;
 7 import org.springframework.web.servlet.mvc.AbstractCommandController;
 8 
 9 import javax.annotation.Resource;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12 import java.util.Date;
13 
14 /**
15  * Created by sherry on 000025/6/25 21:18.
16  */
17 public class LoginController extends AbstractCommandController {
18     @Resource
19     private UserService userService;
20 
21     public LoginController(){
22         setCommandClass(LoginCommand.class);
23     }
24 
25     @Override
26     protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception {
27         LoginCommand loginCommand = (LoginCommand) command;
28         boolean isValidateUser = userService.hasMatchUser(loginCommand.getUserName(),loginCommand.getPassword());
29         if (!isValidateUser){
30             return new ModelAndView("login","error","用户名或密码错误");
31         }else {
32             User user = userService.findUserByUserName(loginCommand.getUserName());
33             user.setLastIp(request.getLocalAddr());
34             user.setLastVisit(new Date());
35             userService.loginSuccess(user);
36             request.getSession().setAttribute("user",user);
37             return new ModelAndView("main");
38         }
39     }
40 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\web\LoginController.java
技术分享
 1 package com.baobaotao.web;
 2 
 3 /**
 4  * Created by sherry on 000025/6/25 21:19.
 5  */
 6 public class LoginCommand {
 7     private String userName;
 8     private String password;
 9 
10     public String getUserName() {
11         return userName;
12     }
13 
14     public void setUserName(String userName) {
15         this.userName = userName;
16     }
17 
18     public String getPassword() {
19         return password;
20     }
21 
22     public void setPassword(String password) {
23         this.password = password;
24     }
25 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\web\LoginCommand.java
技术分享
 1 package com.baobaotao.web;
 2 
 3 import org.springframework.web.servlet.ModelAndView;
 4 import org.springframework.web.servlet.mvc.AbstractController;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 /**
10  * Created by sherry on 000025/6/25 21:37.
11  */
12 public class LoginPage extends AbstractController {
13     /**
14      * Template method. Subclasses must implement this.
15      * The contract is the same as for <code>handleRequest</code>.
16      *
17      * @param request
18      * @param response
19      * @see #handleRequest
20      */
21     @Override
22     protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
23 
24         return new ModelAndView("login");
25     }
26 }
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\src\com\baobaotao\web\LoginPage.java

配置控制器

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 6        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
 7 
 8     <import resource="classpath:baobaotao.xml"/>
 9     <!--注解注入-->
10     <context:annotation-config/>
11     <!--控制器-->
12     <bean name="loginController" class="com.baobaotao.web.LoginController" />
13     <bean name="loginPage" class="com.baobaotao.web.LoginPage"/>
14     <!--URL与控制器映射-->
15     <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
16         <property name="mappings">
17             <props>
18                 <!--请求地址 - 控制器-->
19                 <prop key="/loginController.html">loginController</prop>
20                 <prop key="/index.html">loginPage</prop>
21             </props>
22         </property>
23     </bean>
24 
25     <!--视图解析规则-->
26     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
27         <property name="prefix"><!--视图名前缀-->
28             <value>/WEB-INF/jsp/</value>
29         </property>
30         <property name="suffix"><!--视图名后缀-->
31             <value>.jsp</value>
32         </property>
33     </bean>
34 
35 </beans>
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\web\WEB-INF\baobaotao-servlet.xml

页面开发

技术分享
 1 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 2 <%--
 3   Created by IntelliJ IDEA.
 4   User: nbcoo_000
 5   Date: 000025/6/25
 6   Time: 21:30
 7   To change this template use File | Settings | File Templates.
 8 --%>
 9 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
10 <%
11     String homePage = request.getContextPath();
12 %>
13 <!DOCTYPE html>
14 <html>
15 <head>
16     <title>宝宝淘论坛登录</title>
17 
18 </head>
19 <body>
20     <c:if test="${!empty error}">
21         <font color="red"><c:out value="${error}"/></font>
22     </c:if>
23 
24     <form action="<c:url value=‘/loginController.html‘/>" method="post">
25         <table>
26             <caption>用户登录</caption>
27             <tr>
28                 <td>用户名</td>
29                 <td><input type="text" name="userName" /></td>
30             </tr>
31             <tr>
32                 <td>密码</td>
33                 <td><input type="password" name="password" /></td>
34             </tr>
35             <tr align="right">
36                 <td colspan="2">
37                     <input type="submit" value="登录" />
38                     <input type="reset" value="重置" />
39                 </td>
40             </tr>
41         </table>
42     </form>
43 </body>
44 </html>
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\web\WEB-INF\jsp\login.jsp
技术分享
 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: nbcoo_000
 4   Date: 000025/6/25
 5   Time: 21:39
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
 9 <%
10     String homePage = request.getContextPath();
11 %>
12 <!DOCTYPE html>
13 <html>
14 <head>
15     <title>宝宝淘论坛</title>
16 
17 </head>
18 <body>
19     ${user.userName},欢迎您进入宝宝淘论坛,您当前积分为${user.credits};
20 </body>
21 </html>
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\web\WEB-INF\jsp\main.jsp

启动Spring

技术分享
<?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_3_1.xsd"
         version="3.1">
    <!--初始化Spring-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:baobaotao.xml</param-value>
    </context-param>

    <!--<context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>-->

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

    <servlet>
        <servlet-name>baobaotao</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>baobaotao</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>
</web-app>
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\web\WEB-INF\web.xml

默认主页

技术分享
 1 <%--
 2   Created by IntelliJ IDEA.
 3   User: nbcoo_000
 4   Date: 000024/6/24
 5   Time: 23:51
 6   To change this template use File | Settings | File Templates.
 7 --%>
 8 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
 9 <%
10     String homePage = request.getContextPath()+"/index.html";
11     System.out.println("homePage:"+homePage);
12 %>
13 <script type="text/javascript">
14     window.location.href="<%=homePage%>";
15 </script>
D:\GitHub\tools\JavaEEDevelop\Lesson8_Spring2_Demo1\web\index.jsp

 

Spring综合小练习 - 登陆系统

标签:

原文地址:http://www.cnblogs.com/sherrykid/p/4601180.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!