您的位置:首页 > 编程语言 > Java开发

Struts2入门:struts2环境配置和一个小例子

2015-07-17 22:14 459 查看
Struts2是一个出色的MVC开源框架,它的名字Struts2其实并不表示它跟Struts1有任何联系,只是WebWork借Struts之名推行的框架而已。好,下面我们来看一个怎样配置Struts2,同时实现第一个Struts小例子。

一、Struts2的基本配置步骤:

1. 将Struts2必须包导入WEB-INF/lib目录下,不需要build path,注
意了,是不需要build path的,同时Struts2的必须jar包有5个,分别是:
- struts2-core-2.1.6.jar --------------- struts2的核心包   ,用到标签的时候需要用到
- freemarker-2.3.13.jar---------------FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具
- commons-logging.jar ----------- Jakarta的通用日志记录包
- ognl-2.6.11.jar -------------- 支持ognl表达式
- xwork-2.1.2.jar -------------- xwork的包 由于Struts2是由xwork的延伸 有些类依然关联着 xwork的类

2. 配置web.xml文件,这里相当于配置普通的Filter(因为Struts2是使用Filter作为控制器的),但是Filter的全类名有是规定的:org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
3. 在类路径下(也就是src路径下)创建一个struts.xml用来配置action


二、下面给出web.xml代码:

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>


三、经过上述三个步骤之后,开发环境已经搭建好了,下面我们来写一个使用MVC结构的HelloWorld:

第一步,编写一个类,让该类继承ActionSupport

package com.lhf;

import com.opensymphony.xwork2.ActionSupport;

public class HelloWorldAction extends ActionSupport {
// 要在网页上显示的内容
private String content;

// Struts2实例入口方法,相当于Servlet的serveice()方法,是用来提供服务的
public String execute(){
content = " Hello world!";
return SUCCESS;
}

// content's getter
public String getContent() {
return content;
}

// content's setter
public void setContent(String content) {
this.content = content;
}

}


然后我们再来写两个jsp文件

首先一个是输出helloworld的一个简单的jsp文件page.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!-- 导入struts2标签库 -->
<%@ 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>
<!-- 获取content属性值 -->
the content is:<s:property value="content"/>
</body>
</html>


其次是跳转jsp, page1.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>this is page1</title>
</head>
<body>
<a href='<s:url action="HelloWorldAction"/>'>请点击这里进行跳转</a>
</body>
</html>


-最后要配置struts.xml,配置action和对应的result,代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apac
a49f
he.org/dtds/struts-2.3.dtd">

<struts>

<package name="main" extends="struts-default">
<action name="HelloWorldAction" class="com.lhf.HelloWorldAction">
<result name="success">/page.jsp</result>
</action>
</package>

</struts>


好了,这个例子就这样完成了,其实对于初学者来说,struts2最难的大概是其各种配置,一堆名称满天飞…..
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息