struts2学习笔记1
黑马视频的学习笔记:
简介 :
struts2是应用在Javaee三层框架中的web层的技术。从struts1和webwork发展而来的。struts2请求响应的完整流程。使用版本是2.3.34

小例子
1.使用eclipse新建一个web项目,导入示例程序里的jar包

2.创建action,访问action的时候默认执行里面的execute()方法
package cn.itcast.action;
public class HelloAction {
public String execute() {
return "ok";
}
}
3.在struts.xml里面配置action类的访问路径。注: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="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" />
<package name="hellodemo" extends="struts-default" namespace="/" >
<action name="hello" class="cn.itcast.action.HelloAction">
<result name="ok">/hello.jsp</result>
</action>
</package>
</struts> 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>
hello struts2
</body>
</html>
访问路径:http://localhost:8080/struts2day01/hello.action4.配置过滤器:注:过滤器在服务启动时创建。servlet在第一次访问时创建
<?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>Struts2day01</display-name>
<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>
<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>
</web-app>底层原理图

查看源代码:过滤器在服务器启动时创建,执行里面的init()方法,主要是加载配置文件
配置
1.核心配置文件struts.xml:名称,位置固定.
2.在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="struts.enable.DynamicMethodInvocation" value="true" />
<constant name="struts.devMode" value="true" /> <constant name="struts.i18n.encoding" value="UTF-8" />
<package name="hellodemo" extends="struts-default" namespace="/" > <action name="hello" class="cn.itcast.action.HelloAction"> <result name="ok">/hello.jsp</result> </action> </package><include file="cn/itcast/action/hello.xml"></include></struts>创建action:
三种方式:最常用继承ActionSupport类
package com.struts.action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class HelloAction extends ActionSupport {
public String execute() {
System.out.println("进入Action");
return SUCCESS;
}
}
访问action的方法
1.没有返回值:
package cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class UserAction extends ActionSupport{
@Override
public String execute() throws Exception {
// TODO 自动生成的方法存根
return NONE;
}
}
配置文件
<package name="hellodemo" extends="struts-default" namespace="/" >
<action name="hello" class="cn.itcast.action.HelloAction">
</action>
</package>2.使用method属性
package cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport {
public String add(){
return NONE;
}
public String update(){
return NONE;
}
}
配置文件 <package name="bookdemo" extends="struts-default" namespace="/" >
<action name="addAction" class="cn.itcast.action.BookAction" method="add">
</action>
<action name="updateAction" class="cn.itcast.action.BookAction" method="update">
</action>
</package>
3.使用通配符*: 匹配任意内容
package cn.itcast.action;
import com.opensymphony.xwork2.ActionSupport;
public class BookAction extends ActionSupport {
public String add(){
return NONE;
}
public String update(){
return NONE;
}
}配置文件:
<package name="bookdemo2" extends="struts-default" namespace="/" >
<action name="book_*" class="cn.itcast.action.BookAction" method="{1}">
</action>
</package>4.动态方法访问。。。。

2251

被折叠的 条评论
为什么被折叠?



