开发Struts2应用程序-基本步骤

1. 创建一个web项目,项目取名为struts2_first_project

如图:


2. 添加Struts2的类库和依赖库,使项目支持Struts2的特性(装入最小类库)

最小类库图:


3. 配置web.xml文件,让项目可以使用Struts2

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.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>

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter这个过滤类在struts2-core文件中可以找到


4. 配置struts.xml文件

首先,使用Eclipse或MyEclipse在项目的src目录下创建一个struts.xml文件。

    这里在src创建的struts.xml并不会被项目真正的使用,在src目录下创建的文件会被Eclipse或MyEclipse工具自动的复制一份到WebRoot/Web-INF/classes目录下,该目录下的文件才会被项目真正的使用,之所以在src目录下创建struts.xml文件,是能够使用IDE给我们带来的好处。

其次,编写struts.xml文件

    struts.xml文件是通过一个DTD进行验证的,所以配置文件头应该含有DTD的声明,若不知道struts2的配置文件的DTD,可以从下载的Struts2文件包中,找到apps文件夹,里面含有了struts2给我们的可以直接配置到tomcat服务器上运行的示例。里面含有我们的需要的DTD信息(使用WinRAR之类的程序打开)。

<?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="struts2" extends="struts-default">
    	<action name="login" class="com.suxiaolei.action.LoginAction">
    	    <result name="input">login.jsp</result>
    		<result name="success">result.jsp</result>
    	</action>
    </package>
</struts>

编写struts.xml的规则包含在 http://struts.apache.org/dtds/struts-2.0.dtd这个文件中,可以下载这个文件阅读其中的规则。


5. 编写com.suxiaolei.action.LoginAction类,这个类用于处理action的name=“login”类型的请求。

package com.suxiaolei.action;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport
{
	private static final long serialVersionUID = 2666229676444930208L;
	private String username;
	private String password;

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public String execute()
	{
		return "success";
	}

}

6. 编写jsp登陆页面(login.jsp):
<form action="login">
	username:
	<input type="text" name="username" />
	<br>
	password:
	<input type="password" name="password" />
	<br>
	<input type="submit" value="submit" />
</form>

7. 编写jsp结果页面(result.jsp):
 username:${requestScope.username }<br>
 password:${requestScope.password }<br>


8. 测试程序

启动服务器,打开浏览器,输入http://localhost:6666/Struts2/login.jsp(端口号一般为8080,这里被Oracle所占所以换了一个)。

若程序成功运行将会看到登陆界面。

点击submit按钮 将会显示你所输入的username和password








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值