struts2框架简介+搭建新项目
2018-05-04 本文已影响0人
DouDouZH
一、概述
- 应用在Javaee中web层的框架
- 本质是过滤器
- struts2是struts1和webwork基础上发展的全新框架
-
struts2解决的问题:crud(增删改查操作),和servlet的不同
image.png
- 常见框架:struts2、springMVC
-
基本执行过程
image.png
二、搭建新项目
1.用到的struts2版本

2.配置struts.xml
在src根目录下建立struts.xml文件

找到下载的strust2中blank这个包

打开找到strust.xml复制他的约束条件,按照打开文件的格式写自己的xml

建立的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>
<constant name="false" value="struts.enable.DynamicMethodInvocation"/>
<constant value="true" name="struts.devMode"/>
<package name="default" extends="struts-default" namespace="/">
...这里写配置
</package>
</struts>
3.导入jar包
复制struts2-blank中lib下的所有jar包导入项目中


4.在web.xml中配置过滤器
复制struts2-blank中web.xml下的过滤器配置

配置好的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>struts2.217</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-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>
三、简单的hello world
1.配置过滤器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>struts2.helloworld</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-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>
2.src下配置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>
<constant name="false" value="struts.enable.DynamicMethodInvocation"/>
<constant value="true" name="struts.devMode"/>
<package name="default" extends="struts-default" namespace="/">
<!--name:访问名称 -->
<action name="ok">
<!--配置返回值到页面-->
<result>/HelloWord.jsp</result>
</action>
</package>
</struts>
3.index.jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>hello world</title>
</head>
<body>
<a href="ok">点击跳转<a>
</body>
</html>
4.HelloWorld.jsp界面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>helloworld</title>
</head>
<body>
Hello World
</body>
</html>
5.运行效果

