Struts2配置

    Struts作用:将视图 和 请求分开

1.导入文件

1)structs.xml --> src //structs配置文件

2)structs类库 --> WEBROOT/WEB-INF/lib

2.配置

1)web.xml

<!-- 配置struts需要添加的filter -->
<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>

2)structs.xml

<constant name="struts.devMode" value="true" /> //打开development mode,则可以立即应用作出的更改

<package name="main" namespace="" extends="struts-default"> 等价于 <package name="main" extends="struts-default">


<!-- 终极方案 -->
<package name="role" namespace="/role" extends="struts-default">

<!-- 只需要一个action即可处理对于 'N个类'的多种请求; {1}->类, {2}->方法 -->
<action name="*_*" class="cn.edu.nuc.action.{1}Action" method="{2}">
<result>
/{1}_{2}.jsp
</result>
</action>
</package>


3.structs对应 MVC 中的Controller:

XXXAction类,实现C层


4.Structs中的路径是根据action所在的路径来确定,而不是jsp页面的路径,所以尽量使用[绝对路径]
<%
//path
是项目名称 ———— Structs2_01
//basePath是【站点名称(localhost:8080)】 + 【项目名称】
= localhost:8080/Structs2_01/
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> //设定当前jsp页面的basePath

<title>hello</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>

<body>
this is front.jsp <br>
<a href="hello.jsp">hello.jsp</a>
</body>
</html>




5. structs的默认属性配置文件default.propertities

struts2-core-2.3.1.jar -> org.apache.struts2 -> default.propertities : struts.i18n.encoding=UTF-8

在 struts.xml中可覆盖 default.propertities中的属性

<constant name="struts.i18n.encoding" value="GBK" />


6. 中文乱码

在表单传递数据时,如果文本框中的内空为 “中文的话,会出现乱码“,解决方法如下:

1、首先在struts.xml文件中加入<constant name="struts.i18n.encoding" value="gbk" />; //在struts2-2.3.1中,貌似不设置也可以

2、即然是传递,至少得有两个页面吧,必须将两个页面编码设为一致,如“GBK”或“UTF-8”,另外还得跟struts.xml文件中的编码保持一致,不然起不到效果。最简单的办法就是:需要传递中文的两个页面、struts.xml文件中设置的编码必须保持一致,否则,嘿嘿嘿。。。

3、表单提交方式为POST

@如果是 jsp 给 action.java 传 中文参数,则jsp 和 action.java 的编码 也要统一

<!--解决方案:

a.Windows -> Preference -> General -> Workspace -> Text file encoding -> UTF-8
b.Windows -> Preference -> 搜索JSP -> encoding -> UTF-8

-->



7. 引用struts标签库
<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- 引入struts标签库:struts-core.jar -> META-INF -> struts-tags.tld -->
<%@taglib uri="/struts-tags" prefix="s" %>
<!--+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->

8. Action


@实现Action常用方式:继承ActionSupport类

@DMI动态方法调用:!add

@通配符配置:*_* {1} {2}

@接收参数方法:使用属性 或者 DomainModel 来接受

@简单参数验证:addFieldError
#一般不是用struts2 UI标签

@访问 Web 元素:
#Map类型:request,session,application
i): IoC:Inverse of Controll[控制反转] || DI:dependency injection[依赖注入] //常用

@Struts2 将 HttpServletRequest --> Map<String, Object> request。
所以在action中,使用的都是 Map类型的 request

@使用Map 类型的 request,session,application

1.实现RequestAware接口,则可直接使用 Map request。即 Ioc 、DI


ii): 依赖struts2

2.没有实现------------,则自行获取:

Map<String, Object> request = (Map)ActionContext.getContext.get("request");
session = ActionContext.getContext().getSession();
application = ActionContext.getContext().getApplication();

#原始类型:HttpServletRequest,HttpSession,ServletContext
i): IoC
ii): 依赖struts2

@包含文件配置:在structs.xml中

<include file="login.xml"/>
<include file="user.xml"/>

@默认action处理


9. Result

@常用type
1.dispatcher //1,2常用
2.redirect
3.chain
4.redirectAction

@全局结果集
globa-results | extends

@传递参数
客户端跳转redirect才需要传参,即user.action -> success.jsp
<action name="user" class="cn.edu.nuc.action.UserAction">
<result type="redirect"> /success.jsp?p=${t} </result>
</action>

${}不是EL


10.在Action中初始化Model类

<!--

JSP/HTML 参数 ————> Action 将参数-> ValueStack(存放Action定义的属性)

-->


@struts自动初始化Model
#前提: 1.User.java中必须有【无参数构造方法】
2.必须向UserAction中的User传值:user.age=8

@手动初始化Model
#User user = new User();

@struts初始化model的过程:<br/> <!-- 跟踪初始化过程方法:在Cat/Dog类构造方法中加System.out.print(cat/dog ---) -->

1.OgnlAction中的Cat类对象cat1<br/>

2.Cat类中Dog对象dog1<br/><br/>


11. <s :debug/> 中显示的valueStack中,什么时候有<a>多个Action</a>
@当<result type="chain">时,ActionA -> (服务器端跳转到) ActionB时;


12. <s :debug/> 中显示

@Value Stack ===> 存放Action的{属性-值}


@Stack Context / ActionContext

#parameters ===> 存放传递给Action的所有参数 <!-- user/user?username=jacky&pwd=123&age=29 -->

13. OGNL:object graph navigation language

@初始化DomainModel:自己new、 给action中的DomainModel对象传参(DomainModel类拥有 参数为空的 构造方法)



<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

开发项目步骤

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-->

1.建立界面原型
2.配置struts.xml
@确定 namespace
@---- package
@---- Action名称,建立空方法
@---- Result
@---- 将界面原型进行修改,匹配现有设置
@---- 测试

3.建立数据库
4.建立Model层
5.建立Service层
@使用JUnit单元测试

6.开发



我在IBM工作,可以为大家内部推荐IBM各种职位 IBM全球职位尽在以下链接(请在浏览器中打开,QQ/微信 会阻止): http://ibmreferrals.com/ 很乐意为感兴趣的小伙伴分享:我的面试经验^_^ 如需咨询,请邮件发送以下邮箱,有问必回 1026096425@qq.com
原文地址:https://www.cnblogs.com/jackydalong/p/2408536.html