前言:当时在工作中,一个正在学习计算机的同学对struts不是太理解,但是马上又要迎接考试,我给她大概讲了一遍一个简单的操作流程,讲完之后,归纳成了下面的笔记。
一:Struts2的执行过程
页面发送请求被web.xml配置的拦截器拦截,之后进入struts.xml文件,找到对应的action的名字,进入action类,执行execute或者自定义的方法,在方法里进行一系列业务操作(比如操作数据库),在方法的最后return "*"
(返回值),如果返回值跟struts.xml文件的<result name="*">你的页面</result>中的name值相同,就跳转相应的页面。
接下来图文详细进行说明,这里我用MyEclipse来做演示,Eclipse是一个道理的。

首先先点开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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.xml是初始化配置信息的,这里发现这个文件里只有一个欢迎页面,就是我直接运行这个项目,首页是index.jsp,这里可以更改成自己想要跳转的页面。
用MyEclipse导入struts2
点击项目右键选择MyEclipse,然后选择Add Struts Capabilities...((⊙﹏⊙)这里不是很好截图,。。。)

这里选择struts2.1,因为我们用的就是struts2,URL pattern选择/*
(拦截所有请求),当然这里也可以固定死,可以选择.action(以.action进行结尾),.do(以.do进行结尾),然后直接finish即可,因为我们现在也不需要额外的jar包。
这里会发现项目下多了个struts2.xml

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>
<welcome-file-list>
<welcome-file>index.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>
添加action和配置struts.xml
我们来配置struts.xml之前先创建一个action类

HiAction:
package cn.com.struts2Demo.action;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class HiAction extends ActionSupport{
public String execute(){
//两个sucess一个意思
//return "success";
return SUCCESS;
}
}
当页面提交走这个action类的时候,默认走execute方法,前提是必须要继承ActionSupport,继承之后会重写execute
方法,而且看我上面代码,当我继承之后,我可以直接return SUCCESS,
这里的SUCCESS
是因为我继承了ActionSupport才有的,当然还有INPUT
、ERROR
等。
编写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="myPage" extends="struts-default">
<action name="hi" class="cn.com.struts2Demo.action.HiAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
这里的pageage是java的包机制,name="myPage"是我给这个包起的名字,extends了struts-default,这里可以记成它是默认写法,意思就是我继承了它,也就用了它的一些功能,下面的<action name="hi" class="cn.com.struts2Demo.action.HiAction">是我给action起个名字,对应的是HiAction这个类,这里要跟表单对应才能进入对应的action,<result name="success">这里的success就是我在action类的execute方法中写的reutn SUCCESS,这两个要对应,才能跳转到/success.jsp。
编写页面
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>
<center>
<form action="hi.action" method="post">
<input type="submit" value="点我" />
</form>
</center>
</body>
</html>
说明:这里的hi.action的hi要跟struts.xml中的action的name要对应
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'success.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>
<center>
<h1>hi,struts2!</h1>
</center>
</body>
</html>
好啦,页面访问localhost:8080/struts2Demo
不过,也遇到了问题:

修改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>
<center>
<form action="hi_execute.action" method="post">
<input type="submit" value="点我" />
</form>
</center>
</body>
</html>
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="myPage" extends="struts-default">
<action name="hi_*" class="cn.com.struts2Demo.action.HiAction" method="{1}">
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>
说明<action name="hi_">这个再加上method={1},就是代表hi_方法名,我action里有个execute方法,所以修改成这样了,不知道上面为什么报错。。。好久没接触了。。。
重新编译运行:

网友评论