在eclipse中创建一个Dynamic Web Project。具体内容如下:
HelloWorld.java
public class HelloWorld
{
private String username;
public void setUsername(String username)
{
this.username = username;
}
public String getUsername()
{
return username;
}
public String execute()
{
if(getUsername().equals(""))
{
System.out.println("no String input!");
return "error";
}
else
{
System.out.println(getUsername());
return "success";
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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>Struts Blank</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
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>
<package name="default" namespace="/" extends="struts-default">
<action name="index" class="HelloWorld">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Hello World!
<form method="post" action="HelloWorld.action">
<input type="text" name="username" />
<input type="submit" name="submit" />
</form>
</body>
</html>
运行后,出现下面的错误:
HTTP Status 404 - /HelloWorld/HelloWorld.action
--------------------------------------------------------------------------------
type Status report
message /HelloWorld/HelloWorld.action
description The requested resource (/HelloWorld/HelloWorld.action) is not available.
--------------------------------------------------------------------------------
Apache Tomcat/7.0.21
不知是何故??
------解决方案--------------------
<form method="post" action="index.action">