初学struts2框架(一)

利用struts2框架创建HelloWorld程序

工具:MyEclipse、struts2.3.34

步骤一:打开MyEclipse创建Web Project;

步骤二:打开struts源文件->apps->struts2-blank->classes;在此目录下找到struts.xml配置文件,将其复制

到我们所创项目的src文件中,注释掉根标签下的原内容,并做如下配置:

namespace决定了action的访问路径,默认为空。可以接收所有路径的action。

namespace可以写成/,或者/xx,或者/xxx/yyy,对应的action访问路径就为/index.action,/xxx/yyy/index.action.

namespace最好用模块来命名。

注:表示开启开发模式,action标签名称发生变化,程序会立即响应。

<constant name="struts.devMode" value="true" />
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
	<!--
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">

        <default-action-ref name="index" />

        <global-results>
            <result name="error">/WEB-INF/jsp/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>

    <include file="example.xml"/>
-->
    <!-- Add packages here -->
    <!--
    	package:包 struts2使用package来组织模块。
    	name 属性:必须,用于其他的包应用当前包。
    	extend:当前包继承那个包,继承,既可以继承里面所有的配置,通常情况下继承Strut-deflut 
     -->
     <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
    	<!-- 一个struct2请求就是一个action -->
    	<action name="hell">
    		<result>
    			/Hello.jsp
    		</result>
    	</action>
    </package>
</struts>

步骤三:打开struts源文件->apps->struts2-blank,找到web.xml文件,将filter元素内容复制到,我们所创建项目web.xml文件中,配置内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <display-name>MyStruts2Project010</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!--配置Filter-->
  <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>
</web-app>

步骤四:修改Index.jsp文件名以及内容:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>Hello Struts</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>
    Hello Struts. <br>
  </body>
</html>

查看struts框架源码方法:

打开我们所创目录下的web app library文件,找到struts2类包,右击选择properties选项进行配置;

查看源码和开发时api文档都可以在此目录下配置,文件路径。

总结:struct框架客户请求响应模式


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值