星空网 > 软件开发 > Java

Struts2的入门示例

一个简单的Struts2的使用示例,主要功能是检查用户从jsp页面中输入的用户名是否与密码一致。


 

首先是jsp部分的代码。jsp页面做了一个form表单的请求,和AJAX的请求。

Struts2的入门示例Struts2的入门示例
 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="pragma" 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="description" content="This is my page">18   <!--19   <link rel="stylesheet" type="text/css" href="styles.css">20   -->21   <script src='/images/loading.gif' data-original="./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后,根据结果进行跳转,两个跳转页面的代码。

Struts2的入门示例Struts2的入门示例
 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
Struts2的入门示例Struts2的入门示例
 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代码

Struts2的入门示例Struts2的入门示例
 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代码

Struts2的入门示例Struts2的入门示例
 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的配置文件。

Struts2的入门示例Struts2的入门示例
 1 <??> 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的配置文件。

Struts2的入门示例Struts2的入门示例
 1 <??> 2 <web-app ="http://www.w3.org/2001/ ="http:// xsi:schemaLocation="http:// 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.

 


作为初学者,代码中有什么漏洞或者不规范的地方,希望给位大牛多多指教!!!




原标题:Struts2的入门示例

关键词:Struts

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

Jungle Scout Q4 消费者趋势报告(内含福利):https://www.kjdsnews.com/a/1689185.html
“歪嘴龙王”风靡海外:“霸总”是短剧出海最好的密码!:https://www.kjdsnews.com/a/1689186.html
如何看懂noon费用发票?:https://www.kjdsnews.com/a/1689187.html
解密库存迷宫:亚马逊库存管理的妙招一网打尽!:https://www.kjdsnews.com/a/1689188.html
旺季亚马逊仓库状况,巴西站点向中国卖家全面开发:https://www.kjdsnews.com/a/1689189.html
天无绝人之路!TikTok重返印尼!:https://www.kjdsnews.com/a/1689190.html
温州旧货市场有玻璃柜卖吗?:https://www.vstour.cn/a/411246.html
如何用摄影作品表现“芳草鲜美,落英缤纷”的:https://www.vstour.cn/a/411247.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流