springmvc+struts2+ant整合

本文为初学者使用,高手请指点

一、文件结构


二、需要准备的jar包

spring的jar包,还有下载struts2的jar包。将struts2-core-2.3.15.1.jar,struts2-spring-plugin-2.3.15.1.jar,xwork-core-2.3.15.1.jar,freemarker-2.3.19.jar,ognl-3.0.6.jar等,其他需要的话自己添加。


三、代码

\springMvc\war\WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>springMvc</display-name>
	<servlet>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

	<!-- 配置上下文载入器 -->
	<!-- 上下文载入器载入除DispatcherServlet载入的配置文件之外的其它上下文配置文件 -->
	<!-- 最常用的上下文载入器是一个Servlet监听器,其名称为ContextLoaderListener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>    
            <!-- 这里是需要的struts配置文件,当修改配置文件位置是,需要将默认的其他几个配置文件加到此处 -->  
                struts-default.xml,  
                struts-plugin.xml,  
                struts.xml  
            </param-value>  
		</init-param>
	</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>

</web-app>

\springMvc\war\WEB-INF\springMvc-servlet.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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="net.spring.test" />

	<!--
		the application context definition for the springapp DispatcherServlet
	-->
	<!--
		<bean name="/hello.html"
		class="net.spring.controller.HelloWorldController"/>
	-->

	<bean id="viewResolver"
		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

\springMvc\src\net\spring\service\LoginService.java

package net.spring.service;

/**
 * 
 */
public interface LoginService {
    public boolean isLogin(String userName, String password);
}

\springMvc\src\net\spring\service\impl\LoginServiceImpl.java

package net.spring.service.impl;

import net.spring.service.LoginService;

/**
 * 
 */
public class LoginServiceImpl implements LoginService {

    @Override
    public boolean isLogin(String userName, String password) {
        return false;
    }

}

\springMvc\src\net\spring\action\LoginAction.java

package net.spring.action;

import net.spring.service.LoginService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 */
public class LoginAction extends ActionSupport {

    /**  */
    private static final long serialVersionUID = -6376896299697010303L;

    private LoginService      loginService;
    private String            userName;
    private String            password;

    @Override
    public String execute() throws Exception {
        System.out.println("In Struts!");
        if (loginService.isLogin(userName, password))
            return SUCCESS;
        else
            return INPUT;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}


\springMvc\war\WEB-INF\applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="  
     http://www.springframework.org/schema/beans   
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/tx  
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
     http://www.springframework.org/schema/aop   
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<context:annotation-config />

	<bean id="dalService" class="net.spring.dal.impl.DalServiceImpl"></bean>

	<bean id="loginService" class="net.spring.service.impl.LoginServiceImpl"></bean>
	<bean id="loginAction" class="net.spring.action.LoginAction"
		scope="prototype">
		<property name="loginService" ref="loginService"></property>
	</bean>
</beans>

\springMvc\src\struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
   "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
   "http://struts.apache.org/dtds/struts-2.3.dtd">
	<!-- Struts 2配置文件的根元素 -->
<struts>

	<constant name="struts.objectFactory"
		value="org.apache.struts2.spring.StrutsSpringObjectFactory" />

	<constant name="struts.i18n.encoding" value="GBK" />
	<constant name="struts.devMode" value="true" />

	<package name="login" extends="struts-default" namespace="/login">
		<!--
			定义处理用户请求的Action,该Action的class属性不是实际处理类 , 而是Spring容器中的Bean实例
		-->
		<action name="loginPro" class="loginAction">
			<!-- 为两个逻辑视图配置视图页面 -->
			<result name="input">/WEB-INF/jsp/result.jsp</result>
			<result name="success">/WEB-INF/jsp/hello.jsp</result>
		</action>
		<!-- 让用户直接访问该应用时列出所有视图页面 -->
		<action name="*">
			<result>/WEB-INF/jsp/{1}.jsp</result>
		</action>
	</package>

</struts>


\springMvc\war\WEB-INF\web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<display-name>springMvc</display-name>
	<servlet>
		<servlet-name>springMvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springMvc</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>

	<!-- 配置上下文载入器 -->
	<!-- 上下文载入器载入除DispatcherServlet载入的配置文件之外的其它上下文配置文件 -->
	<!-- 最常用的上下文载入器是一个Servlet监听器,其名称为ContextLoaderListener -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>    
            <!-- 这里是需要的struts配置文件,当修改配置文件位置是,需要将默认的其他几个配置文件加到此处 -->  
                struts-default.xml,  
                struts-plugin.xml,  
                struts.xml  
            </param-value>  
		</init-param>
	</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>

</web-app>

\springMvc\war\WEB-INF\jsp\login.jsp

<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
 <%@taglib prefix="s" uri="/struts-tags"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
     <title>登录页面</title>
 </head>
 <body>
 <h3>用户登录</h3>
 <s:form action="loginPro"  method="post" namespace="/login">
     <s:textfield name="userName" label="用户名1"/>
     <s:password name="password" label="密码"/>
     <tr align="center">
         <td colspan="2">
         <s:submit value="登录" theme="simple"/>
         <s:reset value="重设" theme="simple"/>
         </td>
     </tr>
 </s:form>
 </body>
 </html>

\springMvc\war\WEB-INF\jsp\hello.jsp

<html>
<head>
<title>Hello :: Spring Application</title>
</head>
<body>
<h1>Hello - Spring Application</h1>
<p>Greetings.</p>
<p>Message: <p>${message}</p> </p>
</body>
</html>

\springMvc\war\WEB-INF\jsp\result.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>

<!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=GB18030">
<title>Insert title here</title>
</head>
<body>
    用户名非法:<font color="red">${exception.message}</font>
</body>
</html>



\springMvc\build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="springMvc" basedir="." default="usage">
	<property file="build.properties" />
	<property name="src.dir" value="src" />
	<property name="web.dir" value="war" />
	<property name="build.dir" value="${web.dir}/WEB-INF/classes" />
	<property name="name" value="springMvc" />

	<path id="master-classpath">
		<fileset dir="${web.dir}/WEB-INF/lib">
			<include name="*.jar" />
		</fileset>
		<fileset dir="${appserver.lib}">
			<include name="servlet*.jar" />
		</fileset>
		<pathelement path="${build.dir}" />
	</path>

	<target name="usage">
		<echo message="" />
		<echo message="${name} build file" />
		<echo message="-----------------------------------" />
		<echo message="" />
		<echo message="Available targets are:" />
		<echo message="" />
		<echo message="build --> Build the application" />
		<echo message="deploy --> Deploy application as directory" />
		<echo message="deploywar --> Deploy application as a WAR file" />
		<echo message="install --> Install application in Tomcat" />
		<echo message="reload --> Reload application in Tomcat" />
		<echo message="start --> Start Tomcat application" />
		<echo message="stop --> Stop Tomcat application" />
		<echo message="list --> List Tomcat applications" />
		<echo message="" />
	</target>

	<target name="build" description="Compile main source tree java files">
		<mkdir dir="${build.dir}" />
		<javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failοnerrοr="true">
			<src path="${src.dir}" />
			<classpath refid="master-classpath" />
		</javac>
	</target>

	<target name="deploy" depends="build" description="Deploy application">
		<copy todir="${deploy.path}/${name}" preservelastmodified="true">
			<fileset dir="${web.dir}">
				<include name="**/*.*" />
			</fileset>
		</copy>
	</target>

	<target name="deploywar" depends="build" description="Deploy application as a WAR file">
		<war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml">
			<fileset dir="${web.dir}">
				<include name="**/*.*" />
			</fileset>
		</war>
		<copy todir="${deploy.path}" preservelastmodified="true">
			<fileset dir=".">
				<include name="*.war" />
			</fileset>
		</copy>
	</target>

	<!-- ============================================================== -->
	<!-- Tomcat tasks - remove these if you don't have Tomcat installed -->
	<!-- ============================================================== -->

	<path id="catalina-ant-classpath">
		<!-- We need the Catalina jars for Tomcat -->
		<!-- * for other app servers - check the docs -->
		<fileset dir="${appserver.lib}">
			<include name="catalina-ant.jar" />
		</fileset>
	</path>

	<taskdef name="install" classname="org.apache.catalina.ant.DeployTask">
		<classpath refid="catalina-ant-classpath" />
	</taskdef>
	<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
		<classpath refid="catalina-ant-classpath" />
	</taskdef>
	<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
		<classpath refid="catalina-ant-classpath" />
	</taskdef>
	<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
		<classpath refid="catalina-ant-classpath" />
	</taskdef>
	<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
		<classpath refid="catalina-ant-classpath" />
	</taskdef>

	<target name="install" description="Install application in Tomcat">
		<install url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}" war="${name}" />
	</target>

	<target name="reload" description="Reload application in Tomcat">
		<reload url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}" />
	</target>

	<target name="start" description="Start Tomcat application">
		<start url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}" />
	</target>

	<target name="stop" description="Stop Tomcat application">
		<stop url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" path="/${name}" />
	</target>

	<target name="list" description="List Tomcat applications">
		<list url="${tomcat.manager.url}" username="${tomcat.manager.username}" password="${tomcat.manager.password}" />
	</target>

	<!-- End Tomcat tasks -->

</project>

\springMvc\build.properties

# Ant properties for building the springapp
 
appserver.home=E:/apache-tomcat-7.0.29
# for Tomcat 5 use $appserver.home}/server/lib
# for Tomcat 6 use $appserver.home}/lib
appserver.lib=${appserver.home}/lib
 
deploy.path=${appserver.home}/webapps
 
tomcat.manager.url=http://localhost:8080/manager/html
tomcat.manager.username=admin
tomcat.manager.password=admin

source.home=./src

OK ,代码写完了,运行ant build。

有错误提示 struts.xml文件没有找到。

发现WEB-INF\classes 目录下面struts.xml文件没有拷贝到这个目录下面。

修改 build.xml文件在build的时候修改为如下

<target name="build" description="Compile main source tree java files">
		<mkdir dir="${build.dir}" />
		<javac destdir="${build.dir}" source="1.5" target="1.5" debug="true" deprecation="false" optimize="false" failοnerrοr="true">
			<src path="${src.dir}" />
			<classpath refid="master-classpath" />
		</javac>
		<copy todir="${build.dir}">
			<fileset dir="${source.home}" excludes="CVS,**/*.java" />
		</copy>
	</target>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sinom21

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值