极乐门资源网 Design By www.ioogu.com

本文讲述了struts2入门Demo示例。分享给大家供大家参考。具体如下:

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

struts2入门Demo示例

struts2入门Demo示例

2.建立一个用户库struts2, 包含最少的struts2的最少的6个jar文件;
其实呢, 对于MyEclipse8以上来说, 是不必须的, 因为它直接支持struts2了.不需要另外导包.

struts2入门Demo示例

3.用Build Path将struts2的库加进来;

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

<"1.0" encoding="UTF-8""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)

<"1.0" encoding="UTF-8" "-//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.到此完成.可以运行一下了, 是不是很简单"htmlcode">

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程序设计有所帮助。

标签:
struts2,入门,Demo

极乐门资源网 Design By www.ioogu.com
极乐门资源网 免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
极乐门资源网 Design By www.ioogu.com

评论“struts2入门Demo示例”

暂无struts2入门Demo示例的评论...