原创作者: radovi   阅读:21984次   评论:12条   更新时间:2011-05-26    
注:文章中的全部图片均在附件中明确表明 
首先要安装jdk1.6以及tomcat6和myeclipse 对于这些配置的安装 这里不再细细说明 因为网上好些地方都有的 给个链接吧 http://blog.sina.com.cn/s/blog_5116f6310100b889.html

其次是下载struts2
第一步:去struts21的官网 http://struts.apache.org/2.1.6/index.html
点击下面图1中的的download now,下载图二中的全出即可。 下载后解压待用;

第二步
打开myeclipse tomcat的集成较简单,不多讲;新建一个web project 取名为struts2;
第三步;
在刚刚下载的struts2-1-6目录下的lib中复制出如下六个文件
  commons-logging-1.0.4.jar
  freemarker-2.3.8.jar 
  ognl-2.6.11.jar 
  struts2-core-2.0.6.jar
  xwork-2.0.1.jar
以及(因为是struts2-1-6版本的。所以一下这个文件也必不可少)
commons-fileupload-1.2.1


然后粘贴到WebRoot/WEB-INF/lib即可;
第四步:
WebRoot目录下新建一个login.jsp
代码如下
login.jsp
<%@ 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 'login.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>
    <form action="login.action" method="post">
    	username: <input name="username" type="text"><br>
    	password: <input name="password" type="password"><br>
    	
    	<input type="submit" value="submit">
    </form>
  </body>
</html>


第五步:

修改WEB-INF下的web.xml文件
代码如下
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">

    <filter>
    	<filter-name>struts2</filter-name>
    	<!-- 控制器 -->
    	<filter-class>
    	org.apache.struts2.dispatcher.FilterDispatcher
    	</filter-class>
    </filter>                                                               
             
             
             <filter-mapping>
             	<filter-name>struts2</filter-name>
             	<!-- 任何请求均有过滤器 -->
             	<url-pattern>/*</url-pattern>
             </filter-mapping>
</web-app>



第六步:
新建action
在src目录下新建包com.test.action
在包中新建一个action代码如下
LoginAction.java
package com.test.action;

public class LoginAction 
{
	

	                    //getter和setter方法   就是根据这里的方法名来匹配客户端的信息
	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() throws Exception
	           {
	        	   return "success";
	           }
	
	//对应表单上的
	private String username;
	private String password;
}



第七步:
struts配置文件

在src目录下新建一个struts.xml代码如下:
<?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.test.action.LoginAction">
                   						<!-- result没有名字是默认的success -->
                   					<result name="success">/result.jsp</result>
                   			</action>
                    </package>
</struts>

注意,struts.xml中有句话是
!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

红色这句可能会报错,解决的方法是 将“http://”字样去掉 其他我不知道还有什么方法,有高手知道请指点一二;

第八步;

新建result文件
在WebRoot目录下新建一个result.jsp文件 代码如下:
]
<%@ 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 'result.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>
helloworld
		username: ${requestScope.username }<br>
		password: ${requestScope.password }
  </body>
</html>



至此,已经完成了代码的书写工作。接下去是发布;
右键点击struts2这个项目的名称,在菜单中选择myeclipse,在选择add and remove project……即可,之后将出现图三
选择project,点击add发布到指定的tomcat即可、

最后,打开浏览器。在浏览器 http://localhost:8080/struts2/login.jsp 即可


附件中有图片和两个源代码;
  • 大小: 54.4 KB
  • 大小: 25.6 KB
  • 大小: 21.3 KB
评论 共 12 条 请登录后发表评论
12 楼 ziyitony 2016-03-22 22:12
非常感谢楼主无私分享。现在是20160320,教程已经有点点不同了,但是楼主的指导非常清晰,非常感谢。
11 楼 orange053 2014-02-19 17:19
为什么是404找不到资源?
10 楼 xiaowei24518 2013-10-06 19:25
为什么按照步骤做完后,运行说找不到资源了 ,不懂 !
9 楼 lujiancs 2013-01-24 12:37
LoginAciton 需要继承AcitonSupport 才能运行
8 楼 lysvanilla 2010-12-23 15:52
帖子内容不错,通俗易懂,入门很合适。当然这个和2.2的话就略有不同了。
7 楼 seachen 2010-07-10 07:34
非常感谢楼主
6 楼 Linkey870821 2010-07-09 20:37
谢谢,对我这个菜鸟来说够用了!
5 楼 peenyking 2009-03-29 10:37
顶你一下
谢谢。。。。。
4 楼 qz小峰 2009-03-17 10:41
开始学习struts2,感触很大啊
3 楼 523276508 2009-03-04 15:31
写的非常不错!
2 楼 radovi 2009-02-24 21:22
sharp_lover 写道

写的挺详细的。谢谢~~~

we just learn together
1 楼 sharp_lover 2009-02-24 19:26
写的挺详细的。谢谢~~~

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • radovi在2009-02-21创建
  • radovi在2011-05-26更新
  • 标签: 一天天struts2中……
Global site tag (gtag.js) - Google Analytics