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 页面,配置成功。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 28
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 28
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值