码迷,mamicode.com
首页 > 其他好文 > 详细

Struts2的入门示例

时间:2015-09-12 10:54:23      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

一个简单的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="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="./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       TRUE
27   </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     @Override
11     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与具体的类的关系和一些参数信息。就可以了。

 


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

Struts2的入门示例

标签:

原文地址:http://www.cnblogs.com/firmhappy/p/4802613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!