pom.xml 配置文件



1 <project xmlns="http://maven.apache.org/POM/4.0.0"
  2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5     <groupId>com.xiwi</groupId>
  6     <artifactId>ssm</artifactId>
  7     <packaging>war</packaging>
  8     <version>0.0.1-SNAPSHOT</version>
  9     <name>ssm Maven Webapp</name>
 10     <url>http://maven.apache.org</url>
 11 
 12     <properties>
 13         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 14         <log4j.version>1.2.17</log4j.version>
 15         <spring.version>4.3.9.RELEASE</spring.version>
 16         <mybatis.version>3.2.2</mybatis.version>
 17     </properties>
 18 
 19     <dependencies>
 20         
 21         <!-- spring -->
 22         <dependency>
 23             <groupId>org.springframework</groupId>
 24             <artifactId>spring-aop</artifactId>
 25             <version>${spring.version}</version>
 26         </dependency>
 27         <dependency>
 28             <groupId>org.springframework</groupId>
 29             <artifactId>spring-aspects</artifactId>
 30             <version>${spring.version}</version>
 31         </dependency>
 32         <dependency>
 33             <groupId>org.springframework</groupId>
 34             <artifactId>spring-beans</artifactId>
 35             <version>${spring.version}</version>
 36         </dependency>
 37         <dependency>
 38             <groupId>org.springframework</groupId>
 39             <artifactId>spring-context</artifactId>
 40             <version>${spring.version}</version>
 41         </dependency>
 42         <dependency>
 43             <groupId>org.springframework</groupId>
 44             <artifactId>spring-context-support</artifactId>
 45             <version>${spring.version}</version>
 46         </dependency>
 47         <dependency>
 48             <groupId>org.springframework</groupId>
 49             <artifactId>spring-expression</artifactId>
 50             <version>${spring.version}</version>
 51         </dependency>
 52         <dependency>
 53             <groupId>org.springframework</groupId>
 54             <artifactId>spring-instrument</artifactId>
 55             <version>${spring.version}</version>
 56         </dependency>
 57         <dependency>
 58             <groupId>org.springframework</groupId>
 59             <artifactId>spring-instrument-tomcat</artifactId>
 60             <version>${spring.version}</version>
 61         </dependency>
 62         <dependency>
 63             <groupId>org.springframework</groupId>
 64             <artifactId>spring-jdbc</artifactId>
 65             <version>${spring.version}</version>
 66         </dependency>
 67         <dependency>
 68             <groupId>org.springframework</groupId>
 69             <artifactId>spring-jms</artifactId>
 70             <version>${spring.version}</version>
 71         </dependency>
 72         <dependency>
 73             <groupId>org.springframework</groupId>
 74             <artifactId>spring-messaging</artifactId>
 75             <version>${spring.version}</version>
 76         </dependency>
 77         <dependency>
 78             <groupId>org.springframework</groupId>
 79             <artifactId>spring-orm</artifactId>
 80             <version>${spring.version}</version>
 81         </dependency>
 82         <dependency>
 83             <groupId>org.springframework</groupId>
 84             <artifactId>spring-oxm</artifactId>
 85             <version>${spring.version}</version>
 86         </dependency>
 87         <dependency>
 88             <groupId>org.springframework</groupId>
 89             <artifactId>spring-test</artifactId>
 90             <version>${spring.version}</version>
 91         </dependency>
 92         <dependency>
 93             <groupId>org.springframework</groupId>
 94             <artifactId>spring-tx</artifactId>
 95             <version>${spring.version}</version>
 96         </dependency>
 97         <dependency>
 98             <groupId>org.springframework</groupId>
 99             <artifactId>spring-web</artifactId>
100             <version>${spring.version}</version>
101         </dependency>
102         <dependency>
103             <groupId>org.springframework</groupId>
104             <artifactId>spring-webmvc</artifactId>
105             <version>${spring.version}</version>
106         </dependency>
107         <dependency>
108             <groupId>org.springframework</groupId>
109             <artifactId>spring-webmvc-portlet</artifactId>
110             <version>${spring.version}</version>
111         </dependency>
112         <dependency>
113             <groupId>org.springframework</groupId>
114             <artifactId>spring-websocket</artifactId>
115             <version>${spring.version}</version>
116         </dependency>
117         
118         <!-- mybatis -->
119         <dependency>
120             <groupId>org.mybatis</groupId>
121             <artifactId>mybatis</artifactId>
122             <version>${mybatis.version}</version>
123         </dependency>
124         <dependency>
125             <groupId>org.mybatis</groupId>
126             <artifactId>mybatis-spring</artifactId>
127             <version>1.2.2</version>
128         </dependency>
129         
130         <!-- 阿里数据库连接池 下载地址http://www.mvnrepository.com/artifact/com.alibaba/druid-->
131       <dependency>
132         <groupId>com.alibaba</groupId>
133         <artifactId>druid</artifactId>
134         <version>1.1.10</version>
135       </dependency>
136         
137         <!-- 上传下载 -->
138         <dependency>
139             <groupId>commons-fileupload</groupId>
140             <artifactId>commons-fileupload</artifactId>
141             <version>1.3.1</version>
142         </dependency>
143         <dependency>
144             <groupId>commons-io</groupId>
145             <artifactId>commons-io</artifactId>
146             <version>2.2</version>
147         </dependency>
148         <dependency>
149             <groupId>taglibs</groupId>
150             <artifactId>standard</artifactId>
151             <version>1.1.2</version>
152         </dependency>
153 
154         <!-- javax api -->
155         <dependency>
156             <groupId>javax</groupId>
157             <artifactId>javaee-api</artifactId>
158             <version>8.0</version>
159             <scope>provided</scope>
160         </dependency>
161         <dependency>
162             <groupId>javax</groupId>
163             <artifactId>javaee-web-api</artifactId>
164             <version>8.0</version>
165             <scope>provided</scope>
166         </dependency>
167         
168         <!-- https://mvnrepository.com/artifact/org.json/json -->
169         <dependency>
170             <groupId>org.json</groupId>
171             <artifactId>json</artifactId>
172             <version>20180813</version>
173         </dependency>
174 
175         
176         <dependency>
177             <groupId>javax.servlet</groupId>
178             <artifactId>jstl</artifactId>
179             <version>1.2</version>
180         </dependency>
181         
182         <dependency>
183             <groupId>mysql</groupId>
184             <artifactId>mysql-connector-java</artifactId>
185             <version>5.1.41</version>
186         </dependency>
187         
188     </dependencies>
189 
190 
191     <build>
192         <finalName>ssm</finalName>
193         <plugins>
194 
195             <plugin>
196                 <groupId>org.apache.maven.plugins</groupId>
197                 <artifactId>maven-compiler-plugin</artifactId>
198                 <configuration>
199                     <source>1.8</source>
200                     <target>1.8</target>
201                 </configuration>
202             </plugin>
203 
204             <!-- jetty插件 不需要可移除 -->
205             <plugin>
206                 <groupId>org.eclipse.jetty</groupId>
207                 <artifactId>jetty-maven-plugin</artifactId>
208                 <version>9.3.7.v20160115</version>
209             </plugin>
210 
211         </plugins>
212     </build>
213 
214 
215 </project>



 

Web.xml 配置文件

 




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 6     version="3.0">
 7 
 8     <welcome-file-list>
 9         <welcome-file>index.jsp</welcome-file>
10     </welcome-file-list>
11 
12     <!-- 监听配置 -->
13     <listener>
14         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
15     </listener>
16 
17     <!-- 加载Spring以及Mybatis配置 以下 *星号 是做了通配处理 -->
18     <context-param>
19         <param-name>contextConfigLocation</param-name>
20         <param-value>/WEB-INF/configs/spring-mvc*.xml</param-value>
21     </context-param>
22 
23     <!-- 也可使用多个 context-param标签
24     <context-param>
25         <param-name>contextConfigLocation</param-name>
26         <param-value>classpath:/spring-mvc.xml</param-value>
27     </context-param>
28     <context-param>
29         <param-name>contextConfigLocation</param-name>
30         <param-value>classpath:/spring-mybatis.xml</param-value>
31     </context-param>
32      -->
33 
34 
35     <!-- 配置文件加载 -->
36     <servlet>
37         <servlet-name>dispatcherServlet</servlet-name>
38         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
39         <init-param>
40             <param-name>contextConfigLocation</param-name>
41             <param-value>/WEB-INF/configs/spring/spring-*.xml</param-value>
42         </init-param>
43         <load-on-startup>1</load-on-startup>
44     </servlet>
45 
46     <!-- 拦截请求 /斜杠代表全部 也可以换成 .do代表所有 .do的请求都会被Spring拦截器拦截 -->
47     <servlet-mapping>
48         <servlet-name>dispatcherServlet</servlet-name>
49         <url-pattern>/</url-pattern>
50     </servlet-mapping>
51 
52     <!-- 编码过滤器 -->
53     <filter>
54         <filter-name>characterEncoding</filter-name>
55         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
56         <init-param>
57             <param-name>encoding</param-name>
58             <param-value>UTF-8</param-value>
59         </init-param>
60     </filter>
61     <filter-mapping>
62         <filter-name>characterEncoding</filter-name>
63         <url-pattern>/*</url-pattern>
64     </filter-mapping>
65 
66 </web-app>


web.xml文件


 

 

基于 web.xml 配置文件 项目的配置文件位置如下:

Java ssm框架如何配置释放数据库连接_测试_03

 

spring-mvc.xml 配置文件

 




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

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:mvc="http://www.springframework.org/schema/mvc" 
 6     xmlns:tx="http://www.springframework.org/schema/tx"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8 http://www.springframework.org/schema/beans/spring-beans.xsd
 9 http://www.springframework.org/schema/context
10 http://www.springframework.org/schema/context/spring-context.xsd
11 http://www.springframework.org/schema/mvc
12 http://www.springframework.org/schema/mvc/spring-mvc.xsd
13 http://www.springframework.org/schema/tx
14 http://www.springframework.org/schema/tx/spring-tx.xsd
15 ">
16 
17 <context:component-scan base-package="com.xiwi.controller"/>
18 <context:component-scan base-package="com.xiwi.service"/>
19 <context:component-scan base-package="com.xiwi.dao"/>
20 
21 <mvc:annotation-driven>
22     <!-- 消息转换器 -->
23     <mvc:message-converters register-defaults="true">
24       <bean class="org.springframework.http.converter.StringHttpMessageConverter">
25         <property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
26       </bean>
27     </mvc:message-converters>
28 </mvc:annotation-driven>
29 
30 <mvc:default-servlet-handler/>
31 
32 
33 
34 <!-- 对静态资源文件的访问 -->
35 <mvc:resources mapping="/resources/**" location="/resources1/" />
36 
37 <!-- 返回视图 -->
38 <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
39     <property name="prefix" value="/WEB-INF/jsp/"></property>
40     <property name="suffix" value=".jsp"></property>    
41 </bean>
42 
43 </beans>


spring-mvc文件


 

 

spring-mybatis.xml 配置文件




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

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:p="http://www.springframework.org/schema/p"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xsi:schemaLocation="http://www.springframework.org/schema/beans
 9     http://www.springframework.org/schema/beans/spring-beans.xsd
10     http://www.springframework.org/schema/context
11     http://www.springframework.org/schema/context/spring-context.xsd
12     http://www.springframework.org/schema/aop
13     http://www.springframework.org/schema/aop/spring-aop.xsd
14     http://www.springframework.org/schema/tx
15     http://www.springframework.org/schema/tx/spring-tx.xsd">
16 
17     <!-- 加载数据库配置 -->
18     <context:property-placeholder location="/WEB-INF/configs/mybatis/database.properties"/>
19 
20     <!-- 创建数据库,jdbc -->
21     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
22         <!-- 连接驱动 -->
23         <property name="driverClassName" value="${driverClass}"/>
24         <property name="url" value="${jdbcUrl}"/>
25         <property name="username" value="${user}"/>
26         <property name="password" value="${password}"/>
27     </bean>
28 
29     <!-- 配置jdbc的事物 -->
30     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
31         <property name="dataSource" ref="dataSource"></property>
32     </bean>
33 
34     <!-- 将配置好的事物加到驱动  :: 指定事务管理器 -->
35     <tx:annotation-driven transaction-manager="transactionManager"/>
36 
37     <!-- 获取sqlSessionFactory-->
38     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
39         <property name="dataSource" ref="dataSource" />
40         <!-- 此处直接指向指定的mapping文件, *表示匹配所有 -->
41         <property name="mapperLocations" value="classpath:com/xiwi/dao/*.xml"></property>
42     </bean>
43 
44 
45     <!-- 将指定包下面的map与mapping文件进行绑定 -->
46     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
47         <property name="basePackage" value="com.xiwi.dao"></property>
48         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
49     </bean>
50 </beans>


spring-mybatis文件


 

database.properties 配置文件




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

1 user=root
2 password=root
3 driverClass=com.mysql.jdbc.Driver
4 jdbcUrl=jdbc:mysql://127.0.0.1:3306/xiwi?useUnicode=true&characterEncoding=utf8


View Code


 

新建 Controller控制层 (包名必须要跟 "spring-mvc.xml"配置文件下的 context:component-scan标签的 base-package属性 值一样)




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

1 package com.xiwi.controller;
 2 
 3 import org.springframework.stereotype.Controller;
 4 import org.springframework.web.bind.annotation.RequestMapping;
 5 
 6 @Controller
 7 public class IndexController {
 8 
 9     @RequestMapping(value= {"/index", "/"})
10     public String index () {
11         // 浏览器输入 http://localhost:8080/index 或 http://localhost:8080/ 即可访问控制层
12          System.out.println("进入 IndexConreoller !");
13         return "index";
14     }
15 
16 }


IndexController


 

=========================================

访问成功后,以上是我们最基本的 搭好SpringMVC框架,接下来我们来了解如果在SpringMVC中使用 Mybatis框架对数据库进行CURL操作。

 

=========================================

 

来一个简单的 登录操作:

 

在 控制层 包下 新建一个 UserController.java (此时我们控制层包名为:com.xiwi.controller 可自行修改 也要修改spring-mvc.xml文件)

 




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

1 package com.xiwi.controller;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpSession;
 8 
 9 import org.springframework.beans.factory.annotation.Autowired;
10 import org.springframework.stereotype.Controller;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RequestMethod;
13 import org.springframework.web.bind.annotation.ResponseBody;
14 
15 import com.xiwi.entity.User;
16 import com.xiwi.service.UserService;
17 import com.xiwi.utils.JsonUtil;
18 
19 @Controller
20 public class UserController {
21     @Autowired
22     private UserService userService;
23 
24     @RequestMapping(value="/login", method=RequestMethod.GET)
25     public String login(HttpServletRequest request) {    return "login";        }
26     @RequestMapping(value="/login", method=RequestMethod.POST)
27     @ResponseBody
28     public Object login(HttpSession session, User u) {
29         
30         System.out.println("登录的账号信息: " + u.toString());
31         
32         String user = u.getUser();
33         String pwd = u.getPwd();
34 
35         System.out.println("user: " + user);
36         System.out.println("pwd: " + pwd);
37         
38         if (user==null || user.equals("") || pwd==null || pwd.equals("")) {
39             // 用户名 或 密码不得为空
40             return new JsonUtil().json(-1, "参数不得为空!").toString();
41         }
42         
43         // 验证用户
44         User result = userService.verify(u);
45         if (result!=null) {
46             // 存在账户 并且 账号密码正确
47             System.out.println("u.getId:" + result.getId());
48             System.out.println("u: " + result.toString());
49             session.setAttribute("userId", result.getId());
50             return new JsonUtil().json(1, "登录成功!").toString();
51         } else {
52             // 用户名或密码错误
53             return new JsonUtil().json(0, "用户名或密码错误!").toString();
54         }
55     } 
56     @RequestMapping(value="/outLogin", method=RequestMethod.GET)
57     public String outLogin(HttpSession session) {
58         session.removeAttribute("uid");
59         return "redirect:/login";    
60     }
61     
62     
63     
64     
65     @RequestMapping(value="/user", method=RequestMethod.GET)
66     public String user(HttpSession session) {
67     
68         if (!isLoginn(session)) {
69             // 未登录
70             System.out.println("user 未登录 已重定向 login页面");
71             return "redirect:/login";    
72         }
73         
74         return "user";    
75     }
76     
77     private boolean isLoginn(HttpSession session) {
78         String uid = (String)session.getAttribute("userId");
79         if (uid == null) {
80             return false;
81         } else {
82             return true;
83         }
84     }
85     
86 }


UserController.java


 

 

 

新建 service服务层 (此时,我们的service包名为:com.xiwi.service )

在 服务层 包下 新建 UserService.java




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

1 package com.xiwi.service;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import com.xiwi.dao.UserDao;
 9 import com.xiwi.entity.User;
10 
11 @Service
12 public class UserService {
13 
14     @Autowired(required=false)
15     private UserDao userDao;
16 
17     // 登录
18     public User verify (User user){
19         User u = userDao.findUser(user.getUser(), user.getPwd());
20         if (u!=null) {
21             return u;
22         } else {
23             return null;
24         }
25     }
26 
27 }


UserService.java


 

新建 Dao数据层 (此时,我们的Dao包名为:com.xiwi.dao )

在 数据层 包下 新建 UserDao.java

 



package com.xiwi.dao;

import com.xiwi.entity.User;

// 此时 Dao层的 类类型全部为 interface
public interface UserDao {

    // 根据 用户名,密码 查询用户
    public User findUser(@Param("user")String user, @Param("pwd")String pwd);

}



 

 

新建 实体层 (此时,我们的实体层包名为:com.xiwi.entity )

新建 实体类 User.java (实体层到底是什么意思呢? 通俗点理解就是 里面都是放了 数据库的各各表。而 实体类 就是一个表里面有哪些字段)




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

1 package com.xiwi.entity;
 2 
 3 public class User {
 4 
 5     private String Id;
 6     private String user;
 7     private String pwd;
 8     private String code_pwd;
 9     private String name;
10     private String type;
11     private String create_time;
12     
13     public User() {}
14     public User(String id, String user, String pwd, String code_pwd, String name, String type, String create_time) {
15         super();
16         Id = id;
17         this.user = user;
18         this.pwd = pwd;
19         this.code_pwd = code_pwd;
20         this.name = name;
21         this.type = type;
22         this.create_time = create_time;
23     }
24     
25     public String getId() {
26         return Id;
27     }
28     public void setId(String id) {
29         Id = id;
30     }
31     public String getUser() {
32         return user;
33     }
34     public void setUser(String user) {
35         this.user = user;
36     }
37     public String getPwd() {
38         return pwd;
39     }
40     public void setPwd(String pwd) {
41         this.pwd = pwd;
42     }
43     public String getCode_pwd() {
44         return code_pwd;
45     }
46     public void setCode_pwd(String code_pwd) {
47         this.code_pwd = code_pwd;
48     }
49     public String getName() {
50         return name;
51     }
52     public void setName(String name) {
53         this.name = name;
54     }
55     public String getType() {
56         return type;
57     }
58     public void setType(String type) {
59         this.type = type;
60     }
61     public String getCreate_time() {
62         return create_time;
63     }
64     public void setCreate_time(String create_time) {
65         this.create_time = create_time;
66     }
67     
68     @Override
69     public String toString() {
70         return "User [Id=" + Id + ", user=" + user + ", pwd=" + pwd + ", code_pwd=" + code_pwd + ", name=" + name
71                 + ", type=" + type + ", create_time=" + create_time + "]";
72     }
73 }


User.java


 

 

在 Dao数据层下 也就是 上面刚创建 UserDao.java 同级目录下 新建一个 UserMapper.xml (这是Mybatis要操作数据库需要的配置文件 详细路径更改 需在 spring-mybatis.xml 文件中修改)

 




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE mapper
 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 
 6 <!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离 
 7 注意:使用mapper代理方法开发,namespace有特殊重要的作用
 8 -->
 9 <mapper namespace="com.xiwi.dao.UserDao">
10 
11     <sql id="fields">Id,user,pwd,code_pwd,name,type,create_time</sql>
12 
13     <!-- 根据 user,pwd 查询用户 -->
14     <select id="findUser" parameterType="String" resultType="com.xiwi.entity.User">
15         SELECT <include refid="fields"></include> FROM user WHERE 
16             user = #{user} 
17             AND 
18             pwd = #{pwd} 
19             LIMIT 1;
20     </select> 
21 </mapper>


View Code


 

此时,我们的项目目录如下:

Java ssm框架如何配置释放数据库连接_json_20

 

 

 后端我们已经编写完毕,接下来编写一下前端代码。

login.jsp前端代码 




Java ssm框架如何配置释放数据库连接_json

Java ssm框架如何配置释放数据库连接_maven_02

1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>login</title>
 8 <script type="text/javascript" src="/resources/js/jquery.js"></script>
 9 </head>
10 <body>
11 
12     <table>
13         <tr>
14             <td>用户名: </td>
15             <td><input type="text" name="user"></td>
16         </tr>
17         <tr>
18             <td>密码: </td>
19             <td><input type="password" name="pwd"></td>
20         </tr>
21         <tr>
22             <td><button class="reg_but">注册</button></td>
23             <td><button class="ok_but">提交</button></td>
24         </tr>
25     </table>
26 
27 </body>
28 
29 <script type="text/javascript">
30     
31     $('.reg_but').click(function(){
32         window.location.href = '/register';
33     });
34 
35     // 提交 按钮
36     $('.ok_but').click(function(){
37         var user = $("input[name='user']").val().trim();
38         var pwd = $("input[name='pwd']").val().trim();
39         console.log(user);
40         console.log(pwd);
41 
42         if (user=='' || pwd=='') {
43             alert('参数不得为空!');
44         } else {
45 
46             // 因为我们后端是用 User类类型来接收参数,所以我们请求参数的时候需要和 实习类User 的成员变量名一致
47             $.ajax({
48             url : '/login',
49             type: 'POST',
50             data: {
51                 user: user,
52                 pwd: pwd
53             },
54             success: function(data){
55                 data = jQuery.parseJSON(data);
56                 console.log('data: ' + data);
57                 console.log('code: ' + data.code)
58                 if (data.code == 1) {
59                     // 登录成功
60                     window.location.href='/user';
61                 } else {
62                     alert(data.msg);
63                 }
64             },
65             error: function(error){
66                 console.log(error);
67             }
68         });
69 
70         }
71 
72 
73             
74     });
75 
76 </script>
77 
78 </html>


login.jsp


 

接下 访问 login.jsp 页面 输入账号密码 然后登录 即可访问 user.jsp 页面了 (user.jsp 前端代码并不需要编写什么判断是否登录,该判断的东西 我们都在 Controller控制层 做判断操作了 )