【java】快速搭配SSH框架HelloWord
1、准备工作
工具:Myeclipse2014
工程文件:点我下载
2、SSH整合步骤
1、创建一个web工程
2、引入jar包
3、编写相关配置文件
1、打开配置web.xml文件
1、配置Struts2的核心过滤器
2、配置String监听器
Web.xml代码
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="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">
<!-- 配置Spring的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置Strust的核心过滤器 -->
<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>
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
2、配置Struts.xml文件
1、新建一个文件命名为struts.xml(名字一定要对)
生成
Struts.xml代码
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEstruts PUBLIC
"-//ApacheSoftware Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 开发模式 -->
<constantname="struts.devMode"value="false"/>
<packagename="EShop"extends="struts-default"namespace="/">
<actionname="hello"class="helloAction">
<resultname="hello">/WEB-INF/jsp/hello.jsp</result>
</action>
</package>
</struts>
3、新建一个jsp文件
Hello.jsp代码:
<%@ page language="java"contentType="text/html;charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtml PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<metahttp-equiv="Content-Type"content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
HelloSSH!
</body>
</html>
3、配置applicationContext.xml文件
1、在Src目录下新建一个文件
2、applicationContext.xml代码
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启注解事务 -->
<tx:annotation-driventransaction-manager="transactionManager"/>
<!-- Action的配置 ===========================-->
<!-- 首页访问的Action -->
<beanid="helloAction"class="cn.gxxy.eshop.hello.action.HelloAction"scope="prototype">
</bean>
</beans>
3、新建Action包和相关的类
1、新建cn.gxxy.eshop.hello.action包,再在其下新建一个HelloAction类
HelloAction代码:
packagecn.gxxy.eshop.hello.action;
importcom.opensymphony.xwork2.ActionSupport;
public class HelloAction extends ActionSupport{
public String execute(){
return"hello";
}
}
4、效果
1、点击运行项目
2、在浏览器输入http://127.0.0.1:8080/EShop/hello.action