您的位置:首页 > 编程语言 > Java开发

Struts2框架学习(一)——Struts2的概念及搭建

2018-02-24 17:15 267 查看

一、Struts2的概念



使用优势:1)自动封装参数

     2)参数校验

     3)结果的处理(转发|重定向)

     4)国际化

     5)显示等待页面

     6)防止表单重复提交

Struts2具有更加先进的架构以及思想

Struts2的历史:1>Struts1与Struts1的区别技术上没有关系

        2>struts2的前身是webwork框架

二、搭建Struts2框架

首先下载Struts2的开发包,官网:https://struts.apache.org/

1、创建一个web工程并导入jar包



2、创建一个jsp页面

在WebContent下创建hello.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>
<h1>hello world!</h1>
</body>
</html>


3、书写Action类

package cn.itheima.a_hello;

public class HelloAction {

public String hello(){
System.out.print("hello world!");
return "success";
}
}


4、书写src下的struts.xml

Action类写好之后,Struts2框架如何识别它就是一个Action呢,需要对Action类进行配置

先导入约束(window--->Preferences--->XML--->XML Catalog--->User Specified Entries窗口,点击Add按钮)



在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>
<package name="hello" namespace="/hello" extends="struts-default">
<action name="HelloAction" class="cn.itheima.a_hello.HelloAction" method="hello">
<result name="success">/hello.jsp</result>
</action>
</package>
</struts>


5、将struts2核心过滤器配置到web.xml

web层的框架特点就是基于前端控制器的模式,这个前端控制器由过滤器实现,所以需要配置Struts2的核心过滤器,这个过滤器的名称是StrutsPrepareAndExecuteFilter。

在web.xml中进行如下配置:



6、发布到服务器并测试

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: