您的位置:首页 > 编程语言 > Java开发

Structs2认识和入门

2016-06-18 16:40 387 查看


Structs2认识和入门


Structs2认识

Struts2是处于MVC中的controller(struts其实也是servlet封装,提高开发效率!)

Struts2 是一个用来开发MVC应用程序的框架.它提供了Web应用程序开发过程中的一些常见问题的解决方案:

Struts2 = Struts1 + WebWork 1和2没有本质的关系
对页面导航活动进行管理
对来自用户的输入数据进行合法性验证
统一的布局
可扩展性
国际化和本地化
支持 Ajax
表单的重复提交

查看Struts2API,com包为webwork的,org开头的为struts1的。


Structs2入门


Struts开发步骤:

web项目,引入struts - jar包
web.xml中,引入struts的核心功能 配置过滤器
开发action
配置action src/struts.xml


实例

引入8个jar文件
web.xml
<!-- 引入struts核心过滤器 -->
<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>

开发action
// 开发action: 处理请求
public class HelloAction extends ActionSupport {

// 处理请求
public String execute() throws Exception {
System.out.println("访问到了action,正在处理请求");
System.out.println("调用service");
return "success";
}
}

配置action : src/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="xxxx" extends="struts-default">
<action name="hello" class="cn.csx.action.HelloAction" method="execute">
<!--如果成功,则跳转至success.jsp页面-->
<result name="success">/success.jsp</result>
</action>
</package>

</struts>

访问http://localhost:8080/struct_test/hello
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息