struts2的第一个例子

示例:struts2-blank.war


步骤:

1.创建web工程:Dynamic Web project

2.导入jar包:从示例工程中lib中直接拷贝,。。。

u     struts2-core-2.3.1.1.jar:Struts 2框架的核心类库

u     xwork-core-2.3.1.1.jar:Command模式框架,WebWork和Struts2都基于xwork

u     ognl-3.0.3.jar:对象图导航语言(ObjectGraph Navigation Language),

u                             struts2框架通过其读写对象的属性

u     freemarker-2.3.18.jar:Struts 2的UI标签的模板使用FreeMarker编写

u     commons-logging-1.1.x.jar:ASF出品的日志包,Struts2框架使用这个日志

u                                                  包来支持Log4J和JDK 1.4+的日志记录。

u     commons-fileupload-1.2.2.jar: 文件上传组件,2.1.6版本后需要加入此文件

u     commons-io-2.0.1.jar:传文件依赖的jar包

u     commons-lang-2.5.jar:对java.lang包的增强

u     javassist-3.11.0.GA.jar是一个开源的分析、编辑和创建Java字节码的类库


3.写jsp页面:在WebContext下new一个。

4.编写Action服务器端处理逻辑

Action就是处理request请求的动作类,类似servlet

package cn.tx.action;

public class helloAction {

	public String hello(){
		return "success";
	}
}
 
 

5.进行框架配置:web.xml、struts.xml

web.xml文件位于web-info文件中

struts.xml文件位于src中

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <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>


</web-app>


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="hello" extends="struts-default">
		<action name="hello"//访问时就写项目名+hello class="cn.tx.action.helloAction" method="hello">
			<result name="success">success.jsp</result>
		</action>
	
	</package>

	
</struts>

完成。进行测试:

http://localhost:8080/struts2_01/hello

http://88kan.cn






猜你喜欢

转载自blog.csdn.net/qq_39203959/article/details/80230588