• Struts2 入门笔记


    参考网页:http://www.yiibai.com/struts2/struts2_examples.html 该页内容大部分机翻,坑爹。。。

    1.环境安装、配置

      1 - 安装JDK

    过程不再赘述,注意设置好环境变量。

      2 - 安装Apache Tomcat

    下载地址:http://tomcat.apache.org/

    tomcat是绿色版的,解压到合适的目录即可。

      3 - 安装Eclipse(或MyEclipse)

    你可以选择免费的Eclipse或者收费的MyEclipse。我选择了Eclipse,故接下来本文将基于Eclipse。

    Eclipse也是绿色版,解压到合适的目录即可。笔者下载的是最新4.4版本(luna)。

      4 - 下载strust2

    下载地址:http://struts.apache.org/download.cgi

    下载完毕后解压到合适位置待用。

    2.第一个sturts2 Web应用程序

      1 - 创建一个动态Web项目

    在Eclipse中,点击 File > New > Dynamic Web Project ,输入项目名称为:HelloWorldStruts2

    最后勾选 Generate Web.xml deployment descriptor ,完成项目创建。

    接下来需要从struts的lib目录下复制以下库文件到项目文件夹下的 WEB-INFlib 

    此外,还需将strutslib中的所有jar文件添加到用户库(User  libraries)中。

    eclipse菜单 Window->Preferences->Java->Build Path->User Libraries ,点击 New… 按钮,输入用户库名后点击OK。

    点击 Add JARS… 按钮,添加所需的库文件(struts的lib目录)。

       2 - 创建动作类(Action Class)

    在 Java Resources > src 下新建一个名为 com.hello.action 的包(可通过在src上右键选择生成),并在该包下新建一个名为 HelloWorldAction.java 的java文件。

    文件内容如下:

     1 package com.hello.action;
     2 
     3 public class HelloWorldAction {
     4     private String name;
     5 
     6        public String execute() throws Exception {
     7           return "success";
     8        }
     9        
    10        public String getName() {
    11           return name;
    12        }
    13 
    14        public void setName(String name) {
    15           this.name = name;
    16        }
    17 }

    这是一个非常简单的类,一个名为“name”的私有成员变量。标准的“name”属性的get和set公有成员函数,返回字符串“success”的执行方法。

      3 - 创建视图(View)

    接下来我们创建一个最后显示的页面文件。在Eclipse中,右键点击WebContent文件夹,选择 New >JSP File 创建一个名为 HelloWorld.jsp 的文件

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib prefix="s" uri="/struts-tags"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here - Hello World</title>
     9 </head>
    10 <body>
    11     Hello World, <s:property value="name"/>
    12 </body>
    13 </html>

      4 - 创建一个主页面

    同样的,在WebContent目录下创建一个 index.jsp 文件

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <%@ taglib prefix="s" uri="/struts-tags"%>
     4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
     5 <html>
     6 <head>
     7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     8 <title>Insert title here - Hello World - index</title>
     9 </head>
    10 <body>
    11 <h1>Hello World From Struts2</h1>
    12    <form method="post" action="hellobyform">
    13       <label for="name">Please enter your name</label><br/>
    14       <input type="text" name="name"/>
    15       <input type="submit" value="Say Hello"/>
    16    </form>
    17 </body>
    18 </html>

      5 - 修改配置文件

    在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 <struts>
     6 <constant name="struts.devMode" value="true" />
     7    <package name="default" namespace="/" extends="struts-default">
     8      
     9       <action name="hellobyform" 
    10             class="com.hello.action.HelloWorldAction" 
    11             method="execute">
    12             <result name="success">/HelloWorld.jsp</result>
    13       </action>
    14    </package>
    15 </struts>

    编辑 WEB-INF 目录下的 web.xml 文件:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
     3   <display-name>HelloWorldStruts2</display-name>
     4   <welcome-file-list>
     5 
     6     <welcome-file>index.jsp</welcome-file>
     7 
     8   </welcome-file-list>
     9   
    10     <filter>
    11        <filter-name>struts2</filter-name>
    12        <filter-class>
    13            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
    14        </filter-class>
    15     </filter>
    16 
    17     <filter-mapping>
    18        <filter-name>struts2</filter-name>
    19        <url-pattern>/*</url-pattern>
    20     </filter-mapping>
    21     
    22 </web-app>

    至此,第一个sturts2 Web应用程序已经ready!可以通过打包成war文件并部署到tomcat中进行测试了!

    附:Eclipse中配置tomcat Servers

    注意这里,默认为wtpwebapps,需要修改为webapps。

    配置好后就可以右键项目run了,运行结果会直接在eclipse中显示。

  • 相关阅读:
    jmeter4-数据库性能测试
    jmeter2-接口性能测试
    jmeter1-测试流程
    jmeter-beanshell随机取数组一项
    最强MySQL数据库设计规范... (转载)
    Python词云
    adb常用命令
    jmeter线程组多个请求之间的参数关联
    VisualVM使用与调优案例
    mysql调优工具tuning-primer.sh的使用
  • 原文地址:https://www.cnblogs.com/alexsun/p/3818414.html
Copyright © 2020-2023  润新知