Struts2学习日志(2)初体验

安装好环境后,随便写了个测试工程...


1. eclipse新建StrutsHelloWorld工程Dynamic Web Project.

2. 导入Struts2类库,开始按照网上的贴子,都说只要最核心的五个jar包:

struts2-core-2.3.1.2.jar
xwork-core-2.3.1.2.jar
ognl-3.0.4.jar
freemarker-2.3.18.jar
commons-logging-1.1.1.jar
后来建好工程添加完文件后,运行时一步步的发现还需要以下几个jar才能正常启动server:

commons-lang-2.5.jar
commons-io-2.0.1.jar
commons-fileupload-1.2.2.jar
javassist-3.11.0.GA.jar

3. 添加WebContent/WEB-INF/web.xml,并在其中配置 FilterDispatcher过滤器

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">    
    <display-name>struts2</display-name>
    <welcome-file-list>
    	<welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <filter>
    	<filter-name>struts2</filter-name>
    	<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>

4. 添加一个Action类。

一个Action是一段只有特定的URL被请求时才能够被执行的代码。而执行与否,则由web.xml中配置的FilterDispatcher根据URL的请求来操作。Action执行的结果,不论成功与否,通常都对应着一个需要呈现给用户的结果result(一个结果可能是页面、表格或者其它文件)。Action与result的对应关系在struts.xml配置文件中进行配置。

编写Action类时,只要一个普通java类实现了public String execute()的方法即可。通常,选择实现com.opensymphony.xwork2.Action接口。

package com.dotar.strut2.action;

import com.opensymphony.xwork2.Action;

public class HelloAction implements Action {
	private String message;

	public String getMessage() {
		return message;
	}

	public String execute() throws Exception {
		message = "Holy Crab, Struts2!";
		return SUCCESS;
	}
}

5. 编写一个结果页面,作为Action成功后的result。

<%@ page language="java" contentType="text/html; charset=UTF-8"	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello</title>
</head>
<body>
<h1><s:property value="message" /></h1>
</body>
</html>
6. 配置struts.xml:
默认路径WEB-INF/classes/struts.xml,编写时放在工程的src目录下即可。

配置struts.xml之后,FilterDispatcher才知道哪一个URL请求对应哪一个Action,当收到某URL请求时,FilterDispatcher就调用相应的Action进行处理。除了为Action配置一个对应的URL外,还需要为Action配置一个或多个结果页面,当Action执行后,用户面前呈现的就是Action执行后的结果代码对应的result结果(页面或其它)。

<?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>
	<package name="default" extends="struts-default">
		<action name="hello" class="com.dotar.strut2.action.HelloAction">
			<result name="success">/hello.jsp</result>			
		</action>
	</package>
</struts>
上例中,当请求http://localhost:8080/StrutsHelloWorld/hello时,FilterDispatch就会执行HelloAction这个类,当返回结果为success时,页面返回name为success的result标签对应的hello.jsp页面。
7. 简单的编写完后,部署测试。

访问http://localhost:8080/Struts_HelloWorld/hello 

期间遇到的问题

1)Server启动不成功,是因为第2步导入的类库不全;都导入成功后,Server启动成功。

2)报错

严重: Dispatcher initialization failed
Unable to load configuration. - action - file:/D:/eclipse/workspaces%20-%20jee/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/Struts_HelloWorld/WEB-INF/classes/struts.xml:7:69
	at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:69)
	at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:390)

原因是struts.xml中配置的class写错(需要注意,因为是字符串形式,IDE无法进行校验),与我实现编写的java类不同,修改后成功返回

Holy Crab, Struts2! 

附:目录结构


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值