Visualizzazione dei risultati da 1 a 3 su 3

Visualizzazione discussione

  1. #1

    [Struts2-Java] Usare il convention plug-in di Struts per evitare annotazioni/xml

    Salve a tutti.
    Mi sto appena addentrando nella programmazione server-side con Java EE e sto iniziando ad usare il framework Struts2.
    Sul libro dal quale sto studiando (Learn Java for Web Development di Layka) c'è scritto che è possibile evitare di usare le annotazioni e la mappatura azione-risultato tramite struts.xml semplicemente usando il plug-in convention di struts e seguendo le sue convenzioni.
    Ebbene, questo è il mio progetto:

    web.xml
    codice:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="struts_blank" 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.html</welcome-file>
      </welcome-file-list>
    </web-app>

    struts.xml
    codice:
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
            "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
            "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
        <constant name="struts.devMode" value="true"/>
        <package name="default" namespace="/" extends="struts-default">
            <default-action-ref name="index"/>
            <action name="index">
                <result>/index.html</result>
            </action>
        </package>
        <!-- Add addition packages and configuration here. -->
    </struts>

    index.html (in webapp)
    codice:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head></head>
    <body>
        <form action="hello-world">
            <label for="name">Enter your name:</label>
            <input type="text" name="name" />
            <input type="submit" />
        </form>
    </body>
    </html>

    hello-world.jsp (in WEB-INF/content)
    codice:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%><%@ taglib prefix="s" uri="/struts-tags" %>
    <!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>Hello</title>
    </head>
    <body>
    Welcome <s:property value="name" />
    </body>
    </html>

    HelloWorldAction.java
    codice:
    package com.apress.helloworld.action;
    
    import com.opensymphony.xwork2.Action;
    
    
    public class HelloWorldAction implements Action{
    
    
        private String name;
        
        public String getName() {
            return name;
        }
        
        public void setName(String name) {
            this.name = name;
        }
        
        public String execute() throws Exception {
            return SUCCESS;
        }
    
    
    }

    Mandando in esecuzione il progetto su Tomcat 8.0.23 tramite Eclipse Luna SR2 ottengo il seguente errore:


    codice:
    Unexpected Exception caught setting 'name' on 'class com.opensymphony.xwork2.ActionSupport: Error setting expression 'name' with value ['Un nome a caso', ]

    Se però mappo la action tramite struts.xml o annotazioni tutto funziona a dovere.
    Cosa può essere e come posso risolverlo?

    Grazie mille per la disponibilità.
    Ultima modifica di LeleFT; 19-06-2015 a 11:44 Motivo: Sistemati blocchi HTML/JSP
    I miei esempi in Java qui: http://cdpjavaexamples.altervista.org/index.html

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.