Myeclipse平台struts+hibernate+spring项目开发示例
来源:互联网 发布:自然拼读法软件 编辑:程序博客网 时间:2024/05/19 23:25
接:Myeclipse开发struts+hibernate+spring新手入门---环境配置
如果您按照上篇文章配置成功,可以进行开发简单的J2EE项目。
开发前准备:
1、新建项目包:如下图:
Dao:数据层;service:逻辑层;web:web展现层。
2、建立好三层架构,部署好包名。建立一个jsp文件,取名为:login.jsp
代码如下:
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<HTML>
<HEAD>
<TITLE>test<TITLE>
<HEAD>
<BODY>
<html:form action="/login" method="post">
用户名:<html:text property="username" size="15"/> <br>
密 码:<html:password property="password" size="15"/><br>
<html:submit property="submit1" style="FONT-WEIGHT:bold;COLOR:red">
登 录
<html:submit>
<html:form>
<BODY>
<HTML>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<HTML>
<HEAD>
<TITLE>test<TITLE>
<HEAD>
<BODY>
<html:form action="/login" method="post">
用户名:<html:text property="username" size="15"/> <br>
密 码:<html:password property="password" size="15"/><br>
<html:submit property="submit1" style="FONT-WEIGHT:bold;COLOR:red">
登 录
<html:submit>
<html:form>
<BODY>
<HTML>
3、配置struts-config.xml文件;建立action、formbean;
改配置可以自动生成,使用eclipse进行自动生成,在建立时,如下图,可直接生成对应的action类和formbean类。
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
3
4 <struts-config>
5 <data-sources />
6 <form-beans>
7 <form-bean name="LoginForm" type="com.test.web.form.LoginForm" />
8
9 </form-beans>
10
11 <global-exceptions />
12 <global-forwards />
13 <action-mappings>
14 <action attribute="LoginForm" input="/login.jsp" name="LoginForm" path="LoginAction" scope="request" type="com.test.web.action.LoginAction" validate="true">
15 <forward name="faile" path="faile.jsp" />
16 <forward name="success" path="success.jsp" />
17 </action>
18
19 </action-mappings>
20
21 <message-resources parameter="ApplicationResources" />
22 </struts-config>
2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
3
4 <struts-config>
5 <data-sources />
6 <form-beans>
7 <form-bean name="LoginForm" type="com.test.web.form.LoginForm" />
8
9 </form-beans>
10
11 <global-exceptions />
12 <global-forwards />
13 <action-mappings>
14 <action attribute="LoginForm" input="/login.jsp" name="LoginForm" path="LoginAction" scope="request" type="com.test.web.action.LoginAction" validate="true">
15 <forward name="faile" path="faile.jsp" />
16 <forward name="success" path="success.jsp" />
17 </action>
18
19 </action-mappings>
20
21 <message-resources parameter="ApplicationResources" />
22 </struts-config>
对应生成的formbean如下代码所示:
1 import javax.servlet.http.HttpServletRequest;
2 import org.apache.struts.action.ActionErrors;
3 import org.apache.struts.action.ActionForm;
4 import org.apache.struts.action.ActionMapping;
5
6 /**
7 * MyEclipse Struts
8 * Creation date: 11-10-2005
9 *
10 * XDoclet definition:
11 * @struts.form name="LoginForm"
12 */
13 public class LoginForm extends ActionForm {
14
15 /** password property */
16 private String password;
17
18 /** username property */
19 private String username;
20
21
22 /**
23 * Method validate
24 * @param mapping
25 * @param request
26 * @return ActionErrors
27 */
28 public ActionErrors validate(
29 ActionMapping mapping,
30 HttpServletRequest request) {
31
32 /**对页面提交的元素进行验证,根据需要进行验证:在struts-config.xml中的 action-mapping配置设置validate="true"
33 * 以下验证才会在页面加载时生效.
34 */
35 ActionErrors errors = new ActionErrors();
36 if(username==null||username.equals(""))
37 {
38 errors.add("username",new ActionMessage("username.errors"));
39 return errors;
40 }
41 if(password==null||password.equals(""))
42 {
43 errors.add("password",new ActionMessage("password.errors"));
44 return errors;
45 }
46 return errors;
47 }
48
49 /**
50 * Method reset
51 * @param mapping
52 * @param request
53 */
54 public void reset(ActionMapping mapping, HttpServletRequest request) {
55
56
57 }
58
59 /**
60 * Returns the password.
61 * @return String
62 */
63 public String getPassword() {
64 return password;
65 }
66
67 /**
68 * Set the password.
69 * @param password The password to set
70 */
71 public void setPassword(String password) {
72 this.password = password;
73 }
74
75 /**
76 * Returns the username.
77 * @return String
78 */
79 public String getUsername() {
80 return username;
81 }
82
83 /**
84 * Set the username.
85 * @param username The username to set
86 */
87 public void setUsername(String username) {
88 this.username = username;
89 }
90
91 }
FormBean中验证后的提示需要在ApplicationResources_zh_CN.properties中配置提示错误信息. 2 import org.apache.struts.action.ActionErrors;
3 import org.apache.struts.action.ActionForm;
4 import org.apache.struts.action.ActionMapping;
5
6 /**
7 * MyEclipse Struts
8 * Creation date: 11-10-2005
9 *
10 * XDoclet definition:
11 * @struts.form name="LoginForm"
12 */
13 public class LoginForm extends ActionForm {
14
15 /** password property */
16 private String password;
17
18 /** username property */
19 private String username;
20
21
22 /**
23 * Method validate
24 * @param mapping
25 * @param request
26 * @return ActionErrors
27 */
28 public ActionErrors validate(
29 ActionMapping mapping,
30 HttpServletRequest request) {
31
32 /**对页面提交的元素进行验证,根据需要进行验证:在struts-config.xml中的 action-mapping配置设置validate="true"
33 * 以下验证才会在页面加载时生效.
34 */
35 ActionErrors errors = new ActionErrors();
36 if(username==null||username.equals(""))
37 {
38 errors.add("username",new ActionMessage("username.errors"));
39 return errors;
40 }
41 if(password==null||password.equals(""))
42 {
43 errors.add("password",new ActionMessage("password.errors"));
44 return errors;
45 }
46 return errors;
47 }
48
49 /**
50 * Method reset
51 * @param mapping
52 * @param request
53 */
54 public void reset(ActionMapping mapping, HttpServletRequest request) {
55
56
57 }
58
59 /**
60 * Returns the password.
61 * @return String
62 */
63 public String getPassword() {
64 return password;
65 }
66
67 /**
68 * Set the password.
69 * @param password The password to set
70 */
71 public void setPassword(String password) {
72 this.password = password;
73 }
74
75 /**
76 * Returns the username.
77 * @return String
78 */
79 public String getUsername() {
80 return username;
81 }
82
83 /**
84 * Set the username.
85 * @param username The username to set
86 */
87 public void setUsername(String username) {
88 this.username = username;
89 }
90
91 }
1 username.null.error=/u5462/u79f0/u4e0d/u80fd/u4e3a/u7a7a
2 password.null.error=/u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a
下面我们需要对WEB展现中对Action进行配置;我们在这里主要是针对登录进行开发.因此在action中需要完成以下步骤:2 password.null.error=/u5bc6/u7801/u4e0d/u80fd/u4e3a/u7a7a
1、登录验证;验证用户名是否存在。验证该用户的密码是否正确。
2、增加cookie
3、提示登录成功。
在进行Action类的编写过程中,首先我们需要考虑到对数据的操作有如下步骤。
1、验证用户名是否存在,我们在service层需要提供一个接口:
1 Public boolean isExist(String user);
来判断用户名是否存在,同时要考虑该用户名对应的密码是否正确,需要提供接口: 1 Public UserBasicInfo findByUser(String user);
通过用户名来查找相关数据。 - Myeclipse平台struts+hibernate+spring项目开发示例
- Myeclipse平台struts+hibernate+spring项目开发示例
- Myeclipse平台struts+hibernate+spring项目开发示例
- Myeclipse平台struts+hibernate+spring项目开发示例
- Myeclipse平台struts+hibernate+spring项目开发示例
- Myeclipse开发struts+hibernate+spring小记
- myeclipse 开发SSH(struts+hibernate+spring)
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- 使用myeclipse集成struts,hibernate,spring的一个示例程序
- spring struts hibernate包如何手动导入myeclipse项目中 !!!!
- Myeclipse开发struts+hibernate+spring新手入门---环境配置
- Myeclipse开发struts+hibernate+spring新手入门---环境配置
- rs.open语句详细说明
- web.xml规则经过整理总结如下:
- 鬼子的西游记
- 提前还贷怎样才划算
- Customizing Unattended Installations
- Myeclipse平台struts+hibernate+spring项目开发示例
- Linux入门命令
- 数据库知识串
- 欲哭!
- Myeclipse开发struts+hibernate+spring新手入门---环境配置
- [转自互联网]tomcat5.0中配置sqlserver2000数据库连接池
- 在类文件中如何获取cookie或者session的值
- Solaris 10 安装及SVC管理
- [转自互联网]体验Java 5.0的新增语言特性