zoukankan      html  css  js  c++  java
  • Struts2学习笔记--Struts例子及开发流程

    参考资料:http://blog.csdn.net/hntyzgn2010/article/details/5547753

         http://chenlh.iteye.com/blog/464341

    入门资料:http://wenku.baidu.com/view/91a63c18c281e53a5802fff8.html

      Struts是开源软件。使用Struts的目的是为了帮助我们减少在运用MVC设计模型来开发Web应用的时间。如果我们想混合使用Servlets和JSP的优点来建立可扩展的应用,struts是一个不错的选择。

      Struts 2是Struts的下一代产品,是在 struts 1和WebWork的技术基础上进行了合并的全新的Struts 2框架。其全新的Struts 2的体系结构与Struts 1的体系结构差别巨大。Struts 2以WebWork为核心,采用拦截器的机制来处理用户的请求,这样的设计也使得业务逻辑控制器能够与ServletAPI完全脱离开,所以Struts 2可以理解为WebWork的更新产品。虽然从Struts 1到Struts 2有着太大的变化,但是相对于WebWork,Struts 2的变化很小。

    1. struts2 开发流程

    1.1 配置struts2转发过滤器web.xml

    1.2 创建输入页面login.jsp

    1.3 配置struts2.xml文件

    1.4 编写处理器类Action文件LoginAction

    1.5 编写返回结果JSP页面welcome.jsp和error.jsp

    1.6 将程序发布到Tomcat

    2.实例

    0.创建项目
    工具:MyEclipse
    第一步:新建web project
    第二步:为项目加入Struts 2.0 的jar包
    官方下载地址:http://struts.apache.

    下载得压缩包:

    将一下文件添加到项目目录的lib下

    =======

    也可以这样:

    新建web project-->项目右键-->properties-->MyEclipse-->project facets-->Install Apache struts (2.x)

     

    1. 配置struts2转发过滤器

    编辑web.xml内容

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
      <display-name>StrutsTest2</display-name>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
    </web-app>

    注:“/*”表示涉及本工程的所有浏览器端的请求都经过struts2过滤器处理。

    2. 创建输入页面login.jsp

    login.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head><title>登录界面</title></head>
      <body>
        <form action="LoginAction.action">
           用户名:<input name="username"><br>
           密 码:<input type="password" name="userpass"><br>
          <input type="submit" value="提 交">
          <input type="reset"  value="取 消">
        </form>
      </body>
    </html>

    Struts2动作以.action结尾

    注意:这里的form action要与后面struts.xml中相应的action name对应。

     

    3. 配置struts2.xml文件

    struts.xml

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

    <struts>标签中可以有多个<package>,第一个<package>可以指定一个Servlet访问路径(不包括动作名),如“/mystruts”。
    extends属性继承一个默认的配置文件“struts-default”,一般都继承于它,大家可以先不去管它。
    <action>标签中的name属性表示动作名,class表示动作类名。 
    <result>标签的name实际上就是execute方法返回的字符串,如果返回的是“positive”,就跳转到positive.jsp页面,如果是“negative”,就跳转到negative.jsp页面。
    <struts>中可以有多个<package>,在<package>中可以有多个<action>。

    配置完之后可见如下结构:

    4. 编写处理器类Action文件LoginAction

    LoginAction.java

    package com;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport{
         private String username;
         private String userpass;
         
         public String execute(){
             if("asdf".equals(username)&&"123".equals(userpass))
                 return SUCCESS;
             else
                 return ERROR;
         }
         
         public String getUsername() {
             return username;
         }
         public void setUsername(String username) {
             this.username = username;
         }
         public String getUserpass() {
             return userpass;
         }
         public void setUserpass(String userpass) {
             this.userpass = userpass;
         }
    }

    注:默认配置情况下执行execute()方法,实际应用中经常更改配置。后面将深入讲解。

    getter() setter()是不可少的。

    注意本类中的username和userpass必须和网页文件login.jsp的name属性名一致。

    5. 编写返回结果JSP页面welcome.jsp和error.jsp

    welcome.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>欢迎</title>
      </head>
      <body>
        <font color="red" size="10">登录成功!</font>
      </body>
    </html>

    error.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title></title>
      </head>
      <body>
        <font color="red" size="10">用户或密码错误!</font>
      </body>
    </html>

    6. 将程序发布到Tomcat,启动Tomcat即可。

    我的为:http://localhost:8088/StrutsTest2/login.jsp

    登录界面:

    登录成功:

    登录失败:

     

  • 相关阅读:
    POJ3667 Hotel 题解
    POJ1417 True Liars 题解
    POJ2482 Stars in Your Window 题解
    POJ1704 Georgia and Bob 题解
    矩阵运算
    P4778 Counting Swaps 题解
    poi解析office文档内容的工具类
    VMware安装Centos7超详细过程(图文)
    java后端树形菜单结构
    vue点击事件的修饰符
  • 原文地址:https://www.cnblogs.com/gnivor/p/4340097.html
Copyright © 2011-2022 走看看