用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字

feng    -  云代码空间

——

struts2第一讲 helloworld

2013-03-18|2271阅||

摘要:首先要安装jdk1.7以及tomcat7和myeclipse 对于这些配置的安装 这里不再细细说明 因为网上好些地方都有的 给个链接吧 http://blog.sina.com.cn/s/blog_5116f6310100b889.html 其次是下载struts2 第一步:去

首先要安装jdk1.7以及tomcat7和myeclipse 对于这些配置的安装 这里不再细细说明 因为网上好些地方都有的 给个链接吧 http://blog.sina.com.cn/s/blog_5116f6310100b889.html

其次是下载struts2
第一步:去struts21的官网 http://struts.apache.org/release/2.1.x/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
然后粘贴到WebRoot/WEB-INF/lib即可;

第四步:
WebRoot目录下新建一个login.jsp
代码如下
login.jsp
Jsp代码 复制代码 收藏代码struts2第一讲 helloworld
  1. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>   
  2. <%   
  3. String path = request.getContextPath();   
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  5. %>   
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  8. <html>   
  9.   <head>   
  10.     <base href="<%=basePath%>">   
  11.        
  12.     <title>My JSP 'login.jsp' starting page</title>   
  13.        
  14.     <meta http-equiv="pragma" content="no-cache">   
  15.     <meta http-equiv="cache-control" content="no-cache">   
  16.     <meta http-equiv="expires" content="0">       
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  18.     <meta http-equiv="description" content="This is my page">   
  19.     <!--   
  20.     <link rel="stylesheet" type="text/css" href="styles.css">   
  21.     -->   
  22.   
  23.   </head>   
  24.      
  25.   <body>   
  26.     <form action="login.action" method="post">   
  27.         username: <input name="username" type="text"><br>   
  28.         password: <input name="password" type="password"><br>   
  29.            
  30.         <input type="submit" value="submit">   
  31.     </form>   
  32.   </body>   
  33. </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 '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代码 复制代码 收藏代码struts2第一讲 helloworld
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"    
  3.     xmlns="http://java.sun.com/xml/ns/j2ee"    
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
  6.     http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.   
  8.     <filter>  
  9.         <filter-name>struts2</filter-name>  
  10.         <!-- 控制器 -->  
  11.         <filter-class>  
  12.         org.apache.struts2.dispatcher.FilterDispatcher   
  13.         </filter-class>  
  14.     </filter>                                                                  
  15.                 
  16.                 
  17.              <filter-mapping>  
  18.                 <filter-name>struts2</filter-name>  
  19.                 <!-- 任何请求均有过滤器 -->  
  20.                 <url-pattern>/*</url-pattern>  
  21.              </filter-mapping>  
  22. </web-app>  
<?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
Java代码 复制代码 收藏代码struts2第一讲 helloworld
  1. package com.test.action;   
  2.   
  3. public class LoginAction    
  4. {   
  5.        
  6.   
  7.                         //getter和setter方法   就是根据这里的方法名来匹配客户端的信息   
  8.     public String getUsername() {   
  9.         return username;   
  10.     }   
  11.     public void setUsername(String username) {   
  12.         this.username = username;   
  13.     }   
  14.     public String getPassword() {   
  15.         return password;   
  16.     }   
  17.     public void setPassword(String password) {   
  18.         this.password = password;   
  19.     }   
  20.        
  21.                public String execute() throws Exception   
  22.                {   
  23.                    return "success";   
  24.                }   
  25.        
  26.     //对应表单上的   
  27.     private String username;   
  28.     private String password;   
  29. }  
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代码 复制代码 收藏代码struts2第一讲 helloworld
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <!DOCTYPE struts PUBLIC   
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">  
  5.                
  6. <struts>  
  7.                       
  8.                    <package name="struts2" extends="struts-default">  
  9.                             <action name="login" class="com.test.action.LoginAction">  
  10.                                         <!-- result没有名字是默认的success -->  
  11.                                     <result name="success">/result.jsp</result>  
  12.                             </action>  
  13.                     </package>  
  14. </struts>  
<?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文件 代码如下:
Jsp代码 复制代码 收藏代码struts2第一讲 helloworld
  1. ]   
  2. <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>   
  3. <%   
  4. String path = request.getContextPath();   
  5. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";   
  6. %>   
  7.   
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">   
  9. <html>   
  10.   <head>   
  11.     <base href="<%=basePath%>">   
  12.        
  13.     <title>My JSP 'result.jsp' starting page</title>   
  14.        
  15.     <meta http-equiv="pragma" content="no-cache">   
  16.     <meta http-equiv="cache-control" content="no-cache">   
  17.     <meta http-equiv="expires" content="0">       
  18.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">   
  19.     <meta http-equiv="description" content="This is my page">   
  20.     <!--   
  21.     <link rel="stylesheet" type="text/css" href="styles.css">   
  22.     -->   
  23.   
  24.   </head>   
  25.      
  26.   <body>   
  27. helloworld   
  28.         username: ${requestScope.username }<br>   
  29.         password: ${requestScope.password }   
  30.   </body>   
  31. </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 '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 即可

顶 13踩 12收藏
文章评论
    发表评论

    个人资料

    • 昵称: feng
    • 等级: 资深程序员
    • 积分: 1584
    • 代码: 8 个
    • 文章: 42 篇
    • 随想: 2 条
    • 访问: 84 次
    • 关注

    站长推荐