知识点: struts2与sping的集成
步骤一:在web.xml中添加如下代码(首先是一个struts的过滤器,然后是spring的ContextLoaderListener监听器)
步骤二:在src文件夹下建立一个struts.xml文件 配置action 但是要将action交给spring创建,这里通过引用spring bean的一个id与之对应(代码如下)
步骤三:在appicationContext.xml中添加如下代码:
步骤四:编写action(代码如下)
步骤五:jsp页面代码:
源码下载请点这里:
步骤一:在web.xml中添加如下代码(首先是一个struts的过滤器,然后是spring的ContextLoaderListener监听器)
<?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">
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
步骤二:在src文件夹下建立一个struts.xml文件 配置action 但是要将action交给spring创建,这里通过引用spring bean的一个id与之对应(代码如下)
<?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>
<constant name="objectFactory" value="spring"></constant>
<package name="struts2tutoial" extends="struts-default" namespace="/">
<action name="helloWorld" class="helloWorld">
<result name="success">/helloWorld.jsp</result>
</action>
</package>
</struts>
步骤三:在appicationContext.xml中添加如下代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="helloWorld" class="cn.zhuojingxinxi.web.HelloWorld"></bean>
</beans>
步骤四:编写action(代码如下)
package cn.zhuojingxinxi.web;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
public final static String MESSAGE="struts is up and running";
public String execute(){
System.out.println("进来了");
setMessage(MESSAGE);
return ActionSupport.SUCCESS;
}
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
步骤五:jsp页面代码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
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>
<h1>helloWorld jsp</h1>
<h2><s:property value="message" /></h2>
</body>
</html>
源码下载请点这里: