1.创建web项目,添加struts2支持的类库,在web.xml中配置struts2过滤器。

2.创建名为UserAction的Action对象,并在其中编写execute()方法,代码如下所示:

package com.wh.Demo;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {
private String info;

public String getInfo() {
return info;
}

public void setInfo(String info) {
this.info = info;
}

@Override
public String execute() throws Exception {
if ("".equals(info)) {
return "error";
} else {
return "success";
}
}
}

3.在源文件夹中创建Struts.xml配置文件,在其中配置UserAction,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
<package name="default" extends="struts-default" namespace="/user">
     <action name="userAction" class="com.wh.Demo.UserAction">
            <result name="success">success.jsp</result>
            <result name="error">error.jsp</result>
     </action>
</package>
</struts>

4.创建index.jsp主页面

<body>
<form id="form1" name="form1" method="post" action="userAction.action">
    输入信息:<input type="text" name="info" id="username" />
    <input type="submit" name="submit" value="提交">
</form>
</body>

5.创建success.jsp页面

<%@taglib prefix="s" uri="/struts-tags" %>

Successful!!!

<s:property value="info"/>

6.创建error页面

<h1>404找不到页面</h1>

Struts2 表单提交与execute()方法的结合使用的更多相关文章

  1. 项目总结07:JS图片的上传预览和表单提交(FileReader()方法)

    JS图片的上传预览和表单提交(FileReader()方法) 一开始没有搞明白下面这块代码的,今天有时间简单整理下 核心点:FileReader()方法 以下是代码(以JSP文件为例) <!DO ...

  2. struts2表单提交的乱码的问题的解决

    今天碰到一乱码问题,百思不得其解. 最后解决办法是设置了表单的提交方式,将method设置为post,解决问题.虽然默认的提交方式是post.但是如果不显式设置的话,就会出现我所出现的问题. 总结下处 ...

  3. struts2表单提交Date数据无法接收

    问题:在Struts2环境下,提交含有Date类型数据表单,但是在action中没有接收到:String就可以直接接收到: --网络搜索后,说Struts2可以自己转,但是目前没发现有: 然后在狂搜, ...

  4. tp框架表单提交注意!不要提交到当前方法

    tp框架  表单提交到当前方法,会重复执行显示部分和保存部分的代码.导致不知名的错误.

  5. struts2表单验证

    1:采用手工编写代码实现. 通过继承ActionSupport类,然后重写vlidate方法,validate方法会校验跟execute同样签名的方法,当某个数据校验失败时,我们应该调用addFiel ...

  6. 阻止form空表单提交----JavaScript

    网上看到很不错的阻止form空表单提交 第一种方法 <div class="warp"> <h2>登录到pfan空间</h2> <p> ...

  7. form表单提交信息的方式

    form表单提交信息的方法有两种,一种是get,一种是post.get提交的数据是在地址栏上提交,一般隐私数据不会选择这样方式.地址栏上只能提交字符. 如果使用的是post提交,那么信息都在消息正文中 ...

  8. JavaWeb -- Struts2,对比, 简单表单提交,校验,防重复提交, 文件上传

    Struts2核心流程图 1. Struts2 和 Struts1 对比 struts1:基于Servlet(ActionServlet),actionForm众多(类的爆炸),action单例(数据 ...

  9. Struts2 03---数据封装+获取表单提交数据

        Struts的数据封装分为三种:属性封装,模型驱动,表达式封装.下面以获取表单提交数据来简单介绍一下Struts的数据封装. <form action="loginlogin. ...

随机推荐

  1. [RxJS] Implement the `map` Operator from Scratch in RxJS

    While it's great to use the RxJS built-in operators, it's also important to realize you now have the ...

  2. [Unit Testing] Unit Test a Function that Invokes a Callback with a Sinon Spy

    Unit testing functions that invoke callbacks can require a lot of setup code. Using sinon.spy to cre ...

  3. protobuf 之 MessageLite 接口摘录

    class LIBPROTOBUF_EXPORT MessageLite { public: inline MessageLite() {} virtual ~MessageLite(); // Ba ...

  4. OpenCV实践之路——Python的安装和使用

    本文由@星沉阁冰不语出品,转载请注明作者和出处. 文章链接:http://blog.csdn.net/xingchenbingbuyu/article/details/50936076 微博:http ...

  5. Memory Analysis环境安装

    安装MAT(MAT在eclipse的页面:http://www.eclipse.org/mat/downloads.php) 显示饼图的时候,需要安装BIRT Chart Engine插件,通过Ins ...

  6. 嵌入式驱动开发之--- 虚拟磁盘SBULL块设备驱动程序分析

     #define SBULL_MINORS  16         /* 每个sbull设备所支持的次设备号的数量 */  #define KERNEL_SECTOR_SIZE 512  // 本地定 ...

  7. VC2010 利用 def 文件生成 dll 文件的方法

    近期有个需求,要生成一个dll 文件.文件里的函数都是採用 stdcall 函数调用约定,可是不希望函数名被修饰(add 被修饰成 add@8). 这时就要用def 文件了. 比方我有以下两个函数: ...

  8. php判断手机号码

      //PHP判断手机号码 public function isMobile($params) { $pattern = "/^(13[0-9]|14[0-9]|15[0-9]|17[0-9 ...

  9. ajax跨域问题解决(spring boot)

    之前用的服务器响应头部添加Access-Control-Allow-Origin: *来解决的 public static void setResp(HttpServletResponse resp) ...

  10. java操作json

    import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class ReadJson { public static v ...