zoukankan      html  css  js  c++  java
  • 利用struts2框架开发简单的应用程序

    首先,我们来看看struts2框架处理数据的流程:

    客户端产生一个HttpServletRequest的请求该请求被提交到一系列的过滤器当中,所有的请求都会被前端控制器截获,再根据映射器来确定调用哪一个action,一旦确定后,控制器就会将处理权委托给action代理,action代理就会调用action类中的方法对请求进行处理,最后将结果转发 到相应的页面中。

    下面,我们通过一个一个简单的登录应用来实际体会一下struts2框架的数据流程。

    1、创建一个动态web工程,命名为StrutsTest,将编译路径改为WebContent/WEB-INF/classes,在最后一步选择自动添加we.bxml文件(注意,本例使用的是最新版的eclipse开发工具,版本号3.7.1),struts所需要的jar包复制到WebContent/WEB-INF/lib/下(这里为了方便,全部复制,但一般我们只需复制需要的jar包即可);;

    2、创建一个包,命名为com.action,里面用来存放Action类的;

    3、在包com.action里面创建一个类,命名为LoginAction,代码如下:

    package com.action;

     

    public class LoginAction

    {

    private String userName;

    private String password;

    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 String execute() throws Exception

    {

    if(getUserName().equals("1")&&getPassword().equals("1"))

    {

    return "success";

    }

    else

    {

    return "error";

    }

    }

    }

    4、在scr下创建一个xml系统配置文件struts.xml(编译以后将会自动保存到WebContent/WEB-INF/classes目录下),这个文件中的代码如下:

    <?xml version="1.0" encoding="GB2312" ?>

    <!DOCTYPE struts PUBLIC

        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

        "http://struts.apache.org/dtds/struts-2.0.dtd">

     

    <struts>

    <package name="struts2" extends="struts-default">

    <action name="login" class="com.action.LoginAction">

    <result name="success">/success.jsp</result>

    <result name="error">/error.jsp</result>

     

    </action>

    </package>

    </struts>

    5、修改web.xml中的配置文件,修改后如下所示:

    <?xml version="1.0" encoding="GB2312"?>

    <web-app version="2.4" 

    xmlns="http://java.sun.com/xml/ns/j2ee" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 

    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

     

     

    <welcome-file-list>

    <welcome-file>/login.jsp</welcome-file>

    </welcome-file-list>

    </web-app>

    6、这里我们需要三个页面,分别为登录页面、登录成功页面和登录失败页面,具体代码如下:

    login.jsp代码:

    <%@ page language="java" contentType="text/html; charset=GB2312"

        pageEncoding="GB2312"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=GB2312">

    <title>Insert title here</title>

    </head>

    <body>

    <form action="login.action" method="post">

    username:<input type="text" name="userName"><br>

    password:<input type="password" name="password"><br>

    <input type="submit" value="提交">

    <input type="reset" value="重填">

    </form>

    </body>

    </html>

    success.jsp代码如下:

    <%@ page language="java" contentType="text/html; charset=GB2312"

        pageEncoding="GB2312"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=GB2312">

    <title>登录成功</title>

    </head>

    <body>

    <H1><font color="red">登录成功!!</font></H1>

    </body>

    </html>

    Error.jsp代码如下:

    <%@ page language="java" contentType="text/html; charset=GB2312"

        pageEncoding="GB2312"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=GB2312">

    <title>登录失败</title>

    </head>

    <body>

    <H1><font color="red">登录失败!</font></H1>

    </body>

    </html>

    现在我们就可以部署并且运行这个struts2应用程序了。

  • 相关阅读:
    LVS基于DR模式负载均衡的配置
    Linux源码安装mysql 5.6.12 (cmake编译)
    HOSt ip is not allowed to connect to this MySql server
    zoj 3229 Shoot the Bullet(无源汇上下界最大流)
    hdu 3987 Harry Potter and the Forbidden Forest 求割边最少的最小割
    poj 2391 Ombrophobic Bovines(最大流+floyd+二分)
    URAL 1430 Crime and Punishment
    hdu 2048 神、上帝以及老天爷(错排)
    hdu 3367 Pseudoforest(最大生成树)
    FOJ 1683 纪念SlingShot(矩阵快速幂)
  • 原文地址:https://www.cnblogs.com/jeancandoit/p/2327416.html
Copyright © 2011-2022 走看看