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]





版权声明:本文博客原创文章。博客,未经同意,不得转载。

posted @ 2015-08-20 08:21  lcchuguo  阅读(264)  评论(0编辑  收藏  举报