2013-03-26 66 views
0

我目前在使用约定插件学习struts2注释。除了URL映射外,一切都很顺利。我想要访问类似以下网址的操作:http://{my domain name}/{action name}使用约定插件在Struts2中更改URL映射

但很多尝试之后,我总是结束了与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> 

我的success.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> 

回答

1

你应该用名ROOT.war而不是{project name}.war部署应用程序。

+0

我尝试了你的建议,但现在我在Tomcat Manager中看不到我的项目,当我尝试手动部署它时,它说该文件已存在于服务器 – 2013-03-26 07:45:04

+0

@Doan您必须删除默认的'ROOT Tomcat的.war'。 – Pigueiras 2013-03-26 07:47:09