struts2环境搭建(eclipse)

一:基本配置

1:下载地址:http://archive.apache.org/dist/struts/binaries/

struts2解压后的各个文件:

apps:一些基于struts的示例应用(可以解压其中的文件进行学习,也可以直接在示例应用里获得相关配置文件和jar包)

docs:struts的相关文档

lib:struts2的类库

src:struts2框架的全部源代码

2:配置步骤:

  1:添加jar包

    将必需的jar包复制到lib下(可以直接示例应用里复制,有时在struts2的lib里没有的jar包,就可以在实例应用中找到),

    在struts-2.2.1.1添加下列jar包:

  2:配置struts.xml(从示例文档WEB-INF/classes中直接复制,删除struts标签里的内容),该文件在src下创建

   

<?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>

   
</struts>

  3:配置web.xml

  在<web-app>标签加入以下代码:

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

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

  配置结束以后运行项目,如果没有控制台出现异常则配置成功(此时警告信息为Could not find action or result),若有异常出现,有可能是缺少相关jar包,jar包冲突等,可以根据异常试着去解决

二:一个简单的helloworld

1:创建helloworld.jsp

<body>
hello world!
</body>

2:创建Action类HelloWorld.java

package struts_action;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorld extends ActionSupport{
    public String execute() throws Exception{
        return "success";
    }
}

3:配置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" namespace="/" extends="struts-default">
       <!-- name的值为访问名称,如:http://localhost:8080/项目名/HelloWorld.action ,class的值为包名.类名-->
     <!-- name属性决定该action处理哪个用户请求,class属性决定了该action的实现类-->
<action name="HelloWorld" class="struts_action.HelloWorld" method="execute"> <!-- 定义处理结果和资源之间的映射关系 --> <result name="success">/helloworld.jsp</result> </action> </package> </struts>

 

4:创建一个index.jsp方便访问,也可以在浏览器中直接访问(http://localhost:8080/项目名/HelloWorld.action)

<body>
<a href="HelloWorld.action">请求struts</a>
</body>

 三:一个简单的表单提交

1:创建input.jsp

<body>
<form action="Login.action" method="post">
    账号:<input type="text" name="id"/>
    <br/>
    密码:<input type="password" name="password"/>
    <br/>
    <input type="submit" value="提交"/>
    <input type="reset" value="重置"/>
</form>
</body>

2:创建Action类LoginAction.java

package struts_action;

public class LoginAction {
    private String id;
    private String password;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String execute() throws Exception{
        if("abc".equals(this.getId())&&"abc".equals(this.password)){
            return "success";
        }else{
            return "error";
        }
    }
}

3:创建welcom.jsp

<body>
success
</body>

4:创建error.jsp

<body>
error
</body>

  

5:配置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" namespace="/" extends="struts-default">
        <action name="Login" class="struts_action.LoginAction" method="execute">
            <result name="success">/welcome.jsp</result>
            <result name="error">/error.jsp</result>
        </action>
    </package>
   
</struts>

 

6:运行input.jsp,输入账号“abc”,密码“abc”,登录成功显示success

四:总结

1:配置web.xml中

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

org.apache.struts2.dispatcher.FilterDispatcher从Struts 2.1.3起已被标注为过时

在2,5版本以后写为<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>少了ng

2:struts2的Action是一个包含execute方法的普通Java类,该类里包含了多个属性用于封装用户的请求参数。

为了方便可以实现Action接口,或者继承ActionSupport类,该类实现了Action接口,Validateable接口

3:走出第一步,其它的也就没什么了

 

转载于:https://www.cnblogs.com/labao/p/6900412.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值