Struts2入门先知

来源:互联网 发布:vb label 换行 编辑:程序博客网 时间:2024/04/29 05:25
  • Struts2应用可以不依赖Servlet api和struts api,属于无侵入式设计
  • Struts2是WebWork2基础上发展而来的,属于MVC框架
  • Struts2提供了拦截器,可进行AOP编程,实现权限拦截功能
  • Struts2提供类型转换器
  • Struts2提供支持多种表现层技术,如JSP,freeMarker,Velocity等
  • Struts2可对指定方法进行校验
  • Struts2提供全局范围、包范围、action范围的国际化资源文件管理实现

配置过程



开发Strut2最少要用到的jar包

具体可到其官网下载:http://struts.apache.org
commons-fileupload-1.3.1.jar:文件上传组件,2.1.6版本后必须加入此文件
commons-logging-1.1.3.jar:Struts2框架使用这个日志包来支持Log4J和JDK1.4+的日志记录
freemarker-2.3.19.jar:Struts2的UI标签模板使用freeMarker编写
ognl-3.0.6.jar:对象图导航语言,Struts2框架通过其读写对象的属性
struts2-core-2.3.20.jar:Struts2框架的核心类库
xwork-core-2.3.20.jar:Xwork类库,Struts2在其上构建


struts配置文件struts.xml应放在WEB-INF  -> class文件夹下,在开发阶段放在src下就可以了

<?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><!-- Action必须放在指定的包名空间中,package用来管理一群业务功能相同的action --><!-- name可取任意值,但必须是唯一的 --><!-- namespace可作为访问action路径的一部分,有助于减少重复代码 --><package name="hotel" namespace="/test" extends="struts-default"><!-- 两个示例action配置 --><action name="queryVipAll" class="com.dong.action.VipAction"method="queryVipAll"><result name="success">/admin/vip_manage/vipManage.jsp</result></action><action name="deleteVip" class="com.dong.action.VipAction"method="deleteVip"><result name="success" type="chain">queryVipAll</result><!-- queryVipAll为一个类 --></action></package></struts>

这个配置内容都一样,用到的时候可直接copy,不用每次都手写

当前用到的struts是2.3.20版本的。在Struts2中,struts框架是通过Filter启动的,在web.xml中的配置如下

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"version="3.0"><filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class><!-- 自从Struts 2.1.3以后,下面的FilterDispatcher已经标注为过时 --><!-- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> --></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

当然如果项目后面用到自定义的过滤器,也要在这里定义

在struts.xml里打"<"没有出现提示符,原因是没有找到http://struts.apache.org/dtds/struts-2.0.dtd这个文件
这时机器没有上网,无法下载到这个文件。这时可将已在官网下载的struts-2.0.dtd文件加到
window->preference->myclipse->field and editor->xml->xml catalog
add file system
Key Type: URI
Key:  http://struts.apache.org/dtds/struts-2.0.dtd
这时如果struts.xml如果出现红叉,在<struts>后面按下回车再保存,红叉就会消失




Struts2中路径查找顺序



Action各项默认值配置



浏览器重定向就是引导用户到指定的路径。重定向到某个路径:

<action name="redirect">    <result type="redirect">/employeeAdd.jsp?username=${username}</result></action>
URL的参数为中文的话

this.username = URLEncoder.encode("中文的参数","UTF-8");

在jsp页面获取传递过来的数据

${param.username}
<%=URLDecoder.decode(new String(request.getParameter("username").getBytes("ISO8859-1"),"UTF-8"),"UTF-8")%>

重定向到某个action:

<action name="redirectAction">    <result type="redirectAction">actionName</result></action>

重定向到别的命名空间里的action

<action name="redirectAction"><result type="redirectAction"><param name="actionName">xxx</param><param name="namespace">/...</param></result></action>

原样输出视图的代码

<action name="plainText">    <result type="plainText">/index.jsp</result></action>

<action name="plainText"><result type="plainText"><param name="location">/index.jsp</param><param name="charSet">UTF-8</param></result></action>

全视图

<global-results><result name="message">路径</result></global-results>

待续


1 0
原创粉丝点击