public class HelloAction { public String hello() { System.out.println("hello world"); return "success"; } }
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts> <package name="hello" namespace="/hello" extends="struts-default"> <action name="HelloAction" class="com.itheima.web.HelloAction" method="hello"> <result name="success">/hello.jsp</result> </action> </package> </struts>
name: 随意填,这里是包名,可以被继承。
namespace: 内容为访问的action名。这个要和前端的访问路径要一致,否则会找不到。
extends: 表示继承那个包,如果继承了谋个包,那么该包里的所有配置都会被继承。这里继承了struts-default包
注意: 对于namespace问题,这样想,首先通过url访问,会被拦截器拦截,拦截器根据url去匹配namespace,比如:httt://localhost: 8080/Struts2/test/test2/testMap.action 这个拦截器会到struts.xml去找namespace为/test/test2的package ,如果找不到,就去找namespace为/test的package,如果还是找不到这样的package,就会去找namespace值为/的namespace,这个是默认的,就会去找里面是否有testMap.action,如果没找到,就报没有/test/test2的错。参考文章
一定要注意拦截器寻找的是namespace,而不是某个action.
name: 为Action类名。
class: 为Action类全限名。
method: 为本次访问要访问的这个类的那个方法。
name: 为return返回后的值,如果为该值信息,就转向该result包裹内的结果。
type: 指定调用哪一个result类来处理结果,默认使用转发.
标签体: 填写页面的相对路径
点击前端–>生成url访问路径–>过滤器过滤url–>过滤器拦截Action–>从struts.xml中查找该Action–>进而找到该类–>进而找到该类的方法–>处理方法,返回结果–>转向result包裹的内容。
<filter> <filter-name>struts2</filter-name> <filterclass>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
一、 在src下添加struts.properties文件二、 在struts.xml中添加常量配置,一般选择在此处配置常量。
三、 在web.xml中添加常量配置
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.action.extension" value="action"></constant>
<constant name="struts.devMode" value="true"></constant>
<action name="CustomerAction_*" class="cn.it.web.action.CustomerAction" method="{1}" > <result name="list" >/jsp/customer/list.jsp</result> </action>
public class CustomerAction extends ActionSupport {}