在eclipse使用部署struts2

##struts2 jar##

commons-fileupload-1.2.2.jar

commons-io-2.0.1.jar

commons-lang-2.4.jar

commons-lang3-3.1.jar

freemarker-2.3.19.jar

javassist-3.11.0.GA.jar

ognl-3.0.6.jar

struts2-core-2.3.12.jar

xwork-core-2.3.12.jar

##eclipse##

{Eclipse Java EE IDE for Web Developers.

Version: Kepler Service Release 1

Build id: 20130919-0819}

##步骤##

1 新建 dynamic web project new>>web>>dynamic web project

2 导入jar 把上述jar复制粘贴到webcontent>>web-inf>>lib

3 新建一个action 类

package user;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {
	private String username;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}
	@Override
	public String execute()  {
		if(username.equals("struts2"))
		{
			return SUCCESS;
		}		
		else
		{
			return "FAILURE";
		}
	}
}

 4 在src里新建struts.xml

<?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>
<package name="user" namespace="/user" extends="struts-default">
<action name="login" class="user.LoginAction">
	<result name="success">/user/success.jsp</result>
	<result name="FAILURE">/user/failure.jsp</result>
</action>
</package>
 </struts>

  notice:

>1 注意文件[struts.xml]名称的拼写

>2   对应的是返回的页面 

<result name="success">/user/success.jsp</result><result name="FAILURE">/user/failure.jsp</result>

 

        下边的namespace和name对应的action 的url 例如:http://localhost:8080/项目名称/user/login 

<package name="user" namespace="/user" extends="struts-default"><action name="login" class="user.LoginAction">

        下边的class对应是的是上述url 是请求的哪个action  

<action name="login" class="user.LoginAction">

5  在web.xml中加 filter 和 filter-mapping 

<?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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="WebApp_ID" version="2.5">
	<display-name>strutsing</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>classpath: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.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 6 新建请求页面 webcontent 中 

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!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>
Hi,How are you?
<s:form action="user/login" method="get">
<s:textfield label="户名" name="username"></s:textfield>
<s:password label="密码" name="passworld"/>
<s:submit label="Submit"></s:submit>
</s:form>
</body>
</html>

 notice:

>表示引入struts tags  

<%@ taglib prefix="s" uri="/struts-tags" %>

>展示struts tags 的使用 

<s:form

 >紫色就是相对于请求页面 ation的url 

<s:form action="user/login" method="get">

7 在webcontent中新建 folder user和两个返回页面[success.jsp,failure.jsp]

至此你可以调试你的web project了

你可能感兴趣的:(eclipse,struts2)