struts2.0入门

Struts2.0入门
一、Struts2.0原理和工作流程
 采用Struts2.0框架后,用户通过FORM的Post或Get方法向服务器提交数据后,数据不再是提交给服务器端的某一个JSP页面。框架会根据web.xml和Struts.xml配置的内容把数据提交给对应的ActionSupport类处理(会自动对对应的变量赋值)并返回结果(默认用execute()方法处理,可通过struts.xml中的action结点中的mothod属性来设置用什么方法得到返回值),然后框架对照返回结果和struts.xml的配置内容,将相应的页面(这个页面可以是JSP页面,也可以是PDF、Excel或Applet)返回给客户端。
二、用Struts2.0创建HelloWorld
 (一)创建目录结构
  1.创建Struts2Test目录,其下创建文件index.htm,hello.htm,error.htm和目录web-inf。
  2.在web-inf目录下创建文件web.xml和目录classes、lib。
  3.把struts2.0要用的jar包复制到lib目录下。
  4.在classes目录下创建文件struts.xml和HelloAction.java。
 (二)写一个Struts2的HelloWorld , 我们需要做三件事:
  1. 修改首页:index.htm
   <html>
    <head>
     <title>首页</title>
    </head>
    <body>
     <form action="hello.action" mothod="post">
      <input name="msg" type="text">
      <input type="submit">
     </form>
    </body>
   </html>
  2. 修改web.xml文件,添加filter和filtermapping结点
   <?xml version="1.0" encoding="UTF-8"?>
   <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">
    <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>
    <welcome-file-list>
     <welcome-file>/index.htm</welcome-file>
    </welcome-file-list>
   </web-app>
  3. 修改HelloActon.java文件,创建HelloAction类,继承自ActionSupport类
    package org.lzq;
    import com.opensymphony.xwork2.ActionSupport;
    public class HelloAction extends ActionSupport {
     private String msg;
     public String getMsg() {
      return msg;
     }
     public void setMsg(String msg) {
      this.msg = msg;
     }
     public String execute() throws Exception {//这是重载ActionSupport类的方法
      if(msg=="刘志强"){
       return "suc";
      }else{
        return "err";
      }
     }
    }
  4.修改struts.xml文件
   <?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" extends="struts-default">
     <action name="hello" class="org.lzq.HelloAction">//此处可用method属性指定用于求得返回值的方法,默认为execute()
      <result name="suc" >/hello.jsp</result>
      <result name="err" >/error.htm</result>
     </action>
    </package>
   </struts>
  5.修改hello.jsp和error.htm
  hello.jsp
    <%@ page language="java" contentType="text/html; charset=gbk"  pageEncoding="gbk"%>
    <%@ taglib prefix="s" uri="/struts-tags"%>
    <html>
     <head>
      <title>Hello</title>
     </head>
     <body>
       <h2>您好,<s:property value="msg"/></h2>
     </body>
    </html>
  error.htm
    <html>
     <head>
      <title>出错了</title>
     </head>
     <body>
       <h2>你不是合法的用户</h2>
     </body>
    </html>
 
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值