struts2:
upgrade of webwork, not struts1
- struts1:completely encapsulate underlying servlet, has to inherit action superclass, * integrate jstl
- struts2:singleton module, every request has its action object, encapsulate API other than jsp/servlet, decoupling, integrate ognl
##prepare struts packages:
download struts jar packages(under lib directory): http://struts.apache.org/download.cgi#struts23163
- version: struts-2.3.16.1(for example)
- apps:application samples provided by struts2
- docs:API document provided by struts2
- lib :jar packages of struts2
- src :source code of struts2 core packages, xwork packages(webwork core packages)
##three kinds of jar packages of struts2:
- core packages:
- struts2-core-2.3.16.1.jar: ramework’s core class libraries
- xwork-core-2.3.16.1.ja: webwork’s core package
- ognl-3.0.6.ja: ognl expression support package(to read-write object attribute)
- dependent packages:
- freemarker-2.3.19.jar: UI tag module written by freemarker
- commons-logging-1.1.3.jar: log package by ASF(support log4j and jdk)
- commons-fileupload-1.3.1.jar: file upload components
- commons-io-2.2.jar: dependent package of file transfer
- commons-lang3-3.1.jar: enhancement of java.lang package
- asm-3.3.jar: provide read-write bytecode function
- asm-commons-3.3.jar: provide event-based expression
- asm-tree-3.3.jar: provide object-based expression
- javassist-3.11.0.GA.jar: code generate tool
- log4j-1.2.17.jar: log processing
- third-party packages:
other packages to inherit other framework or techniques
##importing flow:
core packages and dependent packages must be imported
- copy-paste all the packages under
struts-2.3.16.1\apps\struts2-blank\WEB-INF\lib
- relate to resource code
webapps/libraries
struts2-core-2.3.31.jar----right click properties,javaSourceAttachment----external location----encoding utf-8----external folder:/struts-2.3.31-all/struts-2.3.31/src/core/src
xwork-core-2.3.31.jar----right click properties,javaSourceAttachment----external location----encoding utf-8----external folder:/struts-2.3.31-all/struts-2.3.31/src/xwork-core/src
###easy sample:
jsp----action——jsp
- configure struts controller(filter) in web.xml:
//StrutsPrepareAndExecuteFilter
<filter>
<filter-name>Struts2(name by yourself)</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPreparedAndExcuteFilter(find StrutsPreparedAndExcuteFilter in struts2-core-2.3.16.1.jar, path is in it)</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2<filter-name>
<url-pattern>/*</url-pattern> - - - -files needed to be filtered
</filter-mapping>
/*
it means every request will be filtered by StrutsPreparedAndExcuteFilter, equivalent to bring struts framework into it.
StrutsPreparedAndExcuteFilter is one of the core classes of struts controllers.
struts controller will analyze every request path, to decide response program.
StrutsPreparedAndExcuteFilter implement init(), server calls on init() when it runs, and read struts configuration file.
*/
- write action:
the returned value of methods must beString
, method name must beexecute()
(you can rename the method name by 2 ways explained afterward)
public class HelloAction{
public String execute(){
return "success"
}
}
/*
unlike struts1 and servlet, struts2 could inherit no superclasses or implement no interfaces
*/
- configuration:
create struts.xml undersrc/
,(name and path can not be changed)
//contents:
<?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">
/*
this version refers to struts-2.3.dtd(in struts2-core-2.3.31.jar)
import tag associate function in eclipse:
window——preferences——XML——XML catalog——add——location, find key type URI in struts-2.3.dtd, which is http://struts.apache.org/dtds/struts-2.3.dtd
*/
<struts>
<package name="hello" namespace="/" extends="(serach for packagename in struts-default)struts-default">
//namespace is virtual path of the package
<action name="helloAction" class="com.controller.HelloAction" >
<result name="success">/helloworld.jsp</result>
</action>
</package>
<struts/>
/*
one action has one package, one package can have multi actions
in JSP, you can access an action by the link of action name(struts absolute path)+namespace(in this case, it’s the root path of the project)
if the namespace is root"/", then you can only write action name, like
<a href="helloAction">click here</a>
*/
- write jsp:
test.jsp:
<a href="helloAction">click on me to</a>
helloworld.jsp
<%@taglib prefix="s" uri="/struts-tags" %>
hello world!
<s: property value="'helloworld'">
tbc....