struts开发的步骤

说来惭愧,这一个简单的struts折腾了好久,差不多花了三天多的时间才解决。下面我总结一下开发的步骤:(本人用的是MyEclipse);

1.新建一个Exercise3的web Project项目

2.配置web.xml文件
添加如下代码:

<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>
3.加入类库

将如下图的jar包放在WEB-INF/lib下



4.新建一个类

代码如下:

package cn.wwh.www.struts;

/**
 *类的作用:
 *
 *@author 一叶扁舟
 *@version 1.0
 *@创建时间: 2014-8-11   下午08:44:26
 */
public class FirstAction {

	public String <span style="color:#ff0000;">firtstMethod</span>(){
		System.out.println("hello word ,this my first Struts!");
		return "hello";
	}
}

5.在WebRoot下新建一个views/hello/welcome.jsp文件

welcome.jsp的代码如下:

<pre name="code" class="html"><%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'welcome.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    Hello  Word ! welcome to Struts !This is my first Struts.
  </body>
</html>

 

6.在src下建一个struts.xml文件

代码如下:

<?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="/<span style="color:#3333ff;">test</span>" extends="struts-default">
		<action name="<span style="color:#3333ff;">hello</span>" class="cn.wwh.www.struts.FirstAction"
			method="<span style="color:#ff0000;">firtstMethod</span>">
			<result name="hello" type="redirect">
				<param name="location">/views/hello/welcome.jsp</param>
			</result>
		</action>
		
	</package>
</struts>
7.在tomcat中部署一下

在浏览器中输入:http://localhost:8080/Exercise3/test/hello.action

由于在配置文件中采用的是redirect(重定向)的方式,所以地址栏会发生改变

结果图:



特别要注意(一直出错的原因):

        1.jar包不能重复导入,否则出现jar冲突,本人由于有各种版本的jar包,所以在启动tomcat的时候发生了该异常。

        2.在浏览器中输入的访问地址出现了错误,这个地址组合是与struts.xml文件中namespace和action name 有关

一般访问方式:

http://ip:port/contextPath/namespace/actionName[.action]





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值