码迷,mamicode.com
首页 > 系统相关 > 详细

struts开发<在eclipse中配置struts. 一>

时间:2017-05-17 16:08:22      阅读:277      评论:0      收藏:0      [点我收藏+]

标签:cgi   www   说明文   继承   div   tran   work   pac   ble   

1.获取struts的jar包

1.1首先在http://struts.apache.org/download.cgi#struts23163这里下载 struts的文件包(选择struts-2.3.16.3-all)

1.2解压得到例如以下的目录

技术分享


apps目录下是struts的一些官方样例

docs已久是官方api说明文档

lib包是struts全部的jar包

src则是一些样例的资源文件



注意:接下来我们须要取得我们须要的jar包,而不是lib文件夹下所有的jar文件,假设所有导入有可能会发生冲突

那么哪些才是我们须要的jar包呢?

1.3打开apps目录,解压struts2-blank.war得到演示样例的文件

技术分享

1.4打开WEB-INF/lib 里面的jar包就是我们基本struts操作须要的jar包。把他们取出来待用。

技术分享



2.在项目中取得struts的支持

2.1 打开eclipse 新建动态web

技术分享

2.2将第一步取得jar包拷贝到项目WEB-INF/lib文件夹下

技术分享

2.3在项目中加入web.xml并配置

在WEB-INF根文件夹下加入web.xml文件并配置struts的过滤器

 

<span style="font-size:18px;"><?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>
</span>


3.建立struts并实现

3.1在scr中新建action继承ActionSupport

<span style="font-size:18px;">package fzl.struts.demo;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	@Override
	public String execute() throws Exception {
		System.out.println("--------UserAction-------");
		return "success";
	}


		
	}

</span>

3.2在配置struts.xml文件

在src根文件夹下建立struts.xml文件并进行一下配置

<span style="font-size:18px;"><?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="default" namespace="/" extends="struts-default">
<action name="hello" class="fzl.struts.demo.UserAction">
<result>/hello.jsp</result>

</action>



    </package>
</struts>
</span>



4建立显示层文件

在WEB-INF目录下建立hello.jsp

<span style="font-size:18px;"><%@ 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>

<h2>hello struts</h2>
<h2>这是我的第一个struts程序</h2>
</body>
</html></span>

启动Tomcat、在地址栏输入http://localhost:port号/StrutsDemo/hello 就可以得到例如以下页面

技术分享

到这里我们的struts的配置已经完毕并实现了。

最后总结一下


基本步骤:

1、拷贝struts的jar到项目中(apps中的blank项目中能够找到这些jar包)
2、将struts2的过滤器加入到web.xml中
3、配置struts2的配置文件(在src文件夹中创建struts.xml文件)
4、创建action(action就是一个POJO类)
4.1、为action编写execute方法
4.2、在struts.xml文件里配置action和返回结果集


struts开发&lt;在eclipse中配置struts. 一&gt;

标签:cgi   www   说明文   继承   div   tran   work   pac   ble   

原文地址:http://www.cnblogs.com/yfceshi/p/6867852.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!