程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> struts2入門Demo示例,struts2demo示例

struts2入門Demo示例,struts2demo示例

編輯:關於JSP

struts2入門Demo示例,struts2demo示例


本文講述了struts2入門Demo示例。分享給大家供大家參考。具體如下:

1.新建Web Project, 名稱:struts2Demo;

2.建立一個用戶庫struts2, 包含最少的struts2的最少的6個jar文件;
其實呢, 對於MyEclipse8以上來說, 是不必須的, 因為它直接支持struts2了.不需要另外導包.

3.用Build Path將struts2的庫加進來;

4.在web.xml中加入以下配置:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
  <!-- struts2的配置 --> 
  <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>
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list> 
</web-app>

5.建立Action

package com.yenange.action; 
import com.opensymphony.xwork2.Action; 
public class LoginAction implements Action { 
  private String uname;  //表單中的姓名 
  private String upass;  //表單中的密碼 
  //執行方法 
  @Override 
  public String execute() throws Exception { 
    System.out.println("姓名:"+uname); 
    System.out.println("密碼:"+upass); 
    if (uname.equals("leaf") && upass.equals("leaf")) { 
      return "success"; 
    } 
    return "error"; 
  } 
  //記住, 要有get和set方法 
  public String getUpass() { 
    return upass; 
  } 
  public void setUpass(String upass) { 
    this.upass = upass; 
  } 
  public String getUname() { 
    return uname; 
  } 
  public void setUname(String uname) { 
    this.uname = uname; 
  } 
}

6.三個頁面文件:

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> 
<html> 
  <head> 
    <title>My JSP 'index.jsp' starting page</title> 
  </head> 
  <body> 
    <form action="login"> 
      用戶名:<input type="text" name="uname"><br> 
      密 碼:<input type="text" name="upass"><br> 
      <input type="submit" value="登陸" /> 
    </form> 
  </body> 
</html> 

welcome.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> 
<html> 
  <head> 
    <title>My JSP 'welcome.jsp' starting page</title> 
  </head> 
  <body> 
    welcome struts2
  </body> 
</html> 

fail.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> 
<html> 
 <head> 
  <title>My JSP 'welcome.jsp' starting page</title> 
 </head> 
 <body> 
出錯!
</body> 
</html>

7.配置struts.xml. (對於MyEclipse6來說, 只能從別的地方copy)

<?xml version="1.0" encoding="UTF-8" ?> 
<!DOCTYPE struts PUBLIC 
  "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
  "http://struts.apache.org/dtds/struts-2.0.dtd" > 
<struts> 
  <package name="default" namespace="/" extends="struts-default"> 
    <action name="login" class="com.yenange.action.LoginAction"> 
      <result name="success">/welcome.jsp</result> 
      <result name="error">/fail.jsp</result> 
    </action> 
  </package> 
  <!-- Add packages here --> 
</struts>

8.到此完成.可以運行一下了, 是不是很簡單?

下面是取得request, session, application這些東西的方法.

package com.yenange.action; 
import java.util.Map; 
import javax.servlet.ServletContext; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext; 
import org.apache.struts2.interceptor.ServletRequestAware; 
import org.apache.struts2.interceptor.SessionAware; 
import org.apache.struts2.util.ServletContextAware; 
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action,ServletRequestAware,SessionAware,ServletContextAware { 
  private String uname;  //表單中的姓名 
  private String upass;  //表單中的密碼 
  HttpServletRequest request;  //常用的request 
  Map session;  //常用的session,有不同, 但是不需要管 
  ServletContext application;    //常用的application 
  //第二種取的方法; 
  HttpServletRequest request2 = ServletActionContext.getRequest(); 
  HttpServletResponse response2 = ServletActionContext.getResponse(); 
  HttpSession session2 = request2.getSession();
  @Override 
  public void setServletRequest(HttpServletRequest req) { 
    this.request=req; 
  } 
  @Override 
  public void setSession(Map session) { 
    this.session=session; 
  } 
  @Override 
  public void setServletContext(ServletContext application) { 
    this.application=application; 
  } 
  //執行方法 
  public String execute() throws Exception { 
    if (uname.equals("leaf") && upass.equals("leaf")) { 
      request.setAttribute("userName", uname); 
      return "success"; 
    } 
    request.setAttribute("accessError", uname+"這個用戶或密碼不正確!"); 
    return "error"; 
  } 
  //記住, 要有get和set方法 
  public String getUpass() { 
    return upass; 
  } 
  public void setUpass(String upass) { 
    this.upass = upass; 
  } 
  public String getUname() { 
    return uname; 
  } 
  public void setUname(String uname) { 
    this.uname = uname; 
  } 
}

這些只是初步了, 後面再學習一些關於標簽, 動態方法之類的東西.

希望本文所述對大家的Struts程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved