今天开始接触struts2的知识,了解了一下搭框架的流程,

   步骤一:在web.xml中配置拦截器。

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!-- 配置拦截器 -->
	<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>		
  <welcome-file-list>
    <welcome-file>queryTitleName.jsp</welcome-file>
  </welcome-file-list>
</web-app>

步骤二:新建一个包(com.zrp.dao),在这个包下,写一个处理数据的类(BlogDao),
  
package com.zrp.dao;
import java.util.*;

/**
 * 数据处理类
 * @author Administrator
 *
 */
public class BlogDao {
	/**
	 * Map对象用于保持主题信息
	 */
    private static Map<String,Integer> titleName = new LinkedHashMap<String, Integer>();
    static
    {
    	titleName.put("我爱编程", 28);
    	titleName.put("程序员人生", 7);
    	titleName.put("软件工程师", 8);
    	titleName.put("学习java", 6);  
    	titleName.put("学习struts2", 100);
    	titleName.put("学习Silverlight", 99);
    }
    /**
     * 根据模糊查询,实现检索数据
     */
    public Map<String,Integer> getTitleName(String name)
    {
        Map<String,Integer> titleName = new LinkedHashMap<String, Integer>();
        //对Map对象进行扫描
        for(Map.Entry<String, Integer> entry:BlogDao.titleName.entrySet())
        {
        	if(entry.getKey().toLowerCase().contains(name.toLowerCase()))
        	{
        		titleName.put(entry.getKey(), entry.getValue());
        	
        	}       	
        }
        return titleName;
    }    	
}

步骤三:新建一个com.zrp.action包,顾名思义,写关于Action的类(QueryAction)
 
package com.zrp.action;

import com.zrp.dao.BlogDao;
import com.opensymphony.xwork2.*;
import javax.servlet.http.*;
import java.util.*;

public class QueryAction {

	private String name;
	private Map<String,Integer> result;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Map<String, Integer> getResult() {
		return result;
	}
	public void setResult(Map<String, Integer> result) {
		this.result = result;
	}
	public String excute() throws Exception
	{
		try
		{
			//HttpServletRequest request = (HttpServletRequest)ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
		    ///String name = request.getParameter("name");
		    BlogDao blog = new BlogDao();
		    Map<String,Integer> title = blog.getTitleName(name);
		    //request.setAttribute("result", title);
		    setResult(title);
		    return "result";
		}
		catch(Exception e)
		{
		    return "error!";	
		}
	}
}

步骤四:在src目录下,新建一个叫struts.xml的文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--  指定Struts 2配置文件的DTD信息  -->
<!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
   <package name="struts" extends="struts-default">
   <action name="query" class="com.zrp.action.QueryAction" method="excute">
       <result name="result">/result.jsp</result>
       <result name ="error">/error.jsp</result>
   </action>
 </package>
</struts>
 
 步骤五:新建一个queryTitleName.jsp页面
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 <head>
   <title>查询博客主题</title>
 </head>
 <body>
   <s:form action="query">
     <s:textfield lable="书名" name="name"></s:textfield>
     <s:submit value="查询"></s:submit>
   </s:form> 
  </body>
</html>
步骤六:新建一个result.jsp页面,用来显示查询结构的跳转
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<table>
  <tr>
    <td>标题</td>
    <td>得分</td>
  </tr>
  <s:iterator value="result">  <!-- iterator相当于一个for循环,把result里的值找出来 -->
      <tr>
    <td><s:property value="key"/></td>
    <td><s:property value="value"/></td>
    </tr>
  </s:iterator> 
</table>
OK,这样就完成了一个简单的struts2的配置,注意的几点
1.queryTitleName.jsp中的<s:form action="query"> ...</s:form>中的action的名称“query”要与struts.xml中<action name="query" 
class="com.zrp.action.QueryAction" method="excute"> 的action的name名"query"对应。
2.struts.xml中<action name="query" class="com.zrp.action.QueryAction" method="excute"> 中的class和method要写正确,否则
会报错。
3.导入包主要有以下6个,一开始以为不用到commons-fileupload-1.2.1.jar这个包,但遇到
了找不到类的错误。