zoukankan      html  css  js  c++  java
  • struts中的helloword(1)


    注:文章中的所有图片均在附件中明白表明  

    首先要安装jdk1.6以及tomcat6和myeclipse 对于这些配置的安装 这里不再细细说明 

    其次是下载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 

    Jsp代码  收藏代码
    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>  


    第五步:
     
    改动WEB-INF下的web.xml文件 
    代码例如以下 
    web.xml 
    Xml代码  收藏代码
    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>  


    第六步: 
    新建action 
    在src文件夹下新建包com.test.action 
    在包中新建一个action代码例如以下 
    LoginAction.java 
    Java代码  收藏代码
    1. package com.test.action;  
    2.   
    3. public class LoginAction   
    4. {  
    5.       
    6.   
    7.                         //getter和setter方法   就是依据这里的方法名来匹配client的信息  
    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. }  


    第七步: 
    struts配置文件 

    在src文件夹下新建一个struts.xml代码例如以下: 
    Xml代码  收藏代码
    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>  

    注意。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代码  收藏代码
    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>  


    至此。已经完毕了代码的书写工作。

    接下去是公布; 
    右键点击struts2这个项目的名称。在菜单中选择myeclipse,在选择add and remove project……就可以,之后将出现图三 
    选择project,点击add公布到指定的tomcat就可以、 

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

  • 相关阅读:
    Cookie、Session和自定义分页
    logstash 运行
    php json数据保留原样中文
    linux 32位还是64位
    php之isset 与 empty 区别
    php 订单
    个人分类
    laravel 创建自己的函数
    lumen框架导入数据异常
    yum安装samba服务器的安装
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5328645.html
Copyright © 2011-2022 走看看