Struts2 Annotation配置(要jar包,看这里)

  Struts 2 Annotation在官网上叫零配置文件(就是可以没有struts2.xml),通过java annotation注释来代替struts2.xml文件。
这是官网上的资料,可以参考下
      我现在知道什么叫做摸石头过河了,网上关于Struts2 Annotation的配置基本上没有,一些视频也没有详细的说明配置中出现的问题,经过一上午的努力,终于配好了,在这把出现的问题分享下:
1:修改web.xml配置文件:
这个文件在WEB-INF下,记住一定别忘了<filter-mapping>标签。不然struts2的过滤器就相当于没开了
<param-name>actionPackages</param-name>这是名称是默认的(不可以改的)
要改的是下边的:<param-value>action</param-value> 里的内容是你写的action所在的包路径
(就是那个action的在java中的包)
比如你在com.lovo.yfx包下建了一个Action的类那么就得写成
<param-value>com.lovo.yfx</param-value>
----------------------------------------------web.xml-----------------------------------------------
<filter>
        <filter-name>struts2</filter-name>
        <!--<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>-->
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <init-param>    
    <param-name>actionPackages</param-name>
    <param-value>action</param-value>
  </init-param>
</filter>
<filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
-----------------------------------------web.xml-----------------------------------------------------------------
2:也是网上很少有人说的,就是Struts2 Annotation的jar包在哪在,
在官网上下一个struts2的完全ga版,打开其中的lib,找到struts2-codebehind-plugin-2.1.8.1.jar(可能后边的名字有点出入)放入你项目的lib中,就ok了;
看是不是这个jar包的方法是看里面有没
org.apache.struts2.config.Action;
org.apache.struts2.config.Namespace;
org.apache.struts2.config.Result;
类,这个jar我可是找了好久
-----------------------------------------------------------------------------------------------------------------------------
然后就是写类了
在action的包里我写一个最简单的类如下:
---------------------------------------- package action-----------------------------------------
package action;//这个action就是<param-value></param-value>要写的
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Namespace;
import org.apache.struts2.config.Result;import com.opensymphony.xwork2.ActionSupport;
@Namespace("/action")
@Action(name = "action")
@Result("/login.html")
public class LoginAction extends ActionSupport{    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
    
}
---------------------------------------------------------------------------------------------------------------------------
还有两个html没写,想写什么是什么,这个就不用说了
访问http://localhost:8080/项目名/action/action就可以转到login.html了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值