官网:http://struts.apache.org/
Struts2 是目前较为普及和成熟的基于MVC设计模式的web应用程序框架。
优点:
以上是使Struts2 成为准企业框架的十大优点。
缺点:
最后说明一点,一个好的框架应该提供各种类型的应用程序都可以使用的通用行为,Struts2 是最好的Web框架之一,并频繁用于RIA(Rich Internet Applications)的发展。
创建Web项目HeadFirstStruts2Chap01
添加依赖jar包
配置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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>HeadFirstStruts2Chap01</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>
<!-- 拦截用户的请求交给Struts2处理 -->
<filter>
<filter-name>Struts2</filter-name>
<filter-class> <!-- Struts2核心处理器 负责分发请求 -->
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>
创建HelloWorldAction.java:
package com.ordinov.action;
import com.opensymphony.xwork2.Action;
public class HelloWorldAction implements Action {
@Override
public String execute() throws Exception {
System.out.println("执行了Action的默认方法");
return SUCCESS;
}
}
配置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>
<package name="helloWorld" extends="struts-default" >
<action name="hello" class="com.ordinov.action.HelloWorldAction">
<result name="success">helloWorld.jsp</result>
</action>
</package>
</struts>
创建helloWorld.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Struts2 大爷你好!
</body>
</html>
项目结构如下:
启动Tomcat-->
访问http://localhost:8080/HeadFirstStruts2Chap01/hello -->
原文:https://www.cnblogs.com/guoyx/p/12507009.html