struts2概念:处理访问服务器的请求.
struts2优势:自动封装参数 参数校验 结果的处理(转发|重定向) 国际化 显示等待页面 表单的防止重复提交
搭建:拷包 打开解压后的struts2->apps(示例项目)->解压后缀为blank->拷贝lib文件的jar包
创建action类:
package com.java;
//创建一个普通方法
public class HelloAction {
public String hello(){
System.out.println("hello world!");
return "success";
}
}
创建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="hello" namespace="/hello" extend="struts-default">//name为区别其他package,namespace为命名空间,extend继承内置xml
<action name="haha" class="com.java.HelloAction" method="hello">//name为action名,class填写操作的类的完整类名,method使用class元素的类的方法
<result name="success">/index.jsp</result>//转发到/index.jsp
</action>
</package>
</struts>
把struts配置到核心过滤器配置到web.xml
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
访问路径:http://localhost:8080/项目名/hello/haha(项目名+namespace+action名)