本文实例为大家分享了Spring,Spring MVC,MongoDB实现登录注册 的具体代码,供大家参考,具体内容如下
工程目录:
Spring配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
< beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:context = "http://www.springframework.org/schema/context" xmlns:mongo = "http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 开启自动注入,进行包扫描 --> < context:component-scan base-package = "com" /> < mongo:mongo host = "127.0.0.1" port = "27017" /> <!-- 配置MongoTemplate--> < bean id = "mongoTemplate" class = "org.springframework.data.document.mongodb.MongoTemplate" > < constructor-arg ref = "mongo" /> <!-- 指定数据库名字--> < constructor-arg name = "databaseName" value = "data1" /> </ bean > <!-- 映射转换器,扫描back-package目录下的文件,根据注释,把它们作为mongodb的一个collection的映射 --> < mongo:mapping-converter base-package = "com.model" /> <!--普通注入--> < bean id = "userDaoImpl" class = "com.userdaoimpl.UserDaoImpl" > < property name = "mongoTemplate" ref = "mongoTemplate" ></ property > </ bean > < context:annotation-config /> < bean class = "org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <!-- 配置视图解析器,将ModelAndView及字符串解析为具体的页面 --> < bean id = "viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name = "prefix" value = "/" /> < property name = "suffix" value = ".jsp" /> </ bean > </ beans > |
Web.xml:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app version = "3.0" 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_3_0.xsd"> < display-name ></ display-name > < welcome-file-list > < welcome-file >login.jsp</ welcome-file > </ welcome-file-list > < servlet > < servlet-name >spring</ servlet-name > < servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name >contextConfigLocation</ param-name > < param-value >classpath:applicationContext.xml</ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name >spring</ servlet-name > < url-pattern >/</ url-pattern > </ servlet-mapping > <!-- 统一字符编码 --> < filter > < filter-name >encoding</ filter-name > < filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > < init-param > < param-name >encoding</ param-name > < param-value >UTF-8</ param-value > </ init-param > </ filter > < filter-mapping > < filter-name >encoding</ filter-name > < url-pattern >*.do</ url-pattern > </ filter-mapping > </ web-app > |
Model层:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package com.model; import org.springframework.data.annotation.Id; import org.springframework.data.document.mongodb.mapping.Document; @Document (collection = "p3" ) public class User { @Id String name; String password; public String getName() { return name; } public void setName(String name) { this .name = name; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } } |
UserDao:
1
2
3
4
5
6
7
8
9
10
11
|
package com.userdao; import com.model.User; public interface UserDao { public void insert(User user); public boolean find(String name,String password); public void save(User user); } |
UserDaoImpl:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
package com.userdaoimpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.document.mongodb.MongoTemplate; import org.springframework.data.document.mongodb.query.Criteria; import org.springframework.data.document.mongodb.query.Query; import com.model.User; import com.userdao.UserDao; public class UserDaoImpl implements UserDao{ @Autowired MongoTemplate mongoTemplate; @Override public void insert(User user) { getMongoTemplate().insert(user); } @Override public boolean find(String name, String password) { Criteria criteria=Criteria.where( "name" ).is(name).and( "password" ).in(password); User user = getMongoTemplate().findOne( new Query(criteria), User. class ); if (user== null ) { return false ; } return true ; } @Override public void save(User user) { getMongoTemplate().save(user); } public MongoTemplate getMongoTemplate() { return mongoTemplate; } public void setMongoTemplate(MongoTemplate mongoTemplate) { this .mongoTemplate = mongoTemplate; } } |
LoginController:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package com.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.userdaoimpl.UserDaoImpl; @Controller public class LoginController { @Autowired UserDaoImpl userDaoImpl; @RequestMapping ( "/login" ) public String login(String name,String password,Model model) { boolean rs = userDaoImpl.find(name, password); if (rs== true ) { return "index" ; } return "erro" ; } public UserDaoImpl getUserDaoImpl() { return userDaoImpl; } public void setUserDaoImpl(UserDaoImpl userDaoImpl) { this .userDaoImpl = userDaoImpl; } } |
RegisterController:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package com.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.model.User; import com.userdaoimpl.UserDaoImpl; @Controller public class RegisterController { @Autowired UserDaoImpl userDaoImpl; @RequestMapping ( "/register" ) public String register(String name,String password,Model model) { User user = new User(); user.setName(name); user.setPassword(password); userDaoImpl.save(user); return "login" ; } public UserDaoImpl getUserDaoImpl() { return userDaoImpl; } public void setUserDaoImpl(UserDaoImpl userDaoImpl) { this .userDaoImpl = userDaoImpl; } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。