前端控制器:Struts2 ,Spring MVC
前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。
springmvc如何和前端页面联系起来
以下摘自:http://jingyan.baidu.com/article/7f766dafbad8f04100e1d054.html
1.springmvc的使用,在controller中通过注解的形式,获取从前端jsp页面传过来的action参数
使用springmvc必须在web.xml中配置(DispatcherServlet控制器),各个属性的说明如下:
load-on-startup:表示启动容器时初始化该Servlet
url-pattern:表示哪些请求交给Spring Web MVC处理,
“/” 是用来定义默认servlet映射的。
也可以如“*.html”表示拦截所有以html为扩展名的请求。
“.do” 将参数作为请求URL传递
Spring Web MVC框架将加载“classpath:dispatcher-servlet.xml”来进行初始化上下文,即根目录下面的dispatcher-servlet.xml配置文件
2.在dispatcher-servlet.xml配置文件中配置,spring可以自动去扫描base-package下面或者子包下面的java文件,如果扫描到有@Component,@Controller,@Service等这些注解的类,则把这些类注册为bean。
即告诉Spring 该到哪里去找标记为@Controller 的Controller 控制器。
@Controller标注:这里的标注对应spring2.5的Controller接口及其实现类,被此标注修饰的类名代表这个类为一个Controller,可以实现请求的转发。
@RequestMapping:使用此标注处理控制器转发过来的url请求。此标注可以添加在类前或者方法前,并形成类似一级、二级的拦截形式。
RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。
当@RequestMapping 标记在Controller 类上的时候,里面使用@RequestMapping 标记的方法的请求地址都是相对于类上的@RequestMapping 而言的;当Controller 类上没有标记@RequestMapping 注解时,方法上的@RequestMapping 都是绝对路径。这种绝对路径和相对路径所组合成的最终路径都是相对于根路径“/ ”而言的
例如请求../student/add.do
Struts2 前端控制器实现:
以下内容摘自:struts2 HelloWorld 网址:http://blog.csdn.net/minelibra/article/details/9669575
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实现的前端控制器。
<span style="font-size: 12px;"><?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></span>
2、hello.jsp2.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
运行机制梳理:
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里面就是要做以上事情。