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>

4.配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>MyFirst</display-name> <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.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>
</web-app>

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

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

6.新建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>

最新文章

  1. 学习ASP.NET Core, 怎能不了解请求处理管道[4]: 应用的入口&mdash;&mdash;Startup
  2. LNMP安装过程
  3. Java编程中的美好
  4. APP弱网测试
  5. PHP学习笔记:用mysqli连接数据库
  6. leetcode_438_Find All Anagrams in a String_哈希表_java实现
  7. php--mongodb的安装
  8. 转:自定义ASP.NET MVC Html辅助方法
  9. VS2013 试用版到期 解决办法
  10. android布局太深导致的 java.lang.StackOverflowError
  11. Android(java)学习笔记189:eclipse 导入项目是提示:某些项目因位于工作空间目录中而被隐藏。
  12. a标签中调用js的几种方法
  13. Java的结构之美【2】——销毁对象
  14. python 3.4.0 简单的print &amp;#39;hello world&amp;#39;,出错--SyntaxError: invalid syntax
  15. ES 15 - Elasticsearch中的数据类型 (text、keyword、date、geo等)
  16. SQL Server对比两字符串的相似度(函数算法)
  17. 关于QT中.pro文件中的相对路径
  18. mongodb 通过mongodump来备份Sharded Cluste分片集群
  19. MyEclipse2014中Java类右键Run as没有JUnit Test
  20. 朴素贝叶斯文本分类实现 python cherry分类器

热门文章

  1. lua连续随机数
  2. 深入理解Eureka - Eureka Client获取注册信息机制
  3. rzsz安装【转】
  4. JS中onclick事件传参
  5. 【WPF】ListBox无法滚动
  6. Eclipse里安装插件
  7. Android——Handler 多线程
  8. 使用C#创建windows 服务
  9. win8.1 win10存储设备和驱动器分开显示
  10. Linux系统分区方案建议