熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

struts+spring實現的登陸實例源碼

2022-06-13   來源: JSP教程 

  剛接觸Jsp的struts不久又要學spring然後把兩者結合起來用於是我在網上找了很多的有關struts+spring的例子但基本上都是a的例子然後根據它的原理重新編寫一本書上用struts實現的例子
運行環境
eclipseSDK
MyEclipse GA
apachetomcat
Strust
Spring
該工程所需文件
indexjsp
loginjsp
reinput_blankjsp
reinput_errorjsp
successjsp
LoginActiojava
LoginFormjava
strutsconfigxml
applicationContextxml
步驟
建工程
   創建一個名為Welcome_SS的Web Project
   File > New > Project > Web Project “next” Project Name”Welcome_SS” “finish”;
導入struts包
   選擇Welcome_SS工程右鍵>Myeclipse (或選菜單欄上Myeclipse) > Add struts Capabilities……選擇struts然後finish;
導入spring包
   選擇Welcome_SS工程右鍵>Myeclipse (或選菜單欄上Myeclipse) > Add spring Capabilities……在默認基礎上再選擇Spring AOP Libraries <MyEclipseLibrary>和Spring Web Libraries <MyEclipseLibrary>然後finish;
(為什麼要選這兩個我也還沒有弄清楚)
可能還要在lib文件夾下加幾個spring包
springjar
springjar
springaopjar
springbeansjar
springcontextjar
springcorejar
springormjar
springremotingjar
springsupportjar
springwebjar
springwebmvcjar
編寫代碼(頁面及其所需的類)
Ø         indexjsp
選擇Welcome_SS工程下的WebRoot > New > JSP取名為indexjspFinish;
 輸入以下代碼
  
  <!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
  <html>
    <head>   
      <!<link rel="stylesheet" type="text/css" href="/stylescss">>
    </head>  
    <body><div align="center"> 
      Welcome here!<br><a href="loginjsp">sign in</a></div> 
    </body>
</html>
 
Ø         loginjsp
選擇Welcome_SS工程下的WebRoot > New > JSP取名為loginjspFinish;
 輸入以下代碼
  <%@ page contentType="text/html;charset=gb"%>
  <%@ page import="javautil*javasql*javatext*javaio*"%>
  
  <html>
  <body>
  <div align="center"><form name="form" method="post" action="logindo"> 
  Welcome Here! <br><br>
  username:<input type="text" name="username"><br> 
  password:<input type="text" name="password"><br> 
<input type="submit" name="submit" value="submit">
<input type="reset"  name="submit" value="reset" onclick="deleteAllMessage()"><br>
</form></div>
</body>
</html>
Ø         reinput_blankjsp
選擇Welcome_SS工程下的WebRoot > New > JSP取名為reinput_blankjspFinish;
 輸入以下代碼
  <%@ page language="java" import="javautil*" pageEncoding="utf"%>
  
  <!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
  <html>
      <body>
     <div align="center"><form name="form" method="post" action="logindo"><font color="#ff">*用戶名和密碼都不能為空!</font><br><br>
      username:<input type="text" name="username" > <br>
      password:<input type="text" name="password" > <br>
      <input type="submit" name="submit" value="submit">
     <input type="reset" name="submit" value="reset" onclick="deleteAllMessage()">
     </form></div>
     </body> 
</html>
 
Ø         reinput_errorjsp
選擇Welcome_SS工程下的WebRoot > New > JSP取名為reinput_errorjspFinish;
 輸入以下代碼
  <%@ page language="java" import="javautil*" pageEncoding="utf"%>
  
  <!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
  <html>
      <body>
     <div align="center"><form name="form" method="post" action="logindo"><font color="#ff">*用戶名或密碼不正確!</font><br><br>
      username:<input type="text" name="username" > <br>
      password:<input type="text" name="password" > <br>
      <input type="submit" name="submit" value="submit">
     <input type="reset" name="submit" value="reset" onclick="deleteAllMessage()">
     </form></div>
     </body> 
</html>
 
Ø         successjsp
選擇Welcome_SS工程下的WebRoot > New > JSP取名為successjspFinish;
 輸入以下代碼
  <%@ page language="java"  pageEncoding="utf"%>
  
  <!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
  <html>
    <head>   
      <!<link rel="stylesheet" type="text/css" href="/stylescss">>
    </head>
    <body>
      <%=requestgetAttribute("message")%>!<br>
     <a href="loginjsp">sign in</a><br>
     <a href="indexjsp">sign out</a>  
   </body>
</html>
Ø         LoginActionjsp
選擇Welcome_SS工程下的src > com > yourcompany > struts下New>files :action再在這個文件夾下New>Java取名為LoginActionjavaFinish;
 輸入以下代碼
  
  package comyourcompanystrutsaction;
  
  import javautilLocale;
  
  import javaxservlet
  import javaxservlet
  import javaxservlet
  
import orgapachestrutsactionAction;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionForward;
import orgapachestrutsactionActionMapping;
import orgapachestrutsutilMessageResources;
 
import comyourcompanystrutsformLoginForm;
 
public class LoginAction extends Action {
     
     private String message;
     
     public String getMessage() {
         return message;
     }
 
     public void setMessage(String message) {
         thismessage = message;
     }    
     
     public ActionForward execute(ActionMapping mapping
             ActionForm form
             HttpServletRequest request
             HttpServletResponse response)            
         LoginForm userform = (LoginForm) form;        
         if(userformgetUsername()equals("")||(userformgetPassword()equals(""))){
             return mappingfindForward("reinput_blank");
         }
         else if(userformgetUsername()equals("success")&&(userformgetPassword()equals("success"))){
             requestsetAttribute("message"getMessage()+userformgetUsername());
             return mappingfindForward("success");
         }
             else{
                 return mappingfindForward("reinput_error");        
             }
         }
     }
 
Ø         LoginFormjsp
選擇Welcome_SS工程下的src > com > yourcompany > struts下New>files :form再在這個文件夾下New>Java取名為LoginActionjavaFinish;
 輸入以下代碼
  
  package comyourcompanystrutsform;
  
  import javaxservlet
  import orgapachestrutsactionActionErrors;
  import orgapachestrutsactionActionForm;
  import orgapachestrutsactionActionMapping;
  
  public class LoginForm extends ActionForm {
 
     /** username property */
     private String username;
     private String password;
     public String getPassword() {
         return password;
     }
     public void setPassword(String password) {
         thispassword = password;
     }
     public String getUsername() {
         return username;
     }
     public void setUsername(String username) {
         thisusername = username;
     } 
}
根據前一步的代碼進行配置文件的編寫(strutsconfigxml和applicationContextxml)
選擇Welcome_SS工程下的WebRoot > WEBINF > strutsconfigxml
輸入下面代碼
  <?xml version="" encoding="UTF"?>
  <!DOCTYPE strutsconfig PUBLIC "//Apache Software Foundation//DTD Struts Configuration //EN" "
  
  <strutsconfig>
    <datasources />
    <formbeans >
      <formbean name="loginForm" type="comyourcompanystrutsformLoginForm" />
    </formbeans>
  
   <globalexceptions />
   <globalforwards />
   <actionmappings >
     <action
       attribute="loginForm"
       input="/loginjsp"
       name="loginForm"
       path="/login"
       scope="request"
       type="orgspringframeworkwebstrutsDelegatingActionProxy">
       <forward name="success" path="/successjsp" />
       <forward name="reinput_blank" path="/reinput_blankjsp"/>
       <forward name="reinput_error" path="/reinput_errorjsp"/>
     </action>
 
   </actionmappings>
 
   <messageresources parameter="comyourcompanystrutsApplicationResources" />
   
   <plugin className="orgspringframeworkwebstrutsContextLoaderPlugIn">
 <setproperty property="contextConfigLocation" value="/WEBINF/classes/applicationContextxml" />
 </plugin>
   
 </strutsconfig>
 
 
選擇Welcome_SS工程下的src > applicationContextxml
輸入下面代碼
  <?xml version="" encoding="UTF"?>
  <!DOCTYPE beans PUBLIC "//SPRING//DTD BEAN//EN" "
  <beans>
    <bean name="/login" class="comyourcompanystrutsactionLoginAction">
    <property name="message">
    <value>Welcome_</value>
    </property>
    </bean>
  </beans>
 
Web Service(這裡即Tomcat)布署及啟動
(全面的可以參照我的另一篇隨筆Myeclipse中Tomcat的兩種布署方式)
a在apachetomcat"conf下找到serverxml打開在</Host>前面添加一句 
 <Context path="/wess" debug="" docBase="C:Documents and   SettingsttworkspaceWelcome_SSWebRoot" reloadable="true"></Context>
C:Documents and SettingsttworkspaceWelcome_SS是該工程的存儲路徑
b在Myeclipse中的工具欄上找到一個像機箱似形狀的按鈕點擊則打開服務器Tomcat
運行
在IE 浏覽器裡輸入http://localhost:/wess即可運行!成功登陸的條件是用戶名和密碼都為success的時候 


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20631.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.