首页 > 代码库 > Strut2------配置环境

Strut2------配置环境

1.导入必要的包

技术分享

2.在src目录下新建struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <!-- 修改后缀 -->
    <constant name="struts.action.extension" value="do,,"/>
    
    <!-- 注意:后面include进来的xml是无效的 -->
    <include file="servlet.xml"/>
    
</struts>

3.在src目录下新建servlet.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <package name="other" namespace="/" extends="struts-default">
        
        <action name="servlet2" class="context.ServletActionDemo2">
            <result name="success" type="redirect">/index.jsp</result>
        </action>
        
    </package>

    <package name="default" namespace="/" extends="struts-default">
        <!-- 没有定义<result>的<action>使用这个 -->
        <global-results>
            <result name="none" type="dispatcher">/index.jsp</result>
        </global-results>
    
        <!-- 局部结果界面  -->
        <action name="servlet" class="context.ServletActionDemo">
            <result name="success">/index.jsp</result>
        </action>
        
        <!-- <action name="servlet2" class="context.ServletActionDemo2">
            
        </action> -->
        
        <action name="servlet3" class="context.ServletActionDemo3">
            
        </action>
        
        <action name="servlet3" class="context.ServletActionDemo3">
        <!-- type的默认值是dispatcher:请求转发,redirect:重定向,url地址显示的是界面地址 -->
        <!-- type="chain"时是action跳转action,而且跳转的action需要写在另一个package里面,如servlet2 -->
            <result name="none" type="chain">servlet2</result>
        </action>
        
     <!-- 这里只测试了这个action,其余的没有测 --> <action name="login" class="context.LoginAction"> <result name="success" type="dispatcher">/index.jsp</result> </action> <action name="login2" class="context.LoginAction2"> <result name="success" type="redirect">/index.jsp</result> </action> </package> </struts>

3.在src目录下新建context包,里面新建类文件LoginAction.java(其他的没有一一列出来喔)

public class LoginAction extends ActionSupport{
    @Override
    public String execute(){
        //HttpServletRequest request = ServletActionContext.getRequest();
        return SUCCESS;
    }
}

4.新建index.jsp文件

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>主界面</title>
  </head>
  
  <body>
    <a href="${pageContext.request.contextPath}/login">跳转login  Servlet</a>
    <h1>${pageContext.request.contextPath}</h1>
  </body>
</html>

 

Strut2------配置环境