这个整合和myeclipse版本没什么关系
只要遵循一下步骤
需要的包:
commons-fileupload-1.2.jar
commons-io-1.3.1.jar
commons-logging-1.1.jar
freemarker-2.3.8.jar
junit-3.8.2.jar
log4j-1.2.14.jar
mysql-connector-5.1.7.jar
ognl-2.6.11.jar
serializer-2.7.1.jar
spring-2.5.5.jar
struts2-core-2.0.6.jar--
struts2-core-2.0.8.jar
struts2-spring-plugin-2.0.8.jar
xalan-2.7.1.jar
xercesImpl-2.9.0.jar
xml-apis-1.3.04.jar
xwork-2.0.3.jar
建立web工程
web.xml:
<?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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
</web-app>
--------------------------------------------------------------------------------
在src下建立struts.properties
#默认资源文件
#struts.custom.i18n.resources=tutorial.ApplicationMessages
#保存文件临时目录
##struts.multipart.saveDir=/temp
#资源文件修改时的自动加载
struts.i18n.reload=true
#修改Struts配置文件自动加载
struts.devMode=true
struts.configuration.xml.reload=true
#访问action时的后缀
##struts.action.extension=action
现在做一个登录的简单例子
在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>
<!--让spring负责产生bean-->
<constant name="objectFactory" value="spring"/>
<include file="logon.xml" />
</struts>
--------------------------------------------------------------------------------
同目录下的logon.xml
这个namespace有一个规则,还是写成默认 namespace="/" 的好或者不写
namespace="/" URL为
http://127.0.0.1/struts/logon.actionnamespace="/logon" URL为
http://127.0.0.1/struts/logon/logon.action<result>/jsp/logon/input.jsp</result>
开头带"/"表示相对于根目录否则相对于namespace目录(这个没试过)
<action name="Logon!*" method="{1}" class="logonAction">中"*"代表action中对应方法
<?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>
<package name="logon" extends="struts-default" namespace="/">
<!--处理404错误页面等-->
<default-action-ref name="UnderConstruction"/>
<action name="UnderConstruction"><result>/404.jsp</result></action>
<!--<action name="*"><result>/{1}.jsp</result></action>-->
<action name="logon" >
<result>/jsp/logon/input.jsp</result>
</action>
<action name="Logon!*" method="{1}" class="logonAction">
<result>/jsp/logon/success.jsp</result>
<result name="input">/jsp/logon/input.jsp</result>
</action>
<!-- Add your actions here -->
</package>
</struts>
--------------------------------------------------------------------------------
applicationContext.xml:
XML语言: aaa<?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.5.xsd">
<!-- 装在java资源文件 -->
<bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list><value>classpath:jdbc.properties</value></list>
</property>
</bean>
<bean name="logonAction" class="struts.action.logon.LogonAction" scope="prototype">
<property name="service" ref="logonService"/>
</bean>
<bean id="logonService" class="struts.service.logon.LogonService"></bean>
</beans>
--------------------------------------------------------------------------------
struts配置文件中的红色背景部分的class和applicationContext.xml中bean的name或者id对应
此处applicationContext.xml的bean如果不写scope="prototype" 登录出错以后,不能再正确登录,原因是spring缓存了这个bean
jsp页面
/jsp/logon/input.jsp
<%@ page language="java" pageEncoding="UTF-8"
%><%@ taglib prefix="s" uri="/struts-tags"
%><html>
<head><title></title></head>
<body>
<s:if test="hasErrors()">
<font color="red">
ERROR:<s:actionerror/>
<s:fielderror />
</font>
</s:if>
<form method='post' action='Logon!logon.action' name='' target=''>
<input type='text' id='username' name='username' value='username'>
<input type='text' id='password' name='password' value='password'>
<input type='submit'>
</form>
</body>
</html>
--------------------------------------------------------------------------------
struts.action.logon.LogonAction.java
package struts.action.logon;
import struts.service.logon.LogonService;
import com.opensymphony.xwork2.ActionSupport;
public class LogonAction extends ActionSupport{
private static final long serialVersionUID = 1L;
public String logon() throws Exception {
System.out.println("user:"+getUsername()+" password:"+getPassword());
if (isInvalid(getUsername())) {
this.addActionError("用户名为空!");
return INPUT;
}
if (isInvalid(getPassword())) {
this.addActionError("密码为空!");
return INPUT;
}
if(getService().logon(getUsername(),getPassword())){
return SUCCESS;
}else{
this.addActionError("用户名或密码错误!");
}
return INPUT;
}
private boolean isInvalid(String value) {
return (value == null || value.length() == 0);
}
private String username;
private String password;
private LogonService service;
......///set,get方法
}
--------------------------------------------------------------------------------
struts.service.logon.LogonService.java
package struts.service.logon;
public class LogonService {
public boolean logon(String username,String password){
if("admin".equals(username) && "admin".equals(password))return true;
return false;
}
}
--------------------------------------------------------------------------------
浏览器访问
http://127.0.0.1/struts2/logon.action密码不输入提示"密码为空!"
用户名密码不是admin提示 "用户名或密码错误! "