Struts 2学习及问题记录(1)——Struts 2的配置

  最近一段时间一直在学习轻量级Java EE。之前已经对JSP和Servlet进行了学习,初步了解了表现层技术。现在开始对MVC框架进行学习。而Struts 2框架是MVC框架中应用最为广泛的一种,所以我就从Stuts 2框架入手,逐步了解MVC框架的各方面知识。

  从这篇文章开始,我将记录学习和使用Struts过程中遇到的问题。如果找到了解决办法,我会在文章中做好记录;如果没有找到解决办法,希望阅读到文章的大神们能够给予我一些指点。

  下面,开始对遇到的问题进行记录。


------------------------------------------------------

  Struts 2的配置流程

  说来惭愧,刚开始起步就遇到了困难。在各种教程中,我总结了一下Struts 2的配置方法。

  首先,需要将Struts 2的相关函数库导入项目中。在各种教程中,基本都是建议将示例项目压缩包struts-blank.war中WEB-INF/lib路径下的jar文件复制到项目的相应位置。其次,在web.xml文件中,配置核心Filter,代码段如下:

<?xml  version="1.0" encoding="GBK" ?>
<web-app ..........  >
      <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 2核心Filter会拦截所有的用户请求。

  接着需要配置src目录下的struts.xml文件,代码段如下:

<?xml ...... ?>
<!DOCTYPE ........... >
<struts>
      <constant .......  />
      <package name="..." extends="struts-default" >
            <action name="*" >
                  <result>/WEB-INF/content/{1}.jsp</result>
            </action>
      </package>
</struts>
  其中的action元素的name属性为" * "表示该会处理所有的用户请求。

  以上就是Struts 2最基本的配置流程。

------------------------------------------------

  在实际运用中,我遇到了一些问题,应该是我还没有完全理解配置的内涵。首先,这里给出能够在Eclipse中正常Run的项目的结构图。

  

  项目里面,所有编译好的class文件,以及从/src路径下复制出来的struts.xml文件,都保存在了/build路径下。而在/WebContent路径下,不存在.class文件和struts.xml文件。

  然而,我在试着绕过Eclipse直接“纯手动”创建项目时,能够正常运行的项目结构应该是如下图所示。

  对比二者可以看出,项目中的.class文件和struts.xml文件的位置不同。存在这一点不同的原因,我还不是很清楚,希望看到这篇文章的大神们能够给予我一些指点。

-----------------------2016年5月3日-----------------------------


  在使用Action时,我又遇到了一个很奇怪的问题。

  在同样的配置条件下,通过直接implements Action方法实现的Action会出现错误,而通过extends ActionSupport则能够成功编译运行,两种方法的java文件如下:

implements Action方式:

package dong;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Action;

public class LoginAction implements Action
{
	private String username;
	private String password;
	
	public String getUsername()
	{
		return this.username;
	}
	public String getPassword()
	{
		return this.password;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
	
	public String execute() throws Exception
	{
		ActionContext ctx = ActionContext.getContext();
		Integer counter = (Integer) ctx.getApplication().get("counter");
		if (counter == null)
		{
			counter = 1;
		}
		else
		{
			counter = counter + 1;
		}
		ctx.getApplication().put("counter", counter);
		ctx.getSession().put("user", getUsername());
		if (getUsername() == null || getPassword() == null)
		{
			ctx.put("tip", "服务器提示:登录失败");
			return ERROR;
		}
		if (getUsername().equals("dongzhong") && getPassword().equals("dongzhong1990")) 
		{
			ctx.put("tip", "服务器提示:登录成功");
			return SUCCESS;
		}
		
		ctx.put("tip", "服务器提示:登录失败");
		return ERROR;
	}
}

extends ActionSupport方式:

package dong;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
	private String username;
	private String password;
	
	public String getUsername()
	{
		return this.username;
	}
	public String getPassword()
	{
		return this.password;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
	
	public String execute() throws Exception
	{
		ActionContext ctx = ActionContext.getContext();
		Integer counter = (Integer) ctx.getApplication().get("counter");
		if (counter == null)
		{
			counter = 1;
		}
		else
		{
			counter = counter + 1;
		}
		ctx.getApplication().put("counter", counter);
		ctx.getSession().put("user", getUsername());
		if (getUsername() == null || getPassword() == null)
		{
			ctx.put("tip", "服务器提示:登录失败");
			return ERROR;
		}
		if (getUsername().equals("dongzhong") && getPassword().equals("dongzhong1990")) 
		{
			ctx.put("tip", "服务器提示:登录成功");
			return SUCCESS;
		}
		
		ctx.put("tip", "服务器提示:登录失败");
		return ERROR;
	}
}

  希望能够得到大家的指点,我也琢磨了好久,还是没有明白怎么回事。
发布了21 篇原创文章 · 获赞 63 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/dongzhong1990/article/details/51306502