Struts 2学习及问题记录(1)——Struts 2的配置
来源:互联网 发布:软件怎么控制license 编辑:程序博客网 时间:2024/05/17 01:21
最近一段时间一直在学习轻量级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;}}
希望能够得到大家的指点,我也琢磨了好久,还是没有明白怎么回事。
- Struts 2学习及问题记录(1)——Struts 2的配置
- Struts 2学习及问题记录(2)——标签库的使用
- Struts 2学习及问题记录(3)——类型转换及输入校验
- Struts 2学习及问题记录(4)——拦截器机制
- struts 2 的配置问题
- struts 学习问题记录
- Struts2学习2——Struts配置
- struts学习(1)——MVC模型及struts 2概述
- struts 2学习笔记—浅谈struts的线程安全
- Struts 2读书笔记-----struts 的action配置
- Struts(1+2)配置
- 初学STRUTS —— 从struts-blank的简单流转开始学习(2)
- Struts 2 学习(一) 环境配置
- Struts的配置问题
- struts学习---配置struts
- Struts配置及404问题
- Struts学习(—)
- 【ssh2学习记录】2、struts.xml和struts.properties的编写注意事项
- LintCode_508_Wiggle Sort
- Django报错:No translation files found for
- JTable用法
- NSNotificationCenter 使用方法简洁说明
- 打字练习
- Struts 2学习及问题记录(1)——Struts 2的配置
- Activity的ConfigChanges属性
- Android使用HttpURLConnection请求网络资源
- dialog横竖屏切换不适配
- Mac 修改文件夹权限
- 怪新鲜的
- Merge K Sorted Lists
- python numpy 快速处理数据
- Spring与Quartz的整合实现定时任务调度