剛接觸Jsp的struts不久,又要學spring,然後把兩者結合起來用。於是我在網上找了很多的有關struts+spring的例子,但基本上都是a的例子,然後根據它的原理重新編寫一本書上用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>
Ø LoginAction.jsp
選擇Welcome_SS工程下的src -> com -> yourcompany -> struts下,N