Struts2使用步骤
1创建一个javaweb project
2添加struts2基本类库
3在web.xml中添加前端控制器
4在src文件夹下建立配置文件 struts.xml
5编写控制器action
6编写视图jsp
步骤1
步骤2
拷贝jar包后 add building path
步骤3 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<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>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
步骤4 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="one" namespace="day01" extends="struts-default">
<action name="hello" class="com.liangjing.action.HelloAction">
<result> /index.jsp </result>
</action>
</package>
</struts>
步骤5 HelloAction.java
package com.liangjing.action;
public class HelloAction {
public HelloAction(){
System.out.println("HelloAction()...");
}
public String execute(){
System.out.println("HelloAction.execute()...");
return "success";
}
}
步骤6 index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
This is my JSP page. <br>
</body>
</html>
经过如上步骤 出现如下错误
将步骤4中
namespace="day01"
改成
namespace="/day01"
输出结果
myeclipse输出
第一个struts2 程序结束
=======================================================================================
struts2执行流程
1请求提交给前端控制器
2根据配置信息确定要调用的action
3创建一个ValueStack
4创建Action,把Action放到ValueStack栈顶
5调用Action的方法,根据输入属性算输出属性
6在调用Action前后 ,调用拦截器Interceptor
7根据Action返回的字符串,确定result
8调用result将ValueStack中的数据按照特定的格式输出
9很多情况下result转到jsp
struts的核心组件
1前端控制器
2值栈ValueStack
3Action
4result
5拦截器Interceptor
6标签Tags