Struts 2

Struts 2是一個MVC框架,以WebWork框架的設計思想爲核心,吸取了Struts 1的部分優勢.Struts 2擁有更加廣闊的前景,自身功能強大,還對其餘框架下開發的程序提供很好的兼容性。下面咱們瞭解一下syruts2的應用html

1.1引入架包java

1.2建立loginAction類web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package  cn.happy.action;
 
import  java.util.Map;
 
import  javax.servlet.http.HttpSession;
 
import  org.apache.struts2.ServletActionContext;
import  org.apache.struts2.interceptor.SessionAware;
 
import  com.opensymphony.xwork2.Action;
import  com.opensymphony.xwork2.ActionContext;
 
public  class  LoginAction  implements  Action,SessionAware{
     private  Map<String,Object> map;
     private  String username;
     private  String password;    <br>      //自動裝配
     <strong> public  String execute()  throws  Exception {
         if (username.equals( "1" )&&password.equals( "1" )){
             
             //解耦方式 (對Servlet Api進行封裝 藉助ActionContext)
             Map<String,Object> session=ActionContext.getContext().getSession();
             session.put( "uname" , username);
             //耦合方式
//          HttpSession session2 = ServletActionContext.getRequest().getSession();
//          session2.setAttribute("uname",getUsername());
             
             
             return  SUCCESS;
         } else {
             return  ERROR;
         }
         
     }</strong>
 
     
     public  String getUsername() {
         return  username;
     }
 
     public  void  setUsername(String username) {
         this .username = username;
     }
 
     public  String getPassword() {
         return  password;
     }
 
     public  void  setPassword(String password) {
         this .password = password;
     }
 
 
     public  void  setSession(Map<String, Object> map) {
         this .map=map;
     }
 
}

1.3建立struts.xml文件apache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'<?xml version= "1.0"  encoding= "UTF-8"  ?>
<!DOCTYPE struts PUBLIC
     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     "http://struts.apache.org/dtds/struts-2.3.dtd" >
 
<struts>
 
     <!-- 修改文件 tomact不用重啓 -->
     <constant name= "struts.devMode"  value= "true"  />
 
     < package  name= "default"  namespace= "/"  extends = "struts-default" >
 
         <!-- login action -->
         <strong><action name= "LoginAction"  class = "cn.happy.action.LoginAction" >
             <result name= "success" >login/success.jsp</result>
             <result name= "login" >login/login.jsp</result>
             <result name= "error" >login/error.jsp</result>
         </action></strong>
 
 
         <!-- 第一個action -->
         <action name= "HelloWordAction"  class = "cn.happy.action.HelloWordAction" >
             <result name= "success" >index.jsp</result>
         </action>
 
 
     </ package >
 
</struts>

1.4配置web.xml文件session

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version= "1.0"  encoding= "UTF-8" ?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"  xmlns= "http://java.sun.com/xml/ns/javaee"  xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  xsi:schemaLocation= "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  version= "2.5" >
   <display-name></display-name>
   <strong><filter>
     <filter-name>struts2</filter-name>
     <filter- class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter- class >
   </filter>
   <filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
   </filter-mapping></strong>
   <welcome-file-list>
     <welcome-file>login/login.jsp</welcome-file>
   </welcome-file-list>
</web-app>

1.5編寫JSP頁面app

1.6在這裏就展現一下登陸頁面與登陸失敗頁面框架

login.jspjsp

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<%@ page language= "java"  import = "java.util.*"  pageEncoding= "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
<% @taglib  uri= "/struts-tags"  prefix= "s"  %>
<!DOCTYPE HTML PUBLIC  "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
   <head>
     <base href= "<%=basePath%>" >
     
     <title>登陸頁面</title>
     
     
   </head>
   
   <body>
     <s:form name= "form1"  namespace= "/"  method= "post"  action= "LoginAction" >
     請輸入用戶名:
     <s:textfield name= "username" ></s:textfield><br/>
       請輸入密碼:
     <s:textfield name= "password" ></s:textfield>
     <s:reset value= "重填" ></s:reset>
     <s:submit value= "登錄" ></s:submit>
     </s:form>
   </body>
</html>

在jsp中用到了Struts2 標籤ide

引入post

1
<span style= "color: #ff0000;" ><strong><% @taglib  uri= "/struts-tags"  prefix= "s"  %></strong></span>

通用標籤(條件,迭代)

 

1.7 success.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<%@ page language= "java"  import = "java.util.*"  pageEncoding= "utf-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+ "://" +request.getServerName()+ ":" +request.getServerPort()+path+ "/" ;
%>
 
<!DOCTYPE HTML PUBLIC  "-//W3C//DTD HTML 4.01 Transitional//EN" >
<html>
   <head>
     <base href= "<%=basePath%>" >
     
     <title>My JSP  'error.jsp'  starting page</title>
 
   </head>
   
   <body>
         <h1>登陸失敗</h1>
    
         <h3>用戶名或密碼錯誤,請從新<a href= "login/login.jsp" >登陸</a></h3>
         <script>
var t= 10 ; //設定跳轉的時間
setInterval( "refer()" , 1000 );  //啓動1秒定時
function refer(){
if (t== 0 ){
location= "http://localhost:8080/Day-login2-struts2/login/login.jsp" ; //跳轉的連接地址
}
document.getElementById( 'show' ).innerHTML= "" +t+ "秒後跳轉到登陸" // 顯示倒計時
t--;  // 計數器遞減
}
</script>
    <span id= "show" ></span>
     
   </body>
</html>

1
 

1.8結果展示

1.9登陸成功   用戶名:1 密碼:1

1.10 登陸失敗     10秒後會跳會登陸

 

 2.0拓展

當咱們用到的屬性多的時候都寫在loginAction類中就會感受到特別的凌亂,這個時候咱們就能夠建立一個類來管理這些屬性(例:user)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package  cn.happy.entity;
 
public  class  User {
     private  String name;
     private  int  age;
     
     public  int  getAge() {
         return  age;
     }
     public  void  setAge( int  age) {
         this .age = age;
     }
     public  String getName() {
         return  name;
     }
     public  void  setName(String name) {
         this .name = name;
     }
     
     
     private  String username;
     private  String password;
     public  String getUsername() {
         return  username;
     }
     public  String getPassword() {
         return  password;
     }
     public  void  setUsername(String username) {
         this .username = username;
     }
     public  void  setPassword(String password) {
         this .password = password;
     }
     
 
}

這時候只在loginAction類中植入這個類就好了

1
2
3
4
5
6
7
private  User user;
     public  User getUser() {
         return  user;
     }
     public  void  setUser(User user) {
         this .user = user;
     }

這樣代碼就會顯得更加的清晰,歲然這樣作會讓咱們的眼前一亮,事物都有兩面性有利就有弊。咱們用到的屬性名前面都要加上管理它們類的名稱(如:user.getUsername)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public  class  LoginAction  implements  Action,ModelDriven<User>{
 
     @Override
     public  String execute()  throws  Exception {
         if (user.getUsername().equals( "1" )&&(user.getPassword().equals( "1" ))){
             return  SUCCESS;    
         } else {
             //失敗回到登陸
             return  LOGIN;
         }
     }
     private  User user;
     public  User getUser() {
         return  user;
     }
     public  void  setUser(User user) {
         this .user = user;
     }
     @Override
     public  User getModel() {
         // TODO Auto-generated method stub
         return  user;
     }
}

在1.2中咱們能夠看到加粗字體的語句解耦與耦合的應用(在下一篇博客中會有詳細解釋 解耦與耦合的你我他)

1
2
3
4
5
//解耦方式
Map<String,Object> session=ActionContext.getContext().getSession();<br>   session.put( "uname" , username);
//耦合方式
HttpSession session2 = ServletActionContext.getRequest().getSession();
   session2.setAttribute( "uname" ,getUsername());

  

首先咱們先要在登陸成功頁面配置一道(success.jsp)

1
2
3
4
<body>
         歡迎你!${uname}
 
  </body>

實現效果         用戶名爲1 

相關文章
相關標籤/搜索