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

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:端口号/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和返回结果集

时间: 2024-08-07 05:42:29

struts开发<在eclipse中配置struts. 一>的相关文章

struts开发&amp;lt;struts中的action详细配置. 二&amp;gt;

在eclipse中新建项目StrutsDemo1[struts的配置见]struts开发<在eclipse中配置struts. 一> 详细文件夹结构例如以下 第一种配置方法 新建UserAction package fzl.user.struts.demo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { public String list(){ Sys

struts开发&lt;struts中的action具体配置. 二&gt;

在eclipse中新建项目StrutsDemo1[struts的配置见]struts开发<在eclipse中配置struts. 一> 具体目录结构如下 第一种配置方法 新建UserAction package fzl.user.struts.demo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { public String list(){ System

struts开发&lt;struts中的参数传递.三&gt;

不说废话,直接上干货 1.通过set和get传递参数 增加username 和password两个属性并增加set和get方法 <span style="font-size:18px;">package fzl.user.struts.demo; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends ActionSupport { <span style="

struts.xml文件中action配置、OGNL的投影映射、OGNL表达式的符号

在struts.xml文件中不同的action配置,请求的路径是不一样的 1.请求 path = user!query.action; 配置如下: <action name="user" class="com.bwf.code.action.UserAction"> <result name="queryUser">/query.jsp</result> </action> 2.请求path = u

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

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文件,假设所有导入有可能会发

配置struts时web.xml中&lt;url-pattern&gt;*.action&lt;/url-pattern&gt;

<filter>    <filter-name>struts2</filter-name>    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping>    <filter-name>struts2</fil

2、Struts中的action

Struts2中的Action是一个纯java对象 默认的action会执行execute方法 2.1访问不同的方法 1.可以为action设置多个method,之后在struts.xml文件中配置这些action所对应的方法 2.只是写一个action,通过一些特殊的方法来进行访问 以上操作问题是:需要为不同的方法设定不同的返回值.这个名称不好统一 3.使用通配符的方式 在开发中一般都使用通配符的方式(这样可以大大减少action的配置) 2.2.一些常量的配置 可以通过<constant>

SSH框架中struts开发环境搭建

Myeclipse中搭建struts开发环境主要分为4个步骤: 一.找到开发struts应用所需要用的jar包 1.到网站http://struts.apache.org/download.cgi#struts2014下载struts的源码,此处笔者下载的为2.3.16.3版 2.解压缩下载的struts压缩包,找到需要添加到项目中的最核心的jar包,不同的struts所需要的最少jar包是不一样的,这里可以到doc文件中查找,create-struts-2-web-application-wi

struts+spring action应配置为scope=&quot;prototype&quot;

truts+spring action应配置为scope="prototype" <bean id="personAction" scope="prototype" class="quickstart.action.PersonAction"> <constructor-arg ref="personService" /></bean> 在配置文件中,bean默认是单例模