Strtus

1.修改web.xml,增加过滤器,将所有的请求交给Strtus来过滤

<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>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>       
        <url-pattern>/*</url-pattern>
    </filter-mapping>
 
</web-app>

2.在src目录下新建strtus.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="basicstruts" extends="struts-default">
  
</package>
  
</struts>

3.在类前添加注释:

@Namespace("/")表示访问路径
@ParentPackage("struts-default")表示使用默认拦截器
@Results({@Result(name="show", location="/show.jsp"),
@Result(name="home", location="/index.jsp")})表示根据返回结果跳转到相应的jsp.

方法前添加注释:
@Action("showProduct")表示访问showProduct这个url时调用该方法.

4.常用注释
Namespace:指定命名空间。
ParentPackage:指定父包。

Result:提供了Action结果的映射。(一个结果的映射)
Results:“Result”注解列表
ResultPath:指定结果页面的基路径。

Action:指定Action的访问URL。
Actions:“Action”注解列表。

ExceptionMapping:指定异常映射。(映射一个声明异常)
ExceptionMappings:一级声明异常的数组。

InterceptorRef:拦截器引用。
InterceptorRefs:拦截器引用组。

5.类中应该提供getter和setter,因为返回时Strtus会调用getter将对象放在客户端域.客户端可以方便的用el表达式取用.当客户端使用post方法传参为对应对象的属性时,Strtus会调用对应的setter设置属性.

posted on 2019-08-14 21:35  Best_Efforts  阅读(151)  评论(0编辑  收藏  举报

导航