struts2 HelloWorld

Struts 2历史 

最早出现的Struts1是一个非常著名的框架,它实现了MVC模式。Struts1简单小巧,其中最成熟的版本是Struts1.2。 之后出现了WebWork框架,其实现技术比Struts1先进,但影响力丌如Struts1。 在框架技术不断发展过程中,有人在WebWork核心XWork的基础上包装了Struts1(算是两种框架的整合),由此,结合了Struts1的影响力和WebWork的先进技术,Struts 2诞生了。 所以说,Struts2丌是Struts1的升级,它更像是WebWork的升级版本。

开发准备过程

拷贝Struts2的核心Jar包到WEB-INF/lib/下
基本功能核心jar包 5个(2.1.8)
  struts2-core-2.1.8.1.jar(*)
Struts2核心包,是Struts框架的“外衣”
  xwork-core-2.1.6.jar(*)
Struts2核心包,是WebWork内核。
  ognl-2.7.3.jar
用来支持ognl表达式的,类似于EL表达式,功能比EL表达式强大的多。
  freemarker-2.3.15.jar
freemarker是比jsp更简单好用,功能更加强大的表现层技术,用来替代jsp的。 在Struts2中提倡使用 freemarker模板,但实际项目中使用jsp也很多。
  commons-fileupload-1.2.1.jar
用于实现文件上传功能的jar包。

(相关jar包请点击此处下载

项目结构图如下:



配置过程

1、在web.xml中配置Struts2的前端控制器
Struts2用Filter实现的前端控制器(注意丌是Servlet)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>struts2-test</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、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>你好</title>
</head>
<body>
<form action="welcome.action" method="post">
	Please input your name:<input type="text" name="name" /><br />
        Please input your password<input type="password" name = "password" /><br />
	<input type = "submit" value="Submit"  /> 
</form>
</body>
</html>

3、welcome.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>欢迎</title>
</head>
<body>
欢迎您 ${name}(${password})
</body>
</html>

4、新建WelcomeAction类

package com.qiaozhq.outman;

public class WelcomeAction {
	private String name;
	private String password;
	public String execute(){
		System.out.println("WelcomeAction execute...");
		System.out.println("name="+name+",password="+password);
		if("".equals(name)){
			return "fail";
		}
		return "success";
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	
}

5、写struts2所需要的配置文件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>
	<package name="helloworld" extends="struts-default" namespace="/">
	<action name = "hello">
		<result name="success">/hello.jsp</result>
	</action>
	<action name = "welcome" class="com.qiaozhq.outman.WelcomeAction">
		<result name="success">/welcome.jsp</result>
		<result name="fail">/error.jsp</result>
	</action>
	</package>
</struts>

6、测试:

输入网址:http://localhost:8899/struts2-test/hello.jsp(也可输入:http://localhost:8899/struts2-test/hello.action),

输入用户名,密码,点击提交:



转到成功页面:http://localhost:8899/struts2-test/welcome.action


Hello Test(test)

(演示完毕。)


运行机制梳理:

1)客户端在浏览器中输入一个url地址。
2)这个url请求通过http协议发送给tomcat。
3)tomcat根据url找到对应项目里面的web.xml文件。
4)在web.xml里面会发现有struts2的配置。
5)然后会找到struts2对应的struts.xml配置文件。
6)根据url解析struts.xml配置文件就会找到对应的class。
7)调用完class返回一个字String,根据struts.xml返回到对应的jsp。


一个请求在Struts2框架中的处理大概分为以下几个步骤:
1)  客户端初始化一个指向Servlet容器(例如Tomcat)的请求。
2)  这个请求经过一系列的过滤器(Filter)。
3)  接着FilterDispatcher被调用,FilterDispatcher询问ActionMapper来决定这个请是否需要调用某个Action。
4)  如果ActionMapper决定需要调用某个Action,FilterDispatcher把请求的处理交给ActionProxy。
5)  ActionProxy通过Configuration Manager询问框架的配置文件,找到需要调用的Action类。
6)  ActionProxy创建一个ActionInvocation的实例。
7)  ActionInvocation实例使用命名模式来调用,在调用Action的过程前后,涉及到相关拦截器(Intercepter)的调用。
8)  一旦Action执行完毕,ActionInvocation负责根据struts.xml中的配置找到对应的返回结果。
Struts2的核心就是拦截器。Struts.xml中所有的package都要extends="struts-default"。同理与所有的Java类都要extends自Object一样。struts-default.xml里面就是要做以上事情。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值