Struts 2 学习笔记——第一个完整的程序

1、web.xml文件配置

Struts2包装了复杂的jsp页面与servlet之间数据交换的接口,Struts2实际就是一个超级过滤器,所以需要在web.xml文件中配置过滤器

 

<!-- 配置核心过滤器,拦截所有URL请求 -->
<filter>
	<filter-name>struct2</filter-name>
	<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
	<filter-name>struct2</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

 

其中org.apache.struts2.dispatcher.FilterDispatcher就是Struct2的核心过滤器,通过这种方式,把Servlet的逻辑移动到了Filter中

 

2、struts文件配置

Structs2的核心过滤器会拦截所有*.action请求,然后将请求交给对应的类去处理(调用该类的execute方法)。这里的“对应”是通过struts.xml文件来描述的。

默认情况下,会从“\WEB-INF\classes”下加载struts.xml文件,通过Eclipse开发是,在src目录下建立struts.xml文件,发布后会自动拷贝到“\WEB-INF\classes”目录下。

 

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

	<!-- 创建包,该包继承自Struts 2默认包 -->
	<package name="hs" extends="struts-default">
	
		<!-- 配置Action -->
		<action name="helloStruts" class="com.yourcompany.struts2.demo.HelloStrutsAction">
			<result name="success">/success.jsp</result>
			<result name="error">/error.jsp</result>
		</action>
		
	</package>

</struts>

通过这种配置,当helloStruts.action请求到来时,Structs2会自动将它交给"com.yourcompany.struts2.demo.HelloStrutsAction"这个类去处理。

处理方式是:

① 通过反射机制将请求参数注入到"com.yourcompany.struts2.demo.HelloStrutsAction"类中

② 调用"com.yourcompany.struts2.demo.HelloStrutsAction"类的execute方法,该方法的原型为:public String execute() throws Exception;

③ 根据execute方法返回的String类型的字符串,在配置文件中查找该字符串对应的页面,并转到该页面

 

3、Action类的编写

Action类应该包含两部分

① 处理逻辑所需要的属性及相应的get、set方法

② 供Struts2调用的execute方法

可以编写满足这两个条件的类HelloStrutsAction

package com.yourcompany.struts2.demo;

public class HelloStrutsAction {
	
	private String requestMessage;
	private String responseMessage;
	
	public String getRequestMessage() {
		return requestMessage;
	}
	public void setRequestMessage(String requestMessage) {
		this.requestMessage = requestMessage;
	}
	public String getResponseMessage() {
		return responseMessage;
	}
	public void setResponseMessage(String responseMessage) {
		this.responseMessage = responseMessage;
	}
	
	public String execute() throws Exception {
		this.setResponseMessage(this.getRequestMessage());
		if (getRequestMessage().equals("helloStruts")) {
			return "success";
		} else {
			return "error";
		}
	}
}

逻辑很简单,当用户请求时,会读取请求参数requestMessage,并将参数值赋值给返回给用户的属性responseMessage,

同时判断请求参数是否为“helloStruts”,如果是返回字符串“success”(转向到success.jsp),否则返回“error”(转向到error.jsp)

4、编写jsp文件

现在只剩下最后一步,编写相应的jsp文件,首先是success.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    success<br>
    <s:property value="responseMessage"/>
  </body>
</html>

error.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    error<br>
    <s:property value="responseMessage"/>
  </body>
</html>

当页面打开时,只做了一个属性字符串的输出,这里使用了Struts2强大的s标签,直接输出了"com.yourcompany.struts2.demo.HelloStrutsAction"中的属性,

所以在页面头部需要声明<%@ taglib prefix="s" uri="/struts-tags" %>,关于标签的详细使用,可以查看这方面的文章(http://hi.baidu.com/whu_wangmeng/blog/item/77a2ad21f22bcd42935807e0.html

 

5、测试

启动Tomcat,使用浏览器访问“http://localhost:8080/HelloStruct/helloStruts.action?requestMessage=helloStruts”,转到了success.jsp页面;

访问“http://localhost:8080/HelloStruct/helloStruts.action?requestMessage=helloStruts”,转到了error.jsp页面

 

6、遇到的问题

启动Tomcat遇到Unable to load configuration. - bean 异常,解决方法是不要导入所有Struts2的jar包,只导入apps下struts2-blank-2.1.6.war中的几个jar包,其它的需要再导入。(参考http://blog.csdn.net/jsship/article/details/4159908

posted @ 2012-01-16 16:29  石莹  阅读(1422)  评论(0编辑  收藏  举报