刚接触struts不久,公司又要我学spring,然后把两者结合起来用。于是我在网上找了很多的有关struts+spring的例子,但基本上都是struts+spring+hibernate的,而我们公司不用hibernate。后来,我就在struts的基本上,再学习怎么把spring加入的例子,然后根据它的原理重新编写一本书上用struts实现的例子。
一、运行环境
1、eclipse-SDK-3.2
2、MyEclipse GA5.0
3、apache-tomcat-5.5.20
4、Strust1.2
5、Spring1.2
二、该工程所需文件
1、index.jsp
2、login.jsp
3、reinput_blank.jsp
4、reinput_error.jsp
5、success.jsp
6、LoginActio.java
7、LoginForm.java
8、struts-config.xml
9、applicationContext.xml
三、步骤
1、建工程
创建一个名为Welcome_SS的Web Project
File -> New ->
Project -> Web Project , “next” Project Name:”Welcome_SS” ,“finish”;
2、导入struts包
选择Welcome_SS工程,右键->Myeclipse (或选菜单栏上Myeclipse) -> Add struts Capabilities……,选择struts1.2,然后finish;
3、导入spring包
选择Welcome_SS工程,右键->Myeclipse (或选菜单栏上Myeclipse) -> Add spring Capabilities……,在默认基础上,再选择Spring 1.2 AOP Libraries -
<MyEclipse-Library>和Spring 1.2 Web Libraries -
<MyEclipse-Library>,然后finish;
(为什么要选这两个,我也还没有弄清楚)
注:可能还要在lib文件夹下加几个spring包,
spring.jar,
spring-1.2.6.jar,
spring-aop.jar,
spring-beans.jar,
spring-context.jar,
spring-core.jar,
spring-orm.jar,
spring-remoting.jar,
spring-support.jar,
spring-web.jar,
spring-webmvc.jar
4、编写代码(页面及其所需的类)
Ø
index.jsp
选择Welcome_SS工程下的WebRoot -> New -> JSP,取名为index.jsp,Finish;
输入以下代码
1
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <head>
5 <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
6 </head>
7 <body><div align="center">
8 Welcome here!<br>.<a href="login.jsp">sign in</a></div>
9 </body>
10 </html>
11
Ø
login.jsp
选择Welcome_SS工程下的WebRoot -> New -> JSP,取名为login.jsp,Finish;
输入以下代码
1 <%@ page contentType="text/html;charset=gb2312"%>
2 <%@ page import="java.util.*,java.sql.*,java.text.*,java.io.*"%>
3
4 <html>
5 <body>
6 <div align="center"><form name="form1" method="post" action="login.do">
7 Welcome Here! <br><br>
8 username:<input type="text" name="username"><br>
9 password:<input type="text" name="password"><br>
10 <input type="submit" name="submit1" value="submit">
11 <input type="reset" name="submit2" value="reset" onclick="deleteAllMessage()"><br>
12 </form></div>
13 </body>
14 </html>
Ø
reinput_blank.jsp
选择Welcome_SS工程下的WebRoot -> New -> JSP,取名为reinput_blank.jsp,Finish;
输入以下代码
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <body>
6 <div align="center"><form name="form1" method="post" action="login.do"><font color="#ff0000">*用户名和密码都不能为空!</font><br><br>
7 username:<input type="text" name="username" > <br>
8 password:<input type="text" name="password" > <br>
9 <input type="submit" name="submit1" value="submit">
10 <input type="reset" name="submit2" value="reset" onclick="deleteAllMessage()">
11 </form></div>
12 </body>
13 </html>
14
Ø
reinput_error.jsp
选择Welcome_SS工程下的WebRoot -> New -> JSP,取名为reinput_error.jsp,Finish;
输入以下代码
1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
2
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <body>
6 <div align="center"><form name="form1" method="post" action="login.do"><font color="#ff0000">*用户名或密码不正确!</font><br><br>
7 username:<input type="text" name="username" > <br>
8 password:<input type="text" name="password" > <br>
9 <input type="submit" name="submit1" value="submit">
10 <input type="reset" name="submit2" value="reset" onclick="deleteAllMessage()">
11 </form></div>
12 </body>
13 </html>
14
Ø success.jsp
选择Welcome_SS工程下的WebRoot -> New -> JSP,取名为success.jsp,Finish;
输入以下代码
1 <%@ page language="java" pageEncoding="utf-8"%>
2
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <head>
6 <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
7 </head>
8 <body>
9 <%=request.getAttribute("message")%>!<br>
10 .<a href="login.jsp">sign in</a><br>
11 .<a href="index.jsp">sign out</a>
12 </body>
13 </html>
14
15
Ø
LoginAction.jsp
选择Welcome_SS工程下的src -> com -> yourcompany -> struts下,New->files :action,再在这个文件夹下New->Java,取名为LoginAction.java,Finish;
输入以下代码
1
2 package com.yourcompany.struts.action;
3
4 import java.util.Locale;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8 import javax.servlet.http.HttpSession;
9
10 import org.apache.struts.action.Action;
11 import org.apache.struts.action.ActionForm;
12 import org.apache.struts.action.ActionForward;
13 import org.apache.struts.action.ActionMapping;
14 import org.apache.struts.util.MessageResources;
15
16 import com.yourcompany.struts.form.LoginForm;
17
18 public class LoginAction extends Action {
19
20 private String message;
21
22 public String getMessage() {
23 return message;
24 }
25
26 public void setMessage(String message) {
27 this.message = message;
28 }
29
30 public ActionForward execute(ActionMapping mapping,
31 ActionForm form,
32 HttpServletRequest request,
33 HttpServletResponse response)
34 LoginForm userform = (LoginForm) form;
35 if(userform.getUsername().equals("")||(userform.getPassword().equals(""))){
36 return mapping.findForward("reinput_blank");
37 }
38 else if(userform.getUsername().equals("success")&&(userform.getPassword().equals("success"))){
39 request.setAttribute("message",getMessage()+userform.getUsername());
40 return mapping.findForward("success");
41 }
42 else{
43 return mapping.findForward("reinput_error");
44 }
45 }
46 }
47
Ø
LoginForm.jsp
选择Welcome_SS工程下的src -> com -> yourcompany -> struts下,New->files :form,再在这个文件夹下New->Java,取名为LoginAction.java,Finish;
输入以下代码
1
2 package com.yourcompany.struts.form;
3
4 import javax.servlet.http.HttpServletRequest;
5 import org.apache.struts.action.ActionErrors;
6 import org.apache.struts.action.ActionForm;
7 import org.apache.struts.action.ActionMapping;
8
9 public class LoginForm extends ActionForm {
10
11 /** username property */
12 private String username;
13 private String password;
14 public String getPassword() {
15 return password;
16 }
17 public void setPassword(String password) {
18 this.password = password;
19 }
20 public String getUsername() {
21 return username;
22 }
23 public void setUsername(String username) {
24 this.username = username;
25 }
26 }
5、根据前一步的代码,进行配置文件的编写(struts-config.xml和applicationContext.xml)
选择Welcome_SS工程下的WebRoot -> WEB-INF ->
struts-config.xml
输入下面代码:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
3
4 <struts-config>
5 <data-sources />
6 <form-beans >
7 <form-bean name="loginForm" type="com.yourcompany.struts.form.LoginForm" />
8 </form-beans>
9
10 <global-exceptions />
11 <global-forwards />
12 <action-mappings >
13 <action
14 attribute="loginForm"
15 input="/login.jsp"
16 name="loginForm"
17 path="/login"
18 scope="request"
19 type="org.springframework.web.struts.DelegatingActionProxy">
20 <forward name="success" path="/success.jsp" />
21 <forward name="reinput_blank" path="/reinput_blank.jsp"/>
22 <forward name="reinput_error" path="/reinput_error.jsp"/>
23 </action>
24
25 </action-mappings>
26
27 <message-resources parameter="com.yourcompany.struts.ApplicationResources" />
28
29 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
30 <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
31 </plug-in>
32
33 </struts-config>
34
35
选择Welcome_SS工程下的src -> applicationContext.xml
输入下面代码:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
3 <beans>
4 <bean name="/login" class="com.yourcompany.struts.action.LoginAction">
5 <property name="message">
6 <value>Welcome_</value>
7 </property>
8 </bean>
9 </beans>
10
6、Web Service(这里即Tomcat)布署及启动
(全面的可以参照我的另一篇随笔:Myeclipse中Tomcat的两种布署方式)
a.在apache-tomcat-5.5.20"conf下,找到server.xml,打开,在</Host>前面添加一句
<Context path="/wess" debug="0"
docBase="C:\Documents and Settings\tt\workspace\Welcome_SS\WebRoot"
reloadable="true"></Context>
注:C:\Documents and Settings\tt\workspace\Welcome_SS是该工程的存储路径。
b.在Myeclipse中的工具栏上找到一个像机箱似形状的按钮,点击,则打开服务器Tomcat.
7、运行
在IE 浏览器里输入http://localhost:8080/wess,即可运行!成功登陆的条件是用户名和密码都为success的时候。
posted on 2007-11-23 10:41
Harriet 阅读(4304)
评论(9) 编辑 收藏 所属分类:
Struts+Spring