在eclipse下配置struts
1. 下载struts包,地址:http://struts.apache.org/download.cgi#struts23163我下载的是Struts 2.3.30,如图:
2. 新建web工程,我建立的工程名为 MyTry ,不要忘了在最后finish前打钩,即添加web.xml。
3. 将struts-2.3.30文件夹下的lib文件里的jar添加到工程的lib文件夹下。然后将这些包buildpath。
4. 将struts-2.3.30文件夹下的apps文件里的struts2-blank包打开,找到struts.xml文件,将它复制到你新建的工程里,并对其进行修改,这是我修改后的struts.xml 文件:
<?xml version="1.0"encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 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="true" />
<package name="default" namespace="/"extends="struts-default">
<action name="HelloWorld" class="com.HelloWorld">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>
5. 修改web.xml配置文件,添加过滤器:
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
6. 新建HelloWorld类,如图:
添加相应的方法,代码如下:
package com;
import com.opensymphony.xwork2.ActionSupport;
public class HelloWorld extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String execute()
{
if(name.equals(""))
return "error";
else
return "success";
}
}
7. 新建 success.jsp , error.jsp, weicome.jsp。
success.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>
<center>this is success!</center>
</body>
</html>
error.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>
<center>this is error!</center>
</body>
</html>
welcome.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="HelloWorld.action" method="post">
<input type="text" name="name" >
<input type="submit" value="提交"/>
</form>
</body>
</html>
这样,程序就可以运行了。
