日期: 2016-7-21
内容: Struts框架的搭建和测试,这里使用的版本是:Struts2.3.24版本的struts。
一、 下载相应的jar包为框架搭建作准备:这里只需要准备基础jar包就行了。
二、 在Eclipse中创建一个动态的Java web项目:
三、 添加核心jar包及build path设置:
四、 添加struts.xml配置文件,放置的路径是项目的src目录下,但是假如你的是maven项目的话,应该放在ser/mian/resoure目录下。
struts.xml配置文件可以在你下载的jar包的目录下找到。
导入struts.xml配置文件之后的项目结构:
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="default" namespace="/" extends="struts-default">
<action name="index">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
五、 添加web.xml配置文件的配置: 添加struts2过滤器:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SSH2</display-name>
<!-- 添加欢迎界面:首页 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置struts2过滤器 -->
<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:
package com.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String execute() throws Exception {
System.out.println("执行Action!");
return SUCCESS;
}
}
七、 配置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="default" namespace="/" extends="struts-default">
<!-- <action name="index">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action> -->
<action name="loginAction" class="com.action.LoginAction">
<result>success.jsp</result><!-- <result name="" type=""></result>什么都不写就默认name="success" -->
</action>
</package>
</struts>
八、编写成功跳转页面:
<%@ 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>
<h1>测试成功!</h1>
</body>
</html>
九、启动服务器(Tomcat并测试):
2016/07/21 10:48:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
情報: Choosing bean (struts) for (com.opensymphony.xwork2.UnknownHandlerManager)
2016/07/21 10:48:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
情報: Choosing bean (struts) for (org.apache.struts2.views.util.UrlHelper)
2016/07/21 10:48:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
情報: Choosing bean (struts) for (com.opensymphony.xwork2.util.TextParser)
2016/07/21 10:48:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
情報: Choosing bean (struts) for (org.apache.struts2.dispatcher.DispatcherErrorHandler)
2016/07/21 10:48:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
情報: Choosing bean (struts) for (com.opensymphony.xwork2.security.ExcludedPatternsChecker)
2016/07/21 10:48:40 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
情報: Choosing bean (struts) for (com.opensymphony.xwork2.security.AcceptedPatternsChecker)
2016/07/21 10:48:40 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler ["http-bio-8080"]
2016/07/21 10:48:40 org.apache.coyote.AbstractProtocol start
情報: Starting ProtocolHandler ["ajp-bio-8009"]
2016/07/21 10:48:40 org.apache.catalina.startup.Catalina start
情報: Server startup in 7005 ms
测试完成!