Struts2笔记:HelloWorld入门
Struts2笔记:HelloWorld入门
一、开发环境说明
① struts2-2.2.1-all.zip ②JDK 1.6 ③Tomcat 6.0 ④MySql 5.1 ⑤Eclipse 3.5以上版本
二、工程搭建
1、 创建web工程 Struts2Demo,在WebRoot下导入开发要用到的基本包commons-fileupload-1.2.1.jar、commons-io- 1.3.2.jar、freemarker-2.3.16.jar、javassist-3.7.ga.jar、ognl-3.0.jar、 struts2-core-2.2.1.jar、xwork-core-2.2.1.jar。这几个包我们可以从官方的例子当中获取的。
2、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<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>
<welcome-file-list> <welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
3、代码编写
①JSP页面index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
<s:property value="message"/>
</body>
</html>
②创建Action类
package com.hanfeng.action;
import java.text.DateFormat;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* @author hanfeng
*
*/
public class HelloAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String execute() throws Exception{
message="欢迎来到Strus2的世界,现在时间是:"+DateFormat.getInstance().format(new Date());
return "success";
}
}
③
配置struts2文件<?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>
<package name="com.hanfeng" namespace="/"
extends="struts-default"> <action name="Hello"
class="com.hanfeng.action.HelloAction"> <result
name="success">/index.jsp</result> </action>
</package>
<!-- Add packages here -->
</struts>④发布
在IE或是火狐中输入http://localhost:8080/Struts2Demo/Hello.action就可以啦!
⑤代码工作解释
首 先,浏览器请求action,发送到web应用服务器,容器接受到web服务器对资源Hello.action的请求,根据web.xml的配置,服务器 包含有.action后缀的请求转到 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter类进行 处理。调用StrutsPrepareAndExecuteFilter进入struts2的流程当中去。
接着,框架在 struts.xml配置文件中查找名位Hello.action的对应的类。框架初始化该Action,并且执行Action类的execute方法。 execute方法将信息放入message变量当中,并且返回成功。框架检查配置后查看当返回成功时对应的页面,框架告诉容器来获得请求返回的结果页面 index.jsp。
<s:property value="message"/>标签调用Action类中的getMessage方法来获得message的值。最后标签显示出值,并且以HTML的形式显示在页面上。
要点,记住啦!
Struts2框架利用Action来处理HTML的表单及其他请求。Action返回一个结果的名字是字符串,如SUCCESS、ERROR等,从struts.xml从获取映射信息。一个给定的结果字符串将选择一个页面或其他资源来返回给用户。
<?xml
version="1.0" encoding="UTF-8"?><web-app id="WebApp_9"
version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Struts Blank</display-name>
<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>
<welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
</web-app>

浙公网安备 33010602011771号