基于Eclipse搭建SSH框架:第一篇 配置struts2

SSH是Java web开发中常用的框架,本文将讲解SSH框架的搭建过程。这里使用的SSH分别为struts2.3.31,spring3.2.3以及hibernate4.3.10。

具体下载地址,百度即可。还有搭建SSH框架所需要的JDK,eclipse,tomcat这些软件,它们的安装方法这里就不介绍了。下面介绍具体的搭建步骤:

一、在Eclipse中创建一个web项目并配置好Struts2

    1.在eclipse中点击File->New->Dynamic Web Project,并输入项目名称SSHDemo,然后点击Finish。

    2.配置Struts2,将Struts2需要的jar包复制到WebContent下的WEB-INF下的lib里面。必要的jar包如下:

   

     

    3.在WEB-INF下创建web.xml文件,并编辑其内容如下:

<?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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	 <display-name>S2SH</display-name>
	   	<filter>
		<!-- Filter名字 -->
		<filter-name>struts2</filter-name>
		<!-- Filter入口 -->
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<!-- Filter名字 -->
		<filter-name>struts2</filter-name>
		<!-- 截获的URL -->
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
	</welcome-file-list>
</web-app>

     

  4.在src下创建com.integration.action包,并在包中创建TestAction类,代码如下:

package com.integration.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{
	
	public String execute() throws Exception{
		
		return "success";
	}

}
      正如看到的一样,这个类什么也没做,直接返回success。

   5.在src下创建struts.xml文件,编辑其内容如下:

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- 指定struts.xml文件的根元素 -->
<struts>
	<constant name="struts.i18n.encoding" value="UTF-8"></constant>
 <!-- 	<constant name="struts.objectFactory" value="spring" />
  -->
	<!-- 定义包 -->
	<package name="default" namespace="/" extends="struts-default">

		<action name="test" class="com.integration.action.TestAction">
			<result name="success">/success.jsp</result>
		</action>
		
	</package>

</struts>
 

   6.SSHDemo项目概览

     

       其中index.jsp与success.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 World!!
</body>
</html>

<%@ 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>
访问成功!!
</body>
</html>

  7.运行SSHDemo项目

         在项目上右键点击Run As->Run on Server

        

         在地址栏输入localhost:8080/SSHDemo/test

        

         到此,Struts2配置完成。下一篇博客介绍Struts2与Spring的整合。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值