Struts2开发实例
说明:
-
本开发实例是书籍《Java EE架构设计与开发实践》中实例5-1中的一个小demo。由于书本内容较老,所以这个demo并不适合实用,仅做学习使用。
-
使用IDE工具为IDEA,jdk 1.8, tomcat 8x
1. 下载Struts2基本类库
可以直接去Struts官网下载2.3版本的jar包。
或者直接点开以下连接下载整个lib包:Struts2基本类库 (点击下载全部,重命名为lib即可)
2. 新建一个web项目
这里选择之后再添加(不建议用第二种联网下载,因为下载的是比较新的版本,而书本上的例子是比较老的)
点击next进入下一步
填好项目名和路径后,点击finish。
出现一下目录结构证明完成项目创建了。
3.导入并加载Struts2类库
复制第一步中下载的lib包,粘贴到WEB-INF目录下,如下图
导入lib包
有出现小三角型则证明导入成功
创建文件,项目文件结构如下图
4. 修改web.xml文件
正常来说,idea创建后,会自动生成web.xml文件,应该不需要配置此文件。
如没有,请手动添加。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Struts Blank</display-name>
<filter>
<filter-name>struts2</filter-name>
<!-- FilterDispatcher拦截器在高版本中已被弃用,启动时会提示跟换版本,但是在这里不影响使用 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
5. 创建hello.jsp文件
在web根目录下创建hello.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>实例5-1:struts2应用</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">
</head>
<body>
<form action="save.action" method="post">
请输入姓名:<input type="text" name="name"/><br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
6. Action实现类(StrutsAction.java)
package org.action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Map;
public class StrutsAction extends ActionSupport {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
public String execute() throws Exception{
if(!name.equals("HelloWorld")){
Map request=(Map) ActionContext.getContext().get("request");
request.put("name", getName());
return "success";
}
else
{
return "error";
}
}
}
7. 配置struts.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="save" class="org.action.StrutsAction" >
<result name="success">/welcome.jsp</result>
<result name="error">/hello.jsp</result>
</action>
</package>
</struts>
8. 创建welcome.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>实例5-1:struts2应用</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">
</head>
<body>
hello<s:property value="#request.name"/>
</body>
</html>
9. 部署Tomcat环境
10. 运行Tomcat
启动成功后,在浏览器地址栏输入: http://localhost:8080/hello.jsp
点击提交后,出现以下页面则说明成功了~
踩坑:
1. 书本中有错误
2. web.xml文件配置拦截器出错
由于在Struts2的高版本中已经舍弃了这个拦截器,所以如果用新的Struts2类库会导致找不到包的情况。使用我前面提供的链接下载jar包即可。
使用FilterDispatcher拦截器在启动时会提示跟换版本,但是在这里不影响使用。
也可以更换为较新的拦截器(此拦截器在新版本中也被弃用了)
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
3. struts.xml文件爆红
解决方法
4. jsp文件报错
这是因为没有加入tomcat的依赖包
导入tomcat依赖包
5. 500服务器报错
这个错误一般是jdk的问题,建议更换为jdk 1.8版本后,再试试。如果仍然报相同的错误,请仔细检查djk及tomcat调用jre是否有配置错误。