我的struts2学习之一
2016-05-31 00:59
344 查看
小弟在实训期间对struts2的一些复习回顾。如果有错,希望指正。
1.struts2简介
struts2.0在2007年2月份开始发行。 它由之前的struts1和webwork整合而成,是一个优雅的、可扩展的、可用来建立企业级Java Web应用程序的框架。 它与struts1没有太大的联系,大多数功能源于webwork。
2.struts2工作原理
首先,用户发起一个请求到Tomcat,这个请求经过一系列的过滤器(filter)。
然后,请求到达FilterDispatcher(2.1.3版本之后就是StrutsPrepareAndExecuteFilter),这个过滤器就会去查找到struts2的配置文件(即struts.xml)。
之后,struts2根据请求的url其中的namespace和action,查找struts.xml中的配置的Action类,进行方法的调用之前还会将请求经过一系列的拦截器处理。
最后,Action执行完毕,根据struts.xml配置的result标签返回结果。
3.第一个struts2项目
导入jar包
web.xml的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <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.xml的配置
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="User" extends="struts-default" namespace="/"> <action name="hello" class="action.UserAction" method="test"> <result>/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts>
这里配置的action就是处理’/hello.action’(namespace+actionName)这个访问路径的请求,处理请求的方法就是test()这个方法。
这里有一个需要注意的问题,就是这个xml文件的名字问题,默认必须是struts.xml。之前试过把它改成struts2.xml,发现会找不到配置的action。查看源码发现,这是因为struts2框架默认加载的三个配置文件是struts-default.xml,struts-plugin.xml,struts.xml,其中struts.xml就是我们要创建的那个,另外两个xml由框架自动带来。
页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!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>Insert title here</title> </head> <body> <a href="hello">测试</a> </body> </html>
这里的链接就是访问上面配置的hello.action
编写action
package action; import java.util.Random; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport{ public String test(){ int n = new Random().nextInt(100); System.out.println(n); if (n%2 == 0) { return SUCCESS; } return ERROR; } }
action里随机产生100以内的整数,如果是奇数就返回ERROR,否则就返回SUCCESS.
在浏览器输入:http://localhost:9005/struts2_01/hello.action成功跳转到相应的页面
这里我把tomcat的端口改成了9005,没改的话就是8080.
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- i-jetty环境搭配与编译
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 实现单Tomcat多Server配置
- 生产环境下的Tomcat配置
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- Linux部署Tomcat服务器
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器