第一范文网 - 专业文章范例文档资料分享平台

spring快速入门教程详解 - 图文 

来源:用户分享 时间:2025/5/28 20:52:17 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

return mapping.findForward(\edit\); }

public ActionForward list(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception {

if (log.isDebugEnabled()) {

log.debug(\entering 'list' method...\); }

request.setAttribute(%users\, mgr.getUsers()); return mapping.findForward(\list\); }

public ActionForward save(ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception {

if (log.isDebugEnabled()) {

log.debug(\entering 'save' method...\); }

DynaActionForm userForm = (DynaActionForm) form; mgr.saveUser((User)userForm.get(%user\)); ActionMessages messages = new ActionMessages();

messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(%user.saved\));

saveMessages(request, messages);

return list(mapping, form, request, response); } }

接下来,你可以修改这个类的CRUD方法。

8.修改struts-config.xml,使用ContextLoaderPlugin来配置Spring的UserManager设置。要配置ContextLoaderPlugin,把下面内容添加到你的struts-config.xml中。

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

INF/applicationContext.xml, /WEB-INF/action-servlet.xml />

默认情况下这个插件会载入action-servlet.xml文件。要让Test Action 知道你的Manager,必须配置这个插件,如同载入applicationContext。

9.对每个使用Spring的action,定义一个type=

org.springframework.web.struts.DelegatingActionProxy 的action-mapping,为每个Spring action 声明一个配对的Spring bean。这样修改一下你的action mapping就能使用这个新类。

10.为DispatchAction修改action mapping。

为了让DispatchAction运行,在mapping中添加参数parameter= “method” , 它表示(在一个URL 或是隐藏字段hidden field)要调用的方法,同时转向(forwards)edit和list forwards(参考能进行CRUD操作的UserAction类).

type=\org.springframework.web.struts.DelegatingActionProxy\ name=%userForm\scope=\request\ parameter=\method\>

确保web目录下已经建好userList.jsp和userForm.jsp两个文件。暂时不必在文件中写入内容。

11.作为插件的一部分,配置Spring,将/user bean设置成“UserManager”。在we/WEB-INF/action-servlet.xml中添加以下定义。

定义中,使用singleton= false 。这样就为每个请求,新建一个Action,减少线程安全的Action需求。不管是Manager还是DAO都有成员变量,可能不需要这些属性(默认singleton= true )。

12.在message.properties上配置资源绑定。

在userAction类中,在完成一些操作后,会显示“成功”或是“错误”页面,这些信息的键可以存放在这个应用的ResourceBundle(或messages.properties文件中)。特别是:

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

●●●

user.saved user.missing user.deleted

把这些键存入web/WEB-INF/下的messages.properties文件中。例如:user.saved=User has been saved successfully.user.missing=No user found with this id. user.deleted=User successfully deleted.

这个文件通过struts-config.xml中的元素进行加载。

运行 ant test -Dtestcase=UserAction. 输出结果如下:

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

填充JSP文件,这样可以通过浏览器来进行CRUD操作

1.在你的jsp文件(userFrom.jsp 和userList.jsp)中添加代码,这样它们可以表示actions的结果。如果还事先准备,在web目录下建一个文件 userList.jsp。添加一些代码你就可以看到数据库中所有的用户资料。在下面代码中,第一行包含(include)了一个文件taglibs.jsp。这个文件包含了应用所有JSP Tag Library的声明。大部分是StrutsTag,JSTL和SiteMesh(用来美化JSP页面)。

<%@ include file=\/taglibs.jsp\%> MyUsers ~ User List

User Id First Name Last Name
${user.id} ${user.firstName} ${user.lastName}

Downloads By http://down.liehuo.net

http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛

你可以有一行“标题头”(headings)(在中)。JSTL 的 进行结果迭代,显示所有的用户。

2.向数据库添加一些数据,你就会看到一些真实(actual)的用户(users)。你可以选择一种方法,手工添加,使用ant browse,或是在build.xml中添加如下的target:

userid=\sa\ password=\>

INSERT INTO app_user (id, first_name, last_name) values (5, 'Julie','Raible');

INSERT INTO app_user (id, first_name, last_name) values (6, 'Abbie','Raible');

警告:为了使内置的HSQLDB正常工作,从能运行ant的目录下启动tomcat。在Unix/Linux键入 $CATALINA_HOME/bin/startup.sh ,在win上 êTALINA_HOME%\\bin\\startup.bat 。

搜索更多关于: spring快速入门教程详解 - 图文  的文档
spring快速入门教程详解 - 图文 .doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8eir722jc617c1937435_7.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top