国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Struts2的入門示例

2019-11-14 15:39:26
字體:
來源:轉載
供稿:網友

一個簡單的Struts2的使用示例,主要功能是檢查用戶從jsp頁面中輸入的用戶名是否與密碼一致。


 

首先是jsp部分的代碼。jsp頁面做了一個form表單的請求,和Ajax的請求。

 1 <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9   <head>10     <base href="<%=basePath%>">11     12     <title>Hello Struts2</title>13     <meta http-equiv=" content="no-cache">14     <meta http-equiv="cache-control" content="no-cache">15     <meta http-equiv="expires" content="0">    16     <meta http-equiv="keyWords" content="keyword1,keyword2,keyword3">17     <meta http-equiv="descr content="This is my page">18     <!--19     <link rel="stylesheet" type="text/CSS" href="styles.css">20     -->21     <script src="./dist/jquery-2.0.0.min.js"></script>22     <style type="text/css">23         input{24             width:100%25         }26     </style>27   </head>28   29   <body>30        <h1>Hello Struts2</h1>31        <div>32            <h2>Form表單</h2>33                   <form action="UserLoginAction.action">34                   <table>35                       <tr>36                           <td>用戶名:</td><td><input type="text" name="username"></td>37                       </tr>38                       <tr>39                           <td>密碼:</td><td><input type="password" name="password"></td>40                       </tr>41                       <tr><td><input type="submit" value="提交"></td></tr>42                   </table>43        </form>44        </div>45        <div>46            <h2>AJAX調用</h2>47            <table>48                <tr><td>用戶名:</td><td><input id="usernames"></td></tr>49                <tr><td>密碼:<td><input id="passwords" type="password"></td></tr>50                <tr><td><button onclick="check()">提交</button></td></tr>51            </table>52            53        </div>54 55   </body>56  57  <script type="text/Javascript">58      function check(){59          $.post("/UserLoginStruts2/UserLoginAction2.action",{60              password:document.getElementById("passwords").value,61              username:document.getElementById("usernames").value,62              63          },function(data,status){64              alert(data);65          });66      }67  </script>68   69 </html>
View Code

 form表單請求action后,根據結果進行跳轉,兩個跳轉頁面的代碼。

 1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9   <head>10     <base href="<%=basePath%>">11     12     <title>My JSP 'fail.jsp' starting page</title>13     14     <meta http-equiv="pragma" content="no-cache">15     <meta http-equiv="cache-control" content="no-cache">16     <meta http-equiv="expires" content="0">    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">18     <meta http-equiv="description" content="This is my page">19     <!--20     <link rel="stylesheet" type="text/css" href="styles.css">21     -->22 23   </head>24   25   <body>26     Fail <br>27   </body>28 </html>
View Code
 1 <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6  7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9   <head>10     <base href="<%=basePath%>">11     12     <title>My JSP 'result.jsp' starting page</title>13     14     <meta http-equiv="pragma" content="no-cache">15     <meta http-equiv="cache-control" content="no-cache">16     <meta http-equiv="expires" content="0">    17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">18     <meta http-equiv="description" content="This is my page">19     <!--20     <link rel="stylesheet" type="text/css" href="styles.css">21     -->22 23   </head>24   25   <body>26       TRUE27   </body>28 </html>
View Code

form表單的請求會根據結果進行跳轉;利用AJAX請求,會將判斷結果,返回給jsp頁面,并提示。


然后是兩個Action部分的代碼。

 form表單所請求的action代碼

 1 package struts; 2  3 import com.opensymphony.xwork2.ActionSupport; 4  5 public class UserLoginAction extends ActionSupport{ 6     private String username,password; 7  8     @Override 9     public String execute() throws Exception {10         System.out.println("Checking...");11         if(this.username.equals(this.password)){12             return "Success";13         }else{14             return "Fail";15         }16     }17 18     public String getUsername() {19         return username;20     }21 22     public void setUsername(String username) {23         this.username = username;24     }25 26     public String getPassword() {27         return password;28     }29 30     public void setPassword(String password) {31         this.password = password;32     }33     34     35 36 }
View Code

AJAX所請求的Action代碼

 1 package struts; 2  3 import org.apache.struts2.ServletActionContext; 4  5 import com.opensymphony.xwork2.ActionSupport; 6  7 public class UserLoginAction2 extends ActionSupport{ 8     private String username,password; 9 10     @Override11     public String execute() throws Exception {12         System.out.println("Checking2...");13         System.out.println(password);14         System.out.println(username);15         System.out.println(password.equals(username));16         String str= this.password.equals(this.username)?"Success":"Fail";17         ServletActionContext.getResponse().getWriter().print(str);18         return null;19     }20 21     public String getUsername() {22         return username;23     }24 25     public void setUsername(String username) {26         System.out.println("SET:"+username);27         this.username = username;28     }29 30     public String getPassword() {31         return password;32     }33 34     public void setPassword(String password) {35         this.password = password;36     }37 38     39     40 }
View Code

Struts的配置文件。

 1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> 3 <struts> 4  5     <package name="struts" namespace="/" extends="struts-default"> 6         <action name="UserLoginAction" class="struts.UserLoginAction"> 7             <param name="username">default</param> 8             <param name="password">defaults</param> 9         <result name='Fail'>/fail.jsp</result><result name='Success'>/true.jsp</result></action>10         <action name="UserLoginAction2"11             class="struts.UserLoginAction2">12             <param name="username"></param>13             <param name="password"></param>14         </action></package></struts>    
View Code

WEB的配置文件。

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> 3   <display-name>UserLoginStruts2</display-name> 4   <filter> 5     <filter-name>struts2</filter-name> 6     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 7   </filter> 8   <filter-mapping> 9     <filter-name>struts2</filter-name>10     <url-pattern>*.action</url-pattern>11   </filter-mapping>12 </web-app>
View Code

通過這個實例,主要是為了熟悉Struts2的基本流程,利用MyEclipse2014開發Struts2的項目還是很方便的。只需要將所需要的Action寫好,同時在struts.xml中配置好Action與具體的類的關系和一些參數信息。就可以了。

 


作為初學者,代碼中有什么漏洞或者不規范的地方,希望給位大牛多多指教!!!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 大理市| 乐东| 内江市| 晋宁县| 金门县| 乌拉特后旗| 台江县| 长治市| 新源县| 宣汉县| 连平县| 卢湾区| 高密市| 曲靖市| 光泽县| 吕梁市| 桐城市| 修水县| 临沭县| 澄迈县| 上高县| 安阳市| 沛县| 基隆市| 南丰县| 利津县| 郴州市| 长岭县| 新巴尔虎右旗| 岗巴县| 肃南| 九江县| 永靖县| 耒阳市| 乌审旗| 兴宁市| 中宁县| 陆川县| 永年县| 桦南县| 衡南县|