0

我目前正在struts2使用约定插件学习注释。除了 URL 映射,一切都很好。我想使用以下 URL 访问操作:http://{my domain name}/{action name}.

但经过多次尝试,我总是以http://{my domain name}/{project name}/{action name}

我的行动:

package com.vaanila.action;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

public class WelcomeUserAction {
private String userName;
private String message;

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

@Action(value="/Welcome", results={@Result(name="success", location="successPage.jsp")})
public String execute()
{
    message = "Welcome " + userName;
    return "success";
}
}

我的 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">
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Hello World</title>
</head>
<body>
<s:form action="/Welcome">
    <s:textfield name="userName" label="User Name"/>
    <s:submit />
</s:form>
</body>
</html>

我的成功.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>Welcome User</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>

和 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>HelloWorld</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>
4

1 回答 1

1

您应该使用名称ROOT.war而不是{project name}.war.

于 2013-03-26T07:36:16.243 回答