1,在eclipse j2ee模式下创建一个名为struts的dymnamic web project 勾选创建web.xml
2,修改工程的默认构建路径,在struts工程右键-properties-java build path 勾选allow ...,修改default output folder 为struts/WebContent/WEB-INF/classes
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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>struts</display-name>
<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>platform-web.xml,struts-default.xml,struts-plugin.xml,../conf/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>
以上config的配置
<param-value>platform-web.xml,struts-default.xml,struts-plugin.xml,../conf/struts.xml</param-value>
位置不能错也不能少一些默认的。
4,创建web-inf下文件夹conf/struts.xml 如下: 参考struts2包下的apps/struts2-blank.war
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="error"/>
</global-exception-mappings>
<action name="login_*" class="com.test.action.LoginAction" method="{1}">
<result name="success">/WEB-INF/jsp/result.jsp</result>
</action>
</package>
</struts>
加入struts2的包
1、commons-fileupload-1.2.2
2、commons-io-2.0.1
3、commons-lang3-3.1
4、commons-logging-1.1.1
5、freemarker-2.3.19
6、javassist-3.11.0.GA
8、ognl-3.0.5
9、struts2-core-2.3.4
10、xwork-core-2.3.4
下面写个测试下1,web-inf/login.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>
<form action="login.action" method="post">
username:<input type="text" name="username"><br>
password:<input type="password" name="password"><br>
<input type="submit" value="submit">
</form>
</body>
</html>
2,web-inf/error.jsp
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Error</title>
</head>
<body>
Error Message: ${ERR_MSG}
</body>
</html>
3,web-inf/jsp/result.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>
username:${requestScope.username }<br>
password:${requestScope.password }
</body>
</html>
4,src/com/test/action/LoginAction.java
package com.test.action;
public class LoginAction {
private String username;
private String password;
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;
}
public String execute() throws Exception {
return "success";
}
}
配置一下window-preference-server再j2ee 下new 一下server,双击进入server locaion 配置
选择第二个为user tomcat installation deploy path 为webapps,如选择第一个eclipse默认加载metadata\.plugins\org.eclipse.wst.server.core\tmp0,第二个server为tmp1,不方便开发。
这样的启动tomcat 访问http://localhost:8080加载的是webapps下的root
访问http://localhost:8080/struts/login.jsp,输入……提交可看到显示页面,成功。恭喜你,你的第一个struts2程序运行起来了。