Introduction

1.Struts背后的运行机制

a.一个客户端在浏览器地址栏中输入了一个URL地址:http://localhost:8080/struts2/hell,当他输入了这个URL地址之后,这个URL地址的请求会通过HTTP协议发送给我们的Tomcat,Tomcat收到这个请求之后,它看客户请求的是哪个Web Application,然后找到客户请求的Web Application(eg:struts2_0100_introduction),再把该Web Application交给对应的程序处理,既然是这个Web Application, Tomcat必然要读这个Web Application对应的web.xml文件,来看它是怎么配置的,这时候Tomcat就会发现一个配置,这个配置就是<filter>和<filter-mapping>,具体如下:

 

<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>
     <!--对于Struts2,这里永远用(/*)-->
     <url-pattern>/*</url-pattern>
</filter-mapping>

 

 

b.在web.xml中配置了一个filter,这个filter会过滤所有URL地址(/*),所以我们在浏览器地址栏中输入:http://localhost:8080/struts2/hell 时,这个地址就是被web.xml配置文件中org.apache.struts2.

dispatcher.ng.filter.StrutsPrepareAndExecuteFilter产生的对象接受到,它接受到后,①首先看你的struts-config.xml文件中的namespace配置,即和http://localhost:8080/struts2/hell 中的struts2后面的"/"相对应。

②接下来tomcat就会在struts-config.xml中查namespace="/"里面的是不是有一个名为hell的action,如果有,它就会找里面<result>是谁;

一个简单的struts2工程运行机制:


 
 

 

2.namespace决定了action的访问路径,默认为"", 可以接收所有路径的action。

namespace可以为 /  , 或者 /xxx , 或者 /xxx/yyy, 对应的action访问路径为 /index.action, /xxx/index.action, 或者/xxx/yyy/index.action

namespace最好也用模块来进行命名

 

3.修改了struts-config.xml后不能立刻给我反馈,需要重启服务器才能访问成功,怎样才能不需要重启服务器,就能由反馈呢?

在struts-config.xml中配置常量<constant name="struts.devMode" value="true" />

 

4.package作用:Java中package的作用是给我们的类打个包,避免类重名的情况。

在struts2中 package的作用一样,eg:我有2个action,一个叫index,另一个也叫index,怎么给它们作区分呢?可以把它们放到不同的package(包)中

 

5.凡是name为success的result(<result name="success" ></reuslt>),name都可以省略不写,即<result name="success" >/hell.jsp</reuslt>等价于<result>/hell.jsp</reuslt>

 

6.在没找到精确的package的情况下,全部交由namespace=“”的package来处理,也就是说namespace=""的package包括了其它package无法处理的东西;

你找一个action的时候,先到对应的namespace中查找,在对应的namespace中没找到,才会到namespace为空(即namespace=“”)的package中找action,如果都没找到就会报错。

 

7.当你建完JSP文件时,它的默认的编码是ISO-8859-1,你要想改它默认编码的话,是在什么地方?

Window / preferences / Web  /JSP file  修改 Encoding为UTF-8,以后建立的JSP页面都为UTF-8了

 

8.struts2每次访问必定new一个新的action,而struts1是每次访问可能是同一个action。

 

 

 

### Aspect Oriented Programming (AOP) Introduction and Basic Principles #### Definition of AOP Aspect-Oriented Programming represents a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It creates a new way to think about program structure, beyond objects, procedures or functions. Cross-cutting concerns refer to behaviors that span multiple points in an application, such as logging, transaction management, security checks, caching mechanisms, etc.[^1] #### Core Concepts The core concepts within AOP include aspects, join points, pointcuts, advice, introductions, weaving, and interceptors. - **Aspects**: An aspect defines a particular concern affecting many parts of an application. - **Join Points**: These represent specific points during execution where additional behavior can be inserted—such as method calls, exception handling blocks, field access operations, etc. - **Pointcuts**: Pointcuts define precisely which join points should have extra functionality applied; they match one or more join points based on certain criteria defined through expressions. - **Advice**: Advice encapsulates actions taken around join points matched by associated pointcut definitions—it specifies what action needs performing before/after/before-and-after those moments occur. - **Introductions**: Introducing new methods or fields into existing classes without modifying their source code directly allows adding capabilities not originally present while maintaining original class integrity. - **Weaving**: Weaving refers to combining aspects with other pieces of software to create final woven programs ready for deployment. This process happens either statically at compile-time or dynamically at runtime depending upon implementation strategies chosen. - **Interceptors**: Interceptors act similarly to advices but operate specifically over service layer invocations often found inside enterprise applications built following layered architectures patterns like MVC. ```java @Aspect public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void logMethodEntry(JoinPoint joinPoint){ System.out.println("Entering Method : " + joinPoint.getSignature().getName()); } } ``` This example demonstrates how simple it becomes implementing logging across all services under `com.example.service` package using just few lines thanks to powerful features provided by frameworks supporting this approach natively such as Spring Framework's AOP module. --related questions-- 1. What are some common use cases for applying AOP? 2. How does AOP differ from OOP paradigms concerning design principles? 3. Can you provide examples illustrating different types of advice available in AOP implementations? 4. Explain the concept of 'weaving' in relation to AOP and its significance. 5. Discuss potential drawbacks or challenges faced when adopting AOP practices?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值