Struts2框架运行机制

来源:互联网 发布:贵州省大数据局 编辑:程序博客网 时间:2024/05/17 02:46

1.struts2=struts1+webwork

是实现MVC表示层的web框架,主要作用是页面展示,接收请求,分发请求。(SpringMVC)
运行机制:
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;

2.使用struts2框架流程
导包(导入需要的struts2的包)→配置web.xml(主要是做映射,通过web.xml配置找到对应的struts.xml配置文件)→在src里新建,从包里面复制配置dtd的头文件,然后进行配置+action的配置(名字,类名,结果集)
主要相当于取代servlet,减少servlet数量。
3.strtus tag 内部不支持EL表达式
可以使用set标签,当前页面设置一个变量name表示变量的名称 value变量值
   VlaueStack struts值栈
struts控制器,控制器会给每一个请求都创建一个valueStack对象
4.Action和Servlet的比较
servlet单例模式(第一次请求的时候先判断当前的容器是否存在请求的servlet,如果不存在则创建并初始化servlet,如果存在的话,直接调用service→dopost/doget处理请求,通过response响应客户端)
servlet一个实例对应多个请求

Action每一次请求服务器都会去创建一个新的Action的实例,struts控制器会销毁当前Action实例。

servlet中不推荐使用全局变量(线程不安全)
Action(参数都是全局变量)

请求到达struts的核心控制器,核心控制器会去创建新的Action实例(全局变量),先去把当前Action实例的状态清空,下来再初始化参数。
5.测试代码
web.xml配置
<filter>  <filter-name>struts</filter-name>  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  </filter>    <filter-mapping>  <filter-name>struts</filter-name>  <url-pattern>*.action</url-pattern>  </filter-mapping>
struts.xml配置
<struts><!-- 常量(devMode) 表示当前工程处于开发模式(true) false(发布)--><constant name="devMode" value="true"></constant><!-- package 包(多人开发一个struts的工程) namespace 逻辑上区分(命名空间) extends 继承 --><package name="struts" namespace="/" extends="struts-default"><!-- 配置处理请求的Action --><action name="login" class="com.hellojava.action.LoginAction"><!-- 当前action给controller 返回的结果 是指向那个页面 --><result name="ok" type="redirectAction"><param name="actionName">loadAll_book</param><param name="namespace">/</param></result><result name="error">/error.jsp</result></action><!-- <action name="loadAll" class="com.hellojava.action.BookAction" method="loadAll"><result name="ok">/index.jsp</result><result name="error">/error.jsp</result></action><action name="add" class="com.hellojava.action.BookAction" method="save"><result name="ok">/index.jsp</result><result name="error">/error.jsp</result></action><action name="remove" class="com.hellojava.action.BookAction" method="delete"><result name="ok">/index.jsp</result><result name="error">/error.jsp</result></action><action name="update" class="com.hellojava.action.BookAction" method="update"><result name="ok">/index.jsp</result><result name="error">/error.jsp</result></action><action name="loadbyid" class="com.hellojava.action.BookAction" method="loadById"><result name="ok">/index.jsp</result><result name="error">/error.jsp</result></action> --><action name="*_book" class="com.hellojava.action.BookAction" method="{1}"><result name="ok">/index.jsp</result></action></package></struts>
含分页的action类
public class BookAction {private BookService bookService=new BookService();private Book book=new Book();private String[] bookIds;public String[] getBookIds() {return bookIds;}public void setBookIds(String[] bookIds) {this.bookIds = bookIds;}public Book getBook() {return book;}public void setBook(Book book) {this.book = book;}private int pageCount=1;private int displayCount=10;private int maxPageCount;public int getPageCount() {return pageCount;}public void setPageCount(int pageCount) {if(pageCount<1){pageCount=1;}if(pageCount>this.getMaxPageCount()){pageCount=this.getMaxPageCount();}this.pageCount = pageCount;}public int getDisplayCount() {return displayCount;}public void setDisplayCount(int displayCount) {this.displayCount = displayCount;}public int getMaxPageCount() {return bookService.calcMaxPage(displayCount);}public void setMaxPageCount(int maxPageCount) {this.maxPageCount = maxPageCount;}public String loadAll(){List<Book> books=bookService.loadPage(pageCount, displayCount);ServletActionContext.getRequest().getSession().setAttribute("pageCount",pageCount);ServletActionContext.getRequest().getSession().setAttribute("maxPageCount",this.getMaxPageCount());ServletActionContext.getRequest().getSession().setAttribute("books", books);return "ok";}public String save(){System.out.println(book.getBookName()+"--"+book.getBookAuthor()+"--"+book.getBookPrice()+"--"+book.getBookInfo());return "ok";}public String delete(){if(bookIds!=null){for (int i = 0; i < bookIds.length; i++) {System.out.println(bookIds[i]);}}return "ok";}public String update(){System.out.println("update");return "ok";}public String loadById(){System.out.println(this.getBook().getBookId());return "ok";}
工程文件




0 0
原创粉丝点击