剛接觸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下,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 SettingsttworkspaceWelcome_SSWebRoot" reloadable="true"></Context>
注:C:Documents and SettingsttworkspaceWelcome_SS是該工程的存儲路徑。
b.在Myeclipse中的工具欄上找到一個像機箱似形狀的按鈕,點擊,則打開服務器Tomcat.
7、運行
在IE 浏覽器裡輸入http://localhost:8080/wess,即可運行!成功登陸的條件是用戶名和密碼都為success的時候。