struts2簡單入門示例

struts2其實就是爲咱們封裝了servlet,簡化了jsp跳轉的複雜操做,而且提供了易於編寫的標籤,能夠快速開發view層的代碼。html

  過去,咱們用jsp和servlet搭配,實現展示時,大致的過程是:java

  1 jsp觸發actionweb

  2 servlet接受action,交給後臺class處理apache

  3 後臺class跳轉到其餘的jsp,實現數據展示session

  如今有了struts2,實現過程變爲jsp

  1 jsp出發actionui

  2 struts2攔截請求,調用後臺actionthis

  3 action返回結果,由不一樣的jsp展示數據編碼

  

  下面咱們看下,須要的jar包

  前面兩個是apache commons的jar包,暫且忽略spa

  freemarker提供了另外一種展示方式

  ognl提供了OGNL表達式

  struts2-core提供struts2核心包

  xwork-core因爲struts2不少事基於webwork的,所以也須要這個的核心包

 

  咱們提供了三個jsp

登錄界面login.jsp

複製代碼

1 <%@ page language="java" contentType="text/html; charset=GBK"
 2     pageEncoding="GBK"%>
 3 <%@taglib prefix="s" uri="/struts-tags"%>
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=GBK">
 7 <title><s:text name="loginPage"/></title>
 8 </head>
 9 <body>
10 <s:form action="login">
11     <s:textfield name="username" key="user"/>
12     <s:textfield name="password" key="pass"/>
13     <s:submit key="login"/>
14 </s:form>
15 </body>
16 </html>

複製代碼

登錄成功界面welcome.jsp

複製代碼

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title><s:text name="succPage"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
    <s:text name="succTip">
        <s:param>${sessionScope.user}</s:param>
    </s:text><br/>
</body>
</html>

複製代碼

登錄失敗界面error.jsp

複製代碼

<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
    <title><s:text name="errorPage"/></title>
    <meta http-equiv="Content-Type" content="text/html; charset=GBK">
</head>
<body>
    <s:text name="failTip"/>
</body>
</html>

複製代碼

  當login.jsp觸發action時,就會向後擡發送login.action的請求,這個請求被後臺攔截,交給struts.xml中配置的action處理

複製代碼

1 <?xml version="1.0" encoding="GBK"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
 4     "http://struts.apache.org/dtds/struts-2.1.7.dtd">
 5 <struts>
 6     <!-- 指定全局國際化資源文件 -->
 7     <constant name="struts.custom.i18n.resources" value="mess"/>
 8     <!-- 指定國際化編碼所使用的字符集 -->    
 9     <constant name="struts.i18n.encoding" value="GBK"/>
10     <!-- 全部的Action定義都應該放在package下 -->
11     <package name="test" extends="struts-default">
12         <action name="login" class="com.test.action.LoginAction">
13             <result name="error">/error.jsp</result>
14             <result name="success">/welcome.jsp</result>
15         </action>
16     </package>
17 </struts>

複製代碼

下面是LoginAction的代碼,能夠看到成功登錄後,程序把username寫入session中。以便於咱們在welcome.jsp中利用${sessionScope.user}取得名字

複製代碼

1 package com.test.action;
 2 
 3 import com.opensymphony.xwork2.ActionContext;
 4 import com.opensymphony.xwork2.ActionSupport;
 5 
 6 public class LoginAction extends ActionSupport {
 7     private String username;
 8     private String password;
 9 
10     public String getUsername() {
11         return username;
12     }
13 
14     public void setUsername(String username) {
15         this.username = username;
16     }
17 
18     public String getPassword() {
19         return password;
20     }
21 
22     public void setPassword(String password) {
23         this.password = password;
24     }
25 
26     public String execute() throws Exception {
27         if (getUsername().equals("xingoo") && getPassword().equals("123")) {
28             ActionContext.getContext().getSession().put("user", getUsername());
29             return SUCCESS;
30         }else{
31             return ERROR;
32         }
33     }
34 }

複製代碼

  這裏還要兩個國際化文件,

  mess.properties 

複製代碼

loginPage=loginPage
errorPage=errorPage
succPage=succPage
failTip=sorry,login failed
succTip=welcome{0},login success
user=username
pass=password
login=login

複製代碼

  mess_zh_CN.properties

複製代碼

loginPage=登錄界面
errorPage=失敗界面
succPage=成功界面
failTip=對不起,您不能登陸!
succTip=歡迎,{0},您已經登陸!
user=用戶名
pass=密 碼
login=登錄

複製代碼

  登錄界面

 

 

 

  登錄成功

 

  登錄失敗

相關文章
相關標籤/搜索