`
jsczxy2
  • 浏览: 1257812 次
  • 性别: Icon_minigender_1
  • 来自: 常州
文章分类
社区版块
存档分类
最新评论

struts2初学之hello,struts2

阅读更多

本篇应用了最基本的struts跳转程序来解释最基本的struts2的配置代码,具体看代码吧,没啥难点:

首先去下载struts2的包,刚才也说到官网地址下载很慢,官网的另外一个链接则很快,进入这个地址下吧,保证您能满速下载:http://archive.apache.org/dist/

 

1.下载完成后首先建立一个web工程名字叫TestStruts,lib中导入struts必须的包:freemarker-x.jar,ognl-x.jar,struts2-core-x.jar,xwork-x.jar,commons-logging-x.jar,commons-fileupload-x.jar

 

2.配置web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
	
    <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>*.action</url-pattern>
    </filter-mapping>
    
    
</web-app>

 这里可以看到struts2的后缀是action而不再是do了。。嘛。。对于urlrewrite来说这些都无所谓了。。。

 

3.写action类,继承ActionSupport类,嘛。。。这个比struts1要好很多了,属性啥的都很独立,遭了,那我要用的request和response哪去了呢?好吧,去Context里找吧。。。还有session和上下文呢,OK,都封装一下到一个基类里,名字就叫BaseAction吧!

BaseAction.java:

package com.xuyi.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport{
	
	/**
	 * 获得request
	 * 
	 * @return
	 */
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	/**
	 * 获得response
	 * 
	 * @return
	 */
	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	/**
	 * 获得session
	 * 
	 * @return
	 */
	public HttpSession getSession() {
		return getRequest().getSession();
	}

	/**
	 * 获得servlet上下文
	 * 
	 * @return
	 */
	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}
	
	
}

 

然后使用我们需要的action继承这个BaseAction类即可:

HelloStruts2.java:

package com.xuyi.action;

public class HelloStruts2 extends BaseAction {
	
	public String hello() {
		return SUCCESS;
	}
	
}

 4.在src下建立一个struts.xml来对struts的action进行配置:

struts.xml:

<!DOCTYPE struts PUBLIC 
	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
	"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
	<!--package: name可以随便指定  namespace指定页面的目录(/为根目录) extends指定为 struts-default.xml文件(此文件包含默认的设置和定义) -->
	<package name="struts" namespace="/" extends="struts-default">
		<!--action:name是访问action的名称 class是action的类 method是访问的action里的方法等同于struts1的method result是返回url-->
		<action name="helloStruts2" class="com.xuyi.action.HelloStruts2" method="hello">
		  <result name="success">/pages/hello_struts2.jsp</result>
		</action>
	</package>
</struts>

 

5.在WebRoot下建立一个pages文件夹并在里面建立hello_struts2.jsp:

<%@ 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>
<title>Hello</title>
</head>
<body>
	hello,strut2!
</body>
</html>
 

 

6.启动tomact后访问地址:http://localhost:8080/TestStruts/helloStruts2.action

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics