Question

I'm currently learning struts2 annotation with convention plug-in. Everything went okay except URL mapping. I want to access to action with URL like: http://{my domain name}/{action name}.

But after many tried, i always ended up with http://{my domain name}/{project name}/{action name}

My Action:

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";
}
}

My 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>

My 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>

and 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>
Was it helpful?

Solution

You should deploy your application with the name ROOT.war instead of {project name}.war.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top