2013-03-26 62 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