eclipse struts2 快速表单入门

准备:

1. eclipse javaEE 4.5版。新建 dynamic web project,选择生成web.xml

2. 改造web.xml

<web-app> 标签下都不要,填入:

<filter>
<filter-name>mystruts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>mystruts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

这是为了通过过滤器机制,拦截web请求,交给struts2来处理。

3. 为支持struts2的基本功能,如下包拷贝到 WEB-INF/lib目录下。

2013/11/23 17:55 43,578 asm-3.3.jar
2013/11/23 17:55 38,275 asm-commons-3.3.jar
2013/11/23 17:55 21,503 asm-tree-3.3.jar
2014/02/19 16:21 69,002 commons-fileupload-1.3.1.jar
2013/11/23 17:55 173,587 commons-io-2.2.jar
2014/01/02 21:45 384,767 commons-lang3-3.2.jar
2015/04/03 07:09 1,300,487 freemarker-2.3.22.jar
2013/11/23 17:55 614,203 javassist-3.11.0.GA.jar
2015/04/19 12:04 133,518 log4j-api-2.2.jar
2015/04/19 12:04 826,732 log4j-core-2.2.jar
2013/11/23 17:55 227,997 ognl-3.0.6.jar
2015/05/03 12:25 831,973 struts2-core-2.3.24.jar
2015/05/03 12:23 676,775 xwork-core-2.3.24.jar

==============================================

开始折腾个小form。

1. 新建一个jsp,用于显示一个表单,但不用HTML标签,而是代之以struts2标签:

其核心部分是:

<s:form action="my1/form1.action" >
<s:textfield name="op1" label=" opo1"/>
<s:textfield name="op2" label=" opo2" />
<s:submit value="sum it" />
</s:form>

这个页面会显示两个输入框,一个提交按钮。

这里<s:form> 将会映射到后台的一个Action类,把op1,op2作为属性设置到action对象中,然后调用action对象的execute方法。

2. 下面是那个Action类的代码(需要继承ActionSupport类):

package gyh.abc;

import com.opensymphony.xwork2.ActionSupport;

public class MyOpForm extends ActionSupport {
    private static final long serialVersionUID = 916882275318473785L;

    private int op1;
    private int op2;
    
    public int getOp2() {
        return op2;
    }
    public void setOp2(int op2) {
        this.op2 = op2;
    }
    public void setOp1(int op1) {
        this.op1 = op1;
    }
    
    public String execute() throws Exception
    {
        if(getOp_sum()>0)
            return "ok";
        else
            return "warn";
    }
    public int getOp_sum(){
        return op1 + op2;
    }
}

这个类的execute方法根据计算结果不同,返回了不同的串,指引进一步选择不同的视图来呈现。

form表达如何关联到这个类呢?这需要在struts.xml中配置。

3. 在src目录下增加一个struts.xml文件,内容如下:

<?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>
<package name="dddd"  namespace="/my1"  extends="struts-default">
        <action name="form1"  class="gyh.abc.MyOpForm">
            <result name="ok">
                /ok.jsp
            </result >
            <result name="warn">
                /warn.jsp
            </result >
            
        </action>
</package>

</struts>

可以看到/my1/form1 或者/my1/form1.action 会对应到gyh.abc.MyOpForm类所创建的Action对象。

该对象先接受form表单传入的参数,然后调用execute,然后根据返回值,以及这里配置决定从哪个页面返回。

当然,返回的jsp页面中一样可以使用struts2标签来和刚才的Action对象交换信息。

4. 创建ok.jsp,以下是 ok.jsp代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>sum=<s:property value="op_sum" /></h1>
</body>
</html>

5. 以下是warn.jsp代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>warning: <s:property value="op_sum" /> </h1>
</body>
</html>

servers上,右键,start,运行服务器,浏览器中输入:

http://localhost:8080/<你的项目名>/myform1.jsp

测试效果。

原文地址:https://www.cnblogs.com/gyhang/p/4610710.html