📄 day03_2.txt
字号:
修改用户信息
==========================================================
a) 为ContactInfoEJB添加find方法
右键点击ContactInfoEJB --> add --> finder
修改:
(* 用于根据用户名称,查询联系方式 )
finder name : findByUserId
return type :ContactInfoLocal
input parameter: String userId
home interface : local home
query :
select object(c)
from ContactInfo c
where c.userid=?1
编译
=========================================================
为UserMgmSessionFacade添加商业方法
右键点击UserMgmSessionFacade --> add --> method
修改:
(* 用于根据用户名称,查询联系方式 )
name : getContactByUserId
return type :dto.ContactInfoDTO
input parameter: String userId
interface : local
=========================================================
右键点击UserMgmSessionFacade --> view bean source
为getContactByUserId函数添加函数体
public dto.ContactInfoDTO getContactByUserId(
String userid) throws FinderException {
try{
ContactInfoLocal local = contactInfoHome.findByUserId( userid );
ContactInfoDTO info = new ContactInfoDTO(
userid,local.getStreet1(),
local.getStreet2(),
local.getCity(),
local.getProvince(),
local.getCountry(),
local.getZip(),
local.getEmail(),
local.getHomephone(),
local.getCellphone(),
local.getOfficephone() );
return info;
}catch( FinderException fe ){
fe.printStackTrace();
throw fe;
}
}
==========================================================
修改UserMgmSessionFacadeLocal中的函数声, 使之与bean class 中的一致 :
为getContactByUserId方法,添键throws FinderException
public void getContactByUserId(String userid)
throws FinderException;
d) 编译
==========================================================
为UserMgmDelegate 添加商业方法
public ContactInfoDTO getContactByUserId( String userid ) throws FinderException{
return sessionLocal.getContactByUserId( userid );
}
=========================================================··························································
为UserMgmSessionFacade添加商业方法
右键点击UserMgmSessionFacade --> add --> method
修改:
(* 用于根据用户名称,修改联系方式 )
name : updateContact
return type :void
input parameter: dto.ContactInfoDTO info
interface : local
=========================================================
右键点击UserMgmSessionFacade --> view bean source
为dto.ContactInfoDTO info函数添加函数体
public void updateContact(dto.ContactInfoDTO info)
throws FinderException {
ContactInfoLocal local =
contactInfoHome.findByUserId( info.getUserid());
local.setStreet1( info.getStreet1());
local.setStreet2( info.getStreet2());
local.setCity( info.getCity() );
local.setProvince( info.getProvince() );
local.setCountry( info.getCountry() );
local.setZip( info.getZip());
local.setEmail( info.getEmail() );
local.setHomephone( info.getHomephone() );
local.setCellphone( info.getCellphone());
local.setOfficephone( info.getOfficephone() );
}
==========================================================
修改UserMgmSessionFacadeLocal中的函数声, 使之与bean class 中的一致 :
为updateContact方法,添键throws FinderException
public void updateContact(dto.ContactInfoDTO info)
throws FinderException {
d) 编译
==========================================================
为UserMgmDelegate 添加商业方法
public void updateContact( ContactInfoDTO info )
throws FinderException{
sessionLocal.updateContact( info );
}
=========================================================
在UserCenter.jsp上添加添加修改用户信息的连接
<a href="viewUserContact.do">修改个人资料</a>
========================================================
为 viewUserContact.do 创建Action
创建ViewUserContactAction
a) file --> new --> web --> Action
填写 package : ec_port_web
Action : ViewUserContactAction
--> next
ActionPath : /viewUserContact
form bean name:
scope :
validate :
input jsp :
用于该action不需要任何数据,所以只配置AcitonPath即可
--> finish
b) 为 ViewUserContactAction 配置forward:
双击struts-config.xml ,进入图形界面
在走下角展开Action Mapping 选中/viewUserContact
在窗口右下方选中forward标签 --> add
1) path : /fail.jsp
name : notLogin
2) path : /fail.jsp
name : systemError
3) path : /modifyUserContact.jsp
name : success
c) 存盘 , 察看struts-config.xml
==========================================================
填写perform方法:
package ec_port_web;
import org.apache.struts.action.*;
import javax.servlet.http.*;
import delegates.UserMgmDelegate;
import exceptions.ServiceLocatorException;
import javax.ejb.CreateException;
import dto.ContactInfoDTO;
import javax.ejb.FinderException;
public class ViewUserContactAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) {
try{
UserMgmDelegate ud = new UserMgmDelegate();
String userid = (String)
request.getSession().getAttribute( "USER_ID" );
if( userid == null ){
return actionMapping.findForward( "notLogin" );
}
ContactInfoDTO info =
ud.getContactByUserId( userid );
request.getSession().setAttribute( "USER_CONTACT_INFO" , info );
return actionMapping.findForward( "success" );
}catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( CreateException ce ){
ce.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( FinderException fe ){
fe.printStackTrace();
return actionMapping.findForward( "systemError" );
}
}
}
==========================================================
创建 modifyUserContact.jsp
<%
ContactInfoDTO info = (ContactInfoDTO)
session.getAttribute( "USER_CONTACT_INFO" );
%>
<form action="modifyUserContact.do" method="post">
<p align=center>
<table width="600" border="0" cellspacing="1" cellpadding="4" bgcolor="3399ff">
<tr>
<td colspan="2" align="center"> <font color="#ffffff" class="title">
用户信息</font> </td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%" align="center" height="11">用户名</td>
<td bgcolor="#FFFFFF" height="11">
<%=info.getUserid()%>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%" align="center">地址一</td>
<td bgcolor="#FFFFFF">
<input type="text" name="street1"
value='<%=info.getStreet1()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center">地址二</td>
<td bgcolor="#FFFFFF">
<input type="text" name="street2"
value='<%=info.getStreet2()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center">城市</td>
<td bgcolor="#FFFFFF">
<input type="text" name="city"
value='<%=info.getCity()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center">省份</td>
<td bgcolor="#FFFFFF">
<input type="text" name="province"
value='<%=info.getProvince()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center">国家</td>
<td bgcolor="#FFFFFF">
<input type="text" name="country"
value='<%=info.getCountry()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center">邮政编码</td>
<td bgcolor="#FFFFFF">
<input type="text" name="zip"
value='<%=info.getZip()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center"> E-mail</td>
<td bgcolor="#FFFFFF">
<input type="text" name="email"
value='<%=info.getEmail()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center"> 住宅电话</td>
<td bgcolor="#FFFFFF">
<input type="text" name="homephone"
value='<%=info.getHomephone()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center"> 手机</td>
<td bgcolor="#FFFFFF">
<input type="text" name="cellphone"
value='<%=info.getCellphone()%>'>
</td>
</tr>
<tr>
<td bgcolor="#ececec" width="22%"
align="center">办公室电话</td>
<td bgcolor="#FFFFFF">
<input type="text" name="officephone"
value='<%=info.getOfficephone()%>'>
</td>
</tr>
</table>
<p align=center>
<input type="submit" value="修改">
</p>
</td>
</tr>
</table>
</form>
==========================================================
为modifyUserContact.jsp 创建formBean
a) file --> new --> web --> actionForm
填写
package : web_port_web
actionForm : ec_port_web.ModifyUserContactForm
--> next --> add from jsp --> register.jsp --> next
formBean name : modifyUserContactForm
(注意:
actionForm : 对应<html:form>中的type
formbean name : 对应<html:form>中的name
)
b) 重写validate 和 reset 方法
c) 编译
d) 察看struts-config.xml原文件
==========================================================
创建ModifyUserContactAction
a) file --> new --> web --> Action
填写 package : ec_port_web
Action : ModifyUserContactAction
--> next
ActionPath : /modifyUserContact.do
form bean name: modifyUserContactForm
scope : session
validate : true
input jsp : modifyUserContact.jsp
其中:
ActionPath : Action的侦听路径,与页面form中的Action相
对应,将.do去掉即可
form bean name : Action 需要用到的form bean
scope :form bean 放在什么区域
input jsp : 请求是由那个页面发送的
--> finish
b) 为 ModifyUserContactAction 配置forward:
双击struts-config.xml ,进入图形界面
在走下角展开Action Mapping 选中/modifyUserContact
在窗口右下方选中forward标签 --> add
1) path : index.jsp
name : success
1) path : fail.jsp
name : systemError
c) 存盘 , 察看struts-config.xml 源代码
==========================================================
实现perform方法:
import javax.ejb.CreateException;
import exception.ServiceLocatorException;
import javax.ejb.FinderException;
public class ModifyUserContactAction extends Action {
public ActionForward perform(
ActionMapping actionMapping,
ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) {
try{
ModifyContactForm modifyContactForm = (ModifyContactForm) actionForm;
String userid = (String)
request.getSession().getAttribute("USER_ID");
ContactInfoDTO info = new ContactInfoDTO(userid,
modifyContactForm.getStreet1(),
modifyContactForm.getStreet2(),
modifyContactForm.getCity(),
modifyContactForm.getProvince(),
modifyContactForm.getCountry(),
modifyContactForm.getZip(),
modifyContactForm.getEmail(),
modifyContactForm.getHomephone(),
modifyContactForm.getCellphone(),
modifyContactForm.getOfficephone());
UserMgmDelegate ud = new UserMgmDelegate();
ud.updateContact( info );
return actionMapping.findForward( "success" );
}catch( ServiceLocatorException se ){
se.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( CreateException ce ){
ce.printStackTrace();
return actionMapping.findForward( "systemError" );
}
catch( FinderException fe ){
fe.printStackTrace();
return actionMapping.findForward( "systemError" );
}
}
}
==========================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -