之前一段时间学习了springmvc+mybatis+spring框架,突然对之前的struts东西有点陌生, 所以这里简单记录下温故而知新的东西吧。

1.  首先建立一个Dynamic Web Project, 下面是我建立的StrutsDemo项目目录,使用的是maven构建的, 自己又增加了几个文件夹

2. 给出pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>StrutsDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>StrutsDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-core -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.16</version>
</dependency> <!--下面这两个插件,是注解方式必须要加载的-->
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-convention-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.5.16</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-config-browser-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-config-browser-plugin</artifactId>
<version>2.5.16</version>
</dependency> </dependencies>
<build>
<finalName>StrutsDemo</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

3. 给出Struts.xml文件:

<?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>
<!-- 所有匹配*.action的请求都由struts2处理 -->
<constant name="struts.action.extension" value="action" />
<!-- 是否启用开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- struts配置文件改动后,是否重新加载 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- 设置浏览器是否缓存静态内容 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 请求参数的编码方式 -->
<constant name="struts.i18n.encoding" value="utf-8" />
<!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 -->
<constant name="struts.i18n.reload" value="true" />
<!-- 文件上传最大值 -->
<constant name="struts.multipart.maxSize" value="104857600" />
<!-- 让struts2支持动态方法调用 -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<!-- Action名称中是否还是用斜线 -->
<constant name="struts.enable.SlashesInActionNames" value="false" />
<!-- 允许标签中使用表达式语法 -->
<constant name="struts.tag.altSyntax" value="true" />
<!-- 对于WebLogic,Orion,OC4J此属性应该设置成true -->
<constant name="struts.dispatcher.parametersWorkaround" value="false" /> <package name="basePackage" extends="struts-default"> </package> </struts>

4. 给出web.xml文件:

<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app>
<display-name>Archetype Created Web Application</display-name> <!-- add struts2 configiguration -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- end add struts2 configuration--> <!-- 项目默认页 -->
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>

5. 给出TestAction.java文件:

package com.strutsdemo.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result; @ParentPackage("basePackage")
@Action
@Namespace("/")
public class TestAction {
// http://localhost:8080/StrutsDemo/print!printSome.action
// 这里是通过注解的方式来输出前台页面,
// 大致的流程
// 1. 输入的url http://localhost:8080/StrutsDemo/print!printSome.action
// 2. action定位到TestAction的printSome方法, 因为注解value="print"
// 3. printSome方法返回print字符串, 而在我们注解results当中name="print",相互匹配, 所以会跳转到/page/print.jsp页面中
// 个人认为,通过注解方式可以很大程度上节省代码量,让代码更简单
@Action(value="print", results= {@Result(name="print", location="/page/print.jsp")})
public String printSome(){
System.out.println("TestAction printSome");
return "print";
} }

6. 给出print.jsp代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h2>print something</h2>
</body>
</html>

7. 运行项目,并在浏览器中输入url:  http://localhost:8080/StrutsDemo/print!printSome.action,  结果为:

print something

最新文章

  1. 【疑难杂症系列01】TypeError: alert is not a function
  2. c语言快速入门3
  3. atitit.解决net.sf.json.JSONException There is a cycle in the hierarchy
  4. [GO编程] GO入门语法基础
  5. Lambda表达式之Python
  6. VS2008试用版到期解决办法
  7. python中列表和字典的高级应用
  8. javaScript中获取鼠标位置的理解
  9. &quot;Cannot find entry symbol nable-stdcll-fixup; defaulting to 00401000&quot; 解决方案
  10. PPT素才搜索简谈
  11. QQ互联第三方登陆 redirect uri is illegal(100010)
  12. hive发杂数据结构的使用,struct,array,map
  13. List和符号分隔的字符串互相转换
  14. for循环输出树木的形状【java】
  15. HUST1017(KB3-A Dancing links)
  16. 怎样在excel中快速输入当前日期和时间
  17. python基础之删除文件及删除目录的方法-乾颐堂
  18. 492. Construct the Rectangle
  19. Vivado 常见报错
  20. hbase-列存储动态数据库

热门文章

  1. ubuntu 13.04 安装 JDK
  2. 阶段3-团队合作\项目-网络安全传输系统\sprint0-产品规划与设计\第2课-产品功能模型设计
  3. jquery 简单入门
  4. Note: Eleos: ExitLess OS Services for SGX Enclaves
  5. Java中Object类的公有方法
  6. SQL数据库操作整理
  7. WPF动画——故事板(Storyboard)
  8. 《OD玩阿里云》搭建环境
  9. SKlearn中分类决策树的重要参数详解
  10. [sql Server]除非另外还指定了TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效