zoukankan      html  css  js  c++  java
  • Struts2 开发环境搭建

    一.开发环境

           eclipse+tomcat+struts-2.2.3

           eclipse下载地址:http://www.eclipse.org/downloads/
           tomcat下载地址:http://tomcat.apache.org/download-70.cgi
           struts下载地址:http://struts.apache.org/download.cgi#struts23162

    二.新建web项目,并向web项目中加入依赖jar包



    三.配置web.xm,向web.xml配置Struts2过滤器


    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
         <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>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

            FilterDispather作为Struts2框架的核心控制器,负责拦截用户的全部请求,当用户请求到达时,该过滤器会过滤用户请求,这样将不同的请求以业务类型划分,并将请求继续传给不同的业务控制器Action,Action调用不同的业务模型对请求进行处理,并将处理结果返回给视图。

    四.向web项目中新建Action业务控制器


    package Action;
     
    import com.opensymphony.xwork2.ActionSupport;
     
    public class indexAction extends ActionSupport {
        public String index()
        {
            return SUCCESS;
        }
    }

            Action是MVC中 C 的一部分,用来调用某个java类的某个方法进行数据预处理和一些相关的业务逻辑处理,然后把终于处理结果返回,结果这个能够是一个页面,也可带或者不带參数訪问别的服务或者链接.或者什么都不做.

    五.配置Struts.xml,配置Action业务控制器到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>
    <constant name="struts.devMode" value="true" />
         <package name="default" namespace="/" extends="struts-default">
            <action name="index" method="index" class="Action.indexAction">
                <result >
                    /hello.jsp
                </result>
            </action>
     
        </package>
        <!-- Add packages here -->
    </struts>

           为什么要配置struts.xml?由于Struts2除了是一个MVC框架之外,还是 一个小巧的轻量级容器,负责管理核心控制器FilterDispather和业务控制器Action.能够说是struts容器将核心控制器拦截的用户请求转发给业务控制器。而不是核心控制器直接将请求转发给业务控制器,这样做解除了核心控制器FilterDispather与业务控制器Action之间的耦合,核心控制器FilterDispather仅仅负责拦截用户请求、业务控制器仅仅负责处理业务请求,核心控制器与业务控制器通过struts容器通信。而它们两者全然不知都彼此的存在。

    六.加入jsp视图


    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    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 'index.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="index" method="get">
       hello word!
        </form>
      </body>
    </html>

    七.部署项目到Tomcat中。打开浏览器,输入http://localhost:8080/Project1/index。出现hello word 页面。配置成功。

  • 相关阅读:
    C# WPF 窗体传递消息
    WPF ProgressBar 样式
    WPF的TextBox以及PasswordBox显示水印文字
    Win8.1 Hyper-V 共享本机IP上网
    ASP.NET MVC入门-Startup 类:注册服务和使用中间件
    ASP.NET MVC入门-Program类:程序的入口
    Unity3d AssetBundle 资源加载与管理
    C#考核知识点总结
    数据结构与算法之美-字符串匹配(上)
    LeetCode-探索链表-综合问题
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5078503.html
Copyright © 2011-2022 走看看