程序功能:在线管理书签,注册账号,帐号管理
架构方案:
(1)前段控制器:
Front Controller 采用前端控制器接受用户请求,由前段控制器决定由那一个具体的控制器来处理请求
(2)控制器
前段控制器决定好控制器后,将请求交由控制器处理,控制器截取用户请求参数,进行数据验证,并呼叫业
务层的对象完成请求
(3)Data,Mail,DAO等model对象
再请求中有一些业务必须调用业务层中的Model对象来处理,控制器调用数据存取对象(DAO),Mail等业
务对象来处理这些请求,如果需要的话,还可将一些消息封装为数据对象
(4)数据库
采用Mysql数据库
(5)网页
页面前段展示
采用技术:
(1)前段控制器:Spring MVC实现,前端控制器为DispatcherServlet类
(2)控制器:配合前段控制器,采用Spring MVC的Controller继承架构,如AbstractCommandController
SimpleFormController,MultiActionController
(3)Data,Mail.DAO等Model对象:采用Hibernate,Spring Java Mail抽象层封装
(4)页面:JS,Spring Tag,JSTL
Model设计
User:
package
model;
public
class
User
...
{
private Integer id;
private String email;
private String username;
private String passwd;
public Integer getId() ...{
return id;
}
public void setId(Integer id) ...{
this.id = id;
}
public String getEmail() ...{
return email;
}
public void setEmail(String email) ...{
this.email = email;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getPasswd() ...{
return passwd;
}
public void setPasswd(String passwd) ...{
this.passwd = passwd;
}
}
User类对应的数据表:
create
table
user
(id
int
(
11
)
not
null
primary
key
auto_increment,
username
varchar
(
16
),
passwd
char
(
16
)
not
null
,
email
varchar
(
16
)
not
null
);
User类对应的Hibernate映射文件 user.hbm.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<
hibernate-mapping
>
<
class
name
="model.User"
table
="usre"
>
<
id
name
="id"
column
="id"
>
<
generator
class
="native"
></
generator
>
</
id
>
<
property
name
="username"
column
="username"
></
property
>
<
property
name
="passwd"
column
="passwd"
></
property
>
<
property
name
="email"
column
="email"
></
property
>
</
class
>
</
hibernate-mapping
>
IUserDAO接口
package
model;
public
interface
IUserDAO
...
{
public void insert(User user);
public User findByName(String name);
public User findByEmail(String email);
public void update(User user);
}
UserDAO实现
package
model;
import
java.util.List;
import
org.springframework.orm.hibernate3.HibernateTemplate;
public
class
UserDAO
implements
IUserDAO
...
{
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() ...{
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) ...{
this.hibernateTemplate = hibernateTemplate;
}
public User findByEmail(String email) ...{
List users=hibernateTemplate.find("from User where user.email='"+email+"'");
return (User)users.get(0);
}
public User findByName(String name) ...{
List users=hibernateTemplate.find("from User where user.username='"+name+"'");
return (User)users.get(0);
}
public void insert(User user) ...{
hibernateTemplate.save(user);
}
public void update(User user) ...{
hibernateTemplate.update(user);
}
}
Bookmark:
package
model;
public
class
Bookmark
...
{
private Integer id;
private String username;
private String url;
public Integer getId() ...{
return id;
}
public void setId(Integer id) ...{
this.id = id;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getUrl() ...{
return url;
}
public void setUrl(String url) ...{
this.url = url;
}
}
Bookmark类对应的数据表:
create
table
bookmark(id
int
(
11
)
not
null
primary
key
auto_increment,
username
varchar
(
16
),
bm_url
char
(
16
)
not
null
);
Bookmark类对应的bookmark.hbm.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
<
hibernate-mapping
>
<
class
name
="model.Bookmark"
table
="bookmark"
>
<
id
name
="id"
column
="id"
>
<
generator
class
="native"
></
generator
>
</
id
>
<
property
name
="username"
column
="username"
></
property
>
<
property
name
="url"
column
="bm_URL"
></
property
>
</
class
>
</
hibernate-mapping
>
IBookmarkDAO接口
package
model;
import
java.util.List;
public
interface
IBookmarlDAO
...
{
public List findUserUrl(String username);
public void addUserUrl(String username,String url);
public void deleteUserUrls(String username,String[] urls);
}
BookmarkDAO实现
package
model;
import
java.util.List;
import
org.springframework.orm.hibernate3.HibernateTemplate;
public
class
BookmarkDAO
implements
IBookmarlDAO
...
{
private HibernateTemplate hibernateTemplate;
public HibernateTemplate getHibernateTemplate() ...{
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) ...{
this.hibernateTemplate = hibernateTemplate;
}
public void addUserUrl(String username, String url) ...{
Bookmark bm=new Bookmark();
bm.setUsername(username);
bm.setUrl(url);
hibernateTemplate.save(bm);
}
public void deleteUserUrls(String username, String[] urls) ...{
for (int i = 0; i < urls.length; i++) ...{
List list=hibernateTemplate.find("from Bookmark bm where (bm.username='"+username+"') and (bm.url='"+urls[i]+"')");
hibernateTemplate.delete(list.get(0));
}
}
public List findUserUrl(String username) ...{
List list=hibernateTemplate.find("select bm.url from bookmark as bm where bm.username='"+username+"'");
return list;
}
}
SpringMail类---当用户忘记登陆密码时候,可以要求他输入注册的email地,邮件告知用户密码
package
model;
import
org.springframework.mail.SimpleMailMessage;
import
org.springframework.mail.javamail.JavaMailSenderImpl;
public
class
SpringMail
...
{
private String smtpHost;
private String from;
public String getSmtpHost() ...{
return smtpHost;
}
public void setSmtpHost(String smtpHost) ...{
this.smtpHost = smtpHost;
}
public String getFrom() ...{
return from;
}
public void setFrom(String from) ...{
this.from = from;
}
public void sendPasswdMail(String email,String username,String password)...{
JavaMailSenderImpl javaMailSenderImpl=new JavaMailSenderImpl();
javaMailSenderImpl.setHost(this.getSmtpHost());
SimpleMailMessage mailMessage=new SimpleMailMessage();
mailMessage.setTo(email);
mailMessage.setFrom(this.getFrom());
mailMessage.setSubject("您的密码:");
mailMessage.setText(username+"您好,您的密码是:"+password);
javaMailSenderImpl.send(mailMessage);
}
}
View设计
tag file的使用
网页中有许多共同的部分,像网页开头,结尾或是公共信息,用户选单等功能,我们可以使用JSP 2.0技术中的Tag Files设计这些内容为tag文件,并在必要的时候把这些文件包含在网页中,达到复用的目的
header.tag
<%
...
@tag description="每个网页的Header" pageEncoding="GB2312"
%>
<
html
>
<
head
>
<
title
>
Spring在线书签
</
title
>
<
style
>
...
body {...}{font-family: Arial, Helvetica, sans-serif;
font-size: 13px}
li, td {...}{Arial, Helvetica, sans-serif;
font-size: 13px}
hr {...}{color: #3333cc; width: 300;
text-align: left}
a {...}{color: #000000}
</
style
>
</
head
>
<
body
>
<
img
src
='images/caterpillar.jpg'
alt
='Spring在线书签Logo'
border
='0'
align
='left'
valign
='bottom'
/>
<
h1
>
Spring在线书签
</
h1
>
<
hr
style
="width: 100%; height: 2px;"
>
<
hr
style
="width: 100%; height: 2px;"
>
site_info.tag
<%
...
@tag description="网站消息" pageEncoding="GB2312"
%>
<
ul
>
<
li
>
在线储存您的书签
<
li
>
分享您我的书签
<
li
>
快来秀出最酷的链接
</
ul
>
usermenu.tag
<%
...
@tag description="使用者功能表单" pageEncoding="GB2312"
%>
<
hr
style
="width: 100%; height: 2px;"
>
<
a
href
='/OnlineBookMark/member.do'
>
会员首页
</
a
>
|
<
a
href
='/OnlineBookMark/member.do?action=addUrl'
>
加入书签
</
a
>
|
<
a
href
='/OnlineBookMark/member.do?action=changePasswd'
>
改变密码
</
a
>
|
<
a
href
='/OnlineBookMark/member.do?action=logout'
>
退出
</
a
>
<
hr
style
="width: 100%; height: 2px;"
>
footer.tag
<%
...
@tag description="每个网页的Footer" pageEncoding="GB2312"
%>
</
body
>
</
html
>
注册网页register.jsp
<%
...
@ page language="java" pageEncoding="GB18030"
%>
<%
...
@taglib prefix="bookmark" tagdir="/WEB-INF/tags"
%>
<%
...
@taglib prefix="spring" uri="http://www.springframework.org/tags"
%>
<
html
>
<
head
>
<
title
>
注册
</
title
>
</
head
>
<
body
>
<
bookmark:header
/>
<
spring:bind
path
="command.*"
>
<
font
color
="red"
>
<
b
>
${status.errorMessage}
</
b
>
</
font
>
</
spring:bind
>
<
form
method
="post"
action
="/OnlineBookMark/register.do"
>
<
table
bgcolor
="#cccccc"
>
<
tr
>
<
td
>
邮件地址
</
td
>
<
td
>
<
spring:bind
path
="command.email"
>
<
input
type
="text"
name
="${status.expression}"
value
="${status.value}"
size
="30"
maxlength
="100"
>
</
spring:bind
>
</
td
>
</
tr
>
<
tr
>
<
td
>
名称(最大16字符)
</
td
>
<
td
>
<
spring:bind
path
="command.username"
>
<
input
type
="text"
name
="${status.expression}"
value
="${status.value}"
size
="16"
maxlength
="16"
>
</
spring:bind
>
</
td
>
</
tr
>
<
tr
>
<
td
>
密码(6到16字符)
</
td
>
<
td
valign
="top"
>
<
spring:bind
path
="command.passwd"
>
<
input
type
="password"
name
="${status.expression}"
value
="${status.value}"
size
="6"
maxlength
="16"
>
</
spring:bind
>
</
td
>
</
tr
>
<
tr
>
<
td
>
确认密码(6到16字符)
</
td
>
<
td
>
<
spring:bind
path
="command.passwd2"
>
<
input
type
="password"
name
="${status.expression}"
value
="${status.value}"
size
="6"
maxlength
="16"
>
</
spring:bind
>
</
td
>
</
tr
>
<
tr
>
<
td
colspan
="2"
align
="center"
>
<
input
type
="submit"
value
="注册"
/>
</
td
>
</
tr
>
</
table
>
</
form
>
</
body
>
</
html
>
注册成功页面register_success.jsp
<%
...
@ page language="java" pageEncoding="GB18030"
%>
<%
...
@taglib prefix="bookmark" tagdir="/WEB-INF/tags"
%>
<%
...
@taglib prefix="spring" uri="http://www.springframework.org/tags"
%>
<
html
>
<
head
>
<
title
>
注册
</
title
>
</
head
>
<
body
>
<
bookmark:header
/>
您的注册已成功,前往会员网页!
<
br
><
a
href
="/OnlineBookMark/member.do"
>
会员网页
</
a
>
<
bookmark:footer
/>
</
body
>
</
html
>
登陆页面 login.jsp
<%
...
@ page language="java" pageEncoding="GB18030"
%>
<%
...
@taglib prefix="bookmark" tagdir="/WEB-INF/tags"
%>
<%
...
@taglib prefix="spring" uri="http://www.springframework.org/tags"
%>
<
html
>
<
head
>
<
title
>
登陆
</
title
>
</
head
>
<
body
>
<
bookmark:header
/>
<
bookmark:site_info
/>
<
a
href
="/OnlineBookMark/register.do"
>
还不是会员?
</
a
>
<
form
metod
="post"
action
="/OnlineBookMark/login.do"
>
<
spring:bind
path
="command.*"
>
<
font
color
="red"
>
<
b
>
${status.errorMessage}
</
b
>
</
font
>
</
spring:bind
>
<
table
bgcolor
="#cccccc"
>
<
tr
>
<
td
colspan
="2"
>
会员登陆
</
td
>
</
tr
>
<
tr
>
<
td
>
名称:
</
td
>
<
td
>
<
spring:bind
path
="command.username"
>
<
input
type
="text"
name
="${status.expression}"
value
="${status.value}"
/>
</
spring:bind
>
</
td
>
</
tr
>
<
tr
>
<
td
>
密码:
</
td
>
<
td
>
<
spring:bind
path
="command.passwd"
>
<
input
type
="password"
name
="${status.expression}"
value
="${status.value}"
/>
</
spring:bind
>
</
td
>
</
tr
>
<
tr
>
<
td
colspan
="2"
align
="center"
>
<
input
type
="submit"
value
="登陆"
>
</
td
>
</
tr
>
<
tr
>
<
td
colspan
="2"
align
="center"
>
<
a
href
="/OnlineBookMark/forgot.do"
>
忘记密码?
</
a
>
</
td
>
</
tr
>
</
table
>
</
form
>
</
body
>
<
bookmark:footer
/>
</
html
>
登陆成功页面login_success.jsp
<%
@ page language
=
"java" pageEncoding
=
"GB18030"
%>
<%
@taglib
prefix
=
"bookmark" tagdir
=
"
/
WEB
-
INF
/
tags"
%>
<%
@taglib
prefix
=
"spring" uri
=
"http:
//
www.springframework.org
/
tags"
%>
<
html
>
<
head
>
<
title
>
登陆
</
title
>
</
head
>
<
body
>
<
bookmark:header
/>
您的登陆已成功,前往会员网页!
<
br
><
a href
=
"
/
OnlineBookMark
/
member.do"
>
会员网页
</
a
>
<
bookmark:footer
/>
</
body
>
</
html
>
新增书签addbm.jsp
<%
...
@ page language="java" pageEncoding="GB18030"
%>
<%
...
@taglib prefix="bookmark" tagdir="/WEB-INF/tags"
%>
<%
...
@taglib prefix="spring" uri="http://www.springframework.org/tags"
%>
<%
...
@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
<
html
>
<
head
>
<
title
>
新增书签
</
title
>
</
head
>
<
body
>
<
bookmark:header
/>
<
form
name
="bm_table"
action
="/OnlineBookMark/member.do"
method
="post"
>
<
table
width
="300"
cellpadding
="2"
cellpacing
="0"
>
<
tr
bgcolor
="#cccccc"
>
<
td
>
<
strong
>
书签
</
strong
>
</
td
>
<
td
>
<
input
type
="text"
name
="new_url"
value
="http://"
size
="30"
maxlength
="255"
/>
</
td
>
</
tr
>
<
tr
>
<
td
colspan
="2"
align
="center"
>
<
input
type
="hidden"
name
="action"
value
="addUrl"
/>
<
input
type
="submit"
value
="新增书签"
/>
</
td
>
</
tr
>
</
table
>
</
form
>
<
bookmark:usermenu
/>
<
bookmark:footer
/>
</
body
>
</
html
>
会员网页member.jsp
<%
...
@ page language="java" pageEncoding="GB18030"
%>
<%
...
@taglib prefix="bookmark" tagdir="/WEB-INF/tags"
%>
<%
...
@taglib prefix="spring" uri="http://www.springframework.org/tags"
%>
<%
...
@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
<
html
>
<
head
>
<
title
>
会员
</
title
>
</
head
>
<
body
>
<
bookmark:header
/>
<
br
>
${valid_user}您好!
</
br
>
<
form
name
="bm_table"
action
="/OnlineBookMark/member.do"
method
="post"
>
<
table
width
="300"
cellpadding
="2"
cellpacing
="0"
>
<
tr
bgcolor
="#cccccc"
>
<
td
>
<
strong
>
书签
</
strong
>
</
td
>
</
tr
>
<
c:forEach
var
="url"
items
="${urls}"
>
<
tr
bgcolor
="#ffffff"
>
<
td
>
<
a
href
=<c:out
value
="${url}}"
/>
>
<
c:out
value
="${url}"
/></
a
>
</
td
>
<
td
>
<
input
type
="checkbox"
name
="del_me"
value
=<c:out
value
="${url}"
/>
>
</
td
>
</
tr
>
</
c:forEach
>
<
tr
bgcolor
="#ffffff"
>
<
td
></
td
>
<
td
>
<
input
type
="hidden"
name
="action"
value
="deleteUrl"
/>
<
input
type
="submit"
value
="删除"
/>
</
td
>
</
tr
>
</
table
>
</
form
>
<
bookmark:usermenu
/>
<
bookmark:footer
/>
</
body
>
</
html
>
改变密码页面 change_passwd.jsp
<%
...
@ page language="java" pageEncoding="GB18030"
%>
<%
...
@taglib prefix="bookmark" tagdir="/WEB-INF/tags"
%>
<%
...
@taglib prefix="spring" uri="http://www.springframework.org/tags"
%>
<%
...
@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
<
html
>
<
head
>
<
title
>
修改密码
</
title
>
</
head
>
<
body
>
<
bookmark:header
/>
<
form
name
="bm_table"
action
="/OnlineBookMark/member.do"
method
="post"
>
<
table
width
="300"
cellpadding
="2"
cellpacing
="0"
>
<
tr
bgcolor
="#cccccc"
>
<
td
>
<
strong
>
新密码
</
strong
>
</
td
>
<
td
>
<
input
type
="password"
name
="new_passwd"
size
="16"
maxlength
="16"
/>
</
td
>
</
tr
>
<
tr
>
<
td
colspan
="2"
align
="center"
>
<
input
type
="hidden"
name
="action"
value
="changePassword"
/>
<
input
type
="submit"
value
="修改密码"
/>
</
td
>
</
tr
>
</
table
>
</
form
>
<
bookmark:usermenu
/>
<
bookmark:footer
/>
</
body
>
</
html
>
取回密码forgot.jsp
<%
...
@ page language="java" pageEncoding="GB18030"
%>
<%
...
@taglib prefix="bookmark" tagdir="/WEB-INF/tags"
%>
<%
...
@taglib prefix="spring" uri="http://www.springframework.org/tags"
%>
<%
...
@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
<
html
>
<
head
>
<
title
>
取回密码
</
title
>
</
head
>
<
body
>
<
bookmark:header
/>
<
form
name
="bm_table"
action
="/OnlineBookMark/forgot.do"
method
="post"
>
<
table
width
="300"
cellpadding
="2"
cellpacing
="0"
>
<
tr
bgcolor
="#cccccc"
>
<
td
>
<
strong
>
邮件地址
</
strong
>
</
td
>
<
td
>
<
input
type
="password"
name
="email"
size
="16"
maxlength
="100"
/>
</
td
>
</
tr
>
<
tr
>
<
td
colspan
="2"
align
="center"
>
<
input
type
="submit"
value
="寄出密码"
/>
</
td
>
</
tr
>
</
table
>
</
form
>
<
bookmark:usermenu
/>
<
bookmark:footer
/>
</
body
>
</
html
>
首页重定向页面index.jsp
<%
...
@ page language="java" pageEncoding="GB18030"
%>
<
<jsp:forward
page
="/OnlineBookMark/login.do"
></
jsp:forward
>
Controller设计
RegisterFomr.java及RegisterValidator.java
package
web;
public
class
RegisterForm
...
{
private String email;
private String username;
private String passwd;
private String passwd2;
public String getEmail() ...{
return email;
}
public void setEmail(String email) ...{
this.email = email;
}
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getPasswd() ...{
return passwd;
}
public void setPasswd(String passwd) ...{
this.passwd = passwd;
}
public String getPasswd2() ...{
return passwd2;
}
public void setPasswd2(String passwd2) ...{
this.passwd2 = passwd2;
}
}
package
web;
import
org.springframework.validation.Errors;
import
org.springframework.validation.Validator;
public
class
RegisterValidator
implements
Validator
...
{
public boolean supports(Class cls) ...{
return cls.equals(RegisterForm.class);
}
public void validate(Object obj, Errors errors) ...{
RegisterForm form = (RegisterForm) obj;
if(!validateEmail(form.getEmail())) ...{
errors.rejectValue("email",
"invalid_email", null, "邮件格式不正确");
}
if(form.getPasswd().length() < 6 ||
form.getPasswd().length() > 16) ...{
errors.rejectValue("passwd",
"less-or-over", null, "密码长度必须是6到16位的字符");
}
if(!form.getPasswd().equals(form.getPasswd2())) ...{
errors.rejectValue(
"passwd", "not-equal", null, "两次输入的密码不符");
}
}
private boolean validateEmail(String email) ...{
return email.matches("^[_a-z0-9-]+([.]" +
"[_a-z0-9-]+)*@[a-z0-9-]+([.][a-z0-9-]+)*$");
}
}
RegisterController.java 注册用户
package
web;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
model.IUserDAO;
import
model.User;
import
org.springframework.validation.BindException;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.SimpleFormController;
public
class
RegisterController
extends
SimpleFormController
...
{
private IUserDAO userDAO;
public RegisterController()...{
this.setCommandClass(RegisterForm.class);
}
public ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException exception) throws Exception...{
RegisterForm form=(RegisterForm)command;
User user=new User();
user.setEmail(form.getEmail());
user.setUsername(form.getUsername());
user.setPasswd(form.getPasswd());
this.getUserDAO().insert(user);
request.getSession().setAttribute("valid_user", form.getUsername());
return new ModelAndView(this.getSuccessView());
}
public IUserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) ...{
this.userDAO = userDAO;
}
}
LoginForm.java
package
web;
public
class
LoginForm
...
{
private String username;
private String passwd;
public String getUsername() ...{
return username;
}
public void setUsername(String username) ...{
this.username = username;
}
public String getPasswd() ...{
return passwd;
}
public void setPasswd(String passwd) ...{
this.passwd = passwd;
}
}
LoingController.java 登陆
package
web;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
model.IUserDAO;
import
model.User;
import
org.springframework.validation.BindException;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.SimpleFormController;
public
class
LoginController
extends
SimpleFormController
...
{
private IUserDAO userDAO;
public IUserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) ...{
this.userDAO = userDAO;
}
public LoginController()...{
this.setCommandClass(LoginForm.class);
}
protected ModelAndView onSubmit(HttpServletRequest request,
HttpServletResponse response, Object command, BindException errors)
throws Exception ...{
LoginForm form=(LoginForm)command;
User user=userDAO.findByName(form.getUsername());
if(user==null)...{
errors.rejectValue("username", "no_such_user", "查无此人");
return new ModelAndView(this.getFormView(),errors.getModel());
}
else if(user!=null&&!user.getPasswd().equals(form.getPasswd()))...{
errors.rejectValue("passwd", "wrong_password", "密码错误");
return new ModelAndView(this.getFormView(),errors.getModel());
}
else...{
request.getSession().setAttribute("valid_user", user.getUsername());
return new ModelAndView(this.getSuccessView());
}
}
}
MemberController.java 会员功能
package
web;
import
java.util.List;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
model.IBookmarkDAO;
import
model.IUserDAO;
import
model.User;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import
org.springframework.web.servlet.view.RedirectView;
public
class
MemberController
extends
MultiActionController
...
{
private IBookmarkDAO bookmarkDAO;
private IUserDAO userDAO;
private String memberPage;
private String addBmPage;
private String passwdPage;
private String redirectPAge;
public ModelAndView displayUrl(HttpServletRequest request,HttpServletResponse response)...{
String username=(String)request.getSession().getAttribute("valid_user");
if(username!=null)...{
List urls=this.getBookmarkDAO().findUserUrl(username);
return new ModelAndView(this.getMemberPage(),"urls",urls);
}
else...{
return new ModelAndView(new RedirectView(this.getRedirectPAge()));
}
}
public ModelAndView addUrl(HttpServletRequest request,HttpServletResponse response)...{
String username=(String)request.getSession().getAttribute("valid_user");
if(username!=null)...{
if(request.getParameter("new_url")!=null)...{
String url=(String)request.getParameter("new_url");
this.getBookmarkDAO().addUserUrl(username, url);
return displayUrl(request,response);
}
else...{
return new ModelAndView(this.getAddBmPage());
}
}else...{
return new ModelAndView(new RedirectView(this.getRedirectPAge()));
}
}
public ModelAndView deleteUrl(HttpServletRequest request,HttpServletResponse response)...{
String username=(String)request.getSession().getAttribute("valid_user");
if(username!=null)...{
if(request.getParameterValues("del_me")!=null)...{
String[] deleted_urls=(String[])request.getParameterValues("del_me");
this.getBookmarkDAO().deleteUserUrls(username, deleted_urls);
return this.displayUrl(request, response);
}else...{
return new ModelAndView(this.getAddBmPage());
}
}else...{
return new ModelAndView(new RedirectView(this.getRedirectPAge()));
}
}
public ModelAndView changePasswd(HttpServletRequest request,HttpServletResponse response)...{
String username=(String)request.getSession().getAttribute("valid_user");
if(username!=null)...{
if(request.getParameter("new_passwd")!=null)...{
String passwd=request.getParameter("new_passwd");
User user=this.getUserDAO().findByName(username);
user.setPasswd(passwd);
this.getUserDAO().update(user);
return displayUrl(request,response);
}
else...{
return new ModelAndView(this.getPasswdPage());
}
}else...{
return new ModelAndView(new RedirectView(this.getRedirectPAge()));
}
}
public ModelAndView logout(HttpServletRequest request,HttpServletResponse response)...{
request.getSession().invalidate();
return new ModelAndView(new RedirectView(this.getRedirectPAge()));
}
public IBookmarkDAO getBookmarkDAO() ...{
return bookmarkDAO;
}
public void setBookmarkDAO(IBookmarkDAO bookmarkDAO) ...{
this.bookmarkDAO = bookmarkDAO;
}
public IUserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) ...{
this.userDAO = userDAO;
}
public String getMemberPage() ...{
return memberPage;
}
public void setMemberPage(String memberPage) ...{
this.memberPage = memberPage;
}
public String getAddBmPage() ...{
return addBmPage;
}
public void setAddBmPage(String addBmPage) ...{
this.addBmPage = addBmPage;
}
public String getPasswdPage() ...{
return passwdPage;
}
public void setPasswdPage(String passwdPage) ...{
this.passwdPage = passwdPage;
}
public String getRedirectPAge() ...{
return redirectPAge;
}
public void setRedirectPAge(String redirectPAge) ...{
this.redirectPAge = redirectPAge;
}
}
ForgotController.java 会员忘记密码
package
web;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
import
model.IUserDAO;
import
model.SpringMail;
import
model.User;
import
org.springframework.web.servlet.ModelAndView;
import
org.springframework.web.servlet.mvc.AbstractController;
public
class
ForgotController
extends
AbstractController
...
{
private IUserDAO userDAO;
private SpringMail springMail;
private String loginPage;
private String forgotPage;
public IUserDAO getUserDAO() ...{
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) ...{
this.userDAO = userDAO;
}
public SpringMail getSpringMail() ...{
return springMail;
}
public void setSpringMail(SpringMail springMail) ...{
this.springMail = springMail;
}
public String getLoginPage() ...{
return loginPage;
}
public void setLoginPage(String loginPage) ...{
this.loginPage = loginPage;
}
public String getForgotPage() ...{
return forgotPage;
}
public void setForgotPage(String forgotPage) ...{
this.forgotPage = forgotPage;
}
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception ...{
String email=(String)request.getParameter("email");
if(email!=null&&!"".equals(email))...{
User user=this.getUserDAO().findByEmail(email);
this.getSpringMail().sendPasswdMail(email, user.getUsername(), user.getPasswd());
return new ModelAndView(this.getLoginPage());
}
else...{
return new ModelAndView(this.getForgotPage());
}
}
}