2016-05-28 9 views
0
<form class = "col l12 s12"> 
    <div class = "row"> 
     <div class="input-field col l6 s6"> 
      <input id="firstName" type="text" class="validate"> 
      <label for="firstName">First Name</label> 
     </div> 
    </div> 
</form> 

Ich möchte Struts2 auf den obigen Code anwenden, um den Wert firstName zu erhalten. Wie soll ich das machen?Wie verwende ich Struts-Formularfelder (s: Textfeld, s: Passwort) und style sie mit materializecss-Framework?

EDIT (ganze jsp Code für Anmeldeformular):

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 

<%@taglib prefix="s" uri="/struts-tags" %> 
<html> 
<head> 
    <!--Import Google Icon Font--> 
    <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> 
    <!--Import materialize.css--> 
    <link type="text/css" rel="stylesheet" href="css/materialize.min.css" media="screen,projection"/> 
    <link type="text/css" rel="stylesheet" href="css/register.css"> 
    <!--Let browser know website is optimized for mobile--> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/> 
</head> 

<body> 
    <!--Import jQuery before materialize.js--> 
    <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script> 
    <script type="text/javascript" src="js/materialize.min.js"></script> 
    <div class = "background"> 
     <h1 class = "center-align white-text">Register</h1> 
    </div> 
    <div class = "container formContainer white z-depth-2"> 
     <div class = "row formParent"> 
      <form class = "col l12 s12"> 
       <div class = "row"> 
        <div class="input-field col l6 s6"> 
         <input id="firstName" type="text" class="validate"> 
         <label for="firstName">First Name</label> 
        </div> 
        <div class="input-field col l6 s6"> 
         <input id="lastName" type="text" class="validate"> 
         <label for="lastName">Last Name</label> 
        </div> 
       </div> 
       <div class = "row"> 
        <div class="input-field col l12 s12"> 
         <input id="username" type="text" class="validate"> 
         <label for="username">Username</label> 
        </div> 
       </div> 
       <div class = "row"> 
        <div class="input-field col s6"> 
         <input id="password" type="password" class="validate"> 
         <label for="password">Enter password</label> 
        </div> 
        <div class="input-field col s6"> 
         <input id="repeatPassword" type="password" class="validate"> 
         <label for="repeatPassword">Repeat password</label> 
        </div> 
       </div> 
       <div class="row"> 
        <div class="input-field col s12"> 
         <input id="email" type="email" class="validate"> 
         <label for="email" data-error="wrong" data-success="right">Email</label> 
        </div> 
       </div> 
       <div class = "row formButtons"> 
        <button class="btn waves-effect waves-light right submitButton purple accent-4" type="submit" name="submit">Submit</button> 
        <a href = 'index.jsp' class = 'btn waves-effect waves-dark right cancelButton purple accent-1'>Cancel</a> 
       </div> 
      </form> 
     </div> 

    </div> 
</body> 

In dem obigen jsp Code, ich wünsche die Feldwerte zu bekommen, und ich möchte, dass sie auf die folgende Lage sein zuweisen actionClass:

public class registerAction { 
    private String firstName, lastName, username; 
    public String execute(){ 
     return ""; 
    } 
} 

aber ich weiß nicht, wie das Streben Präfix auf die jsp Form anzuwenden, die CSS-Eingabefelder wird materialisieren. Hier

ist der Code für struts.xml

<?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.devMode" value="true"/> 
    <package name="helloWorld" extends="struts-default"> 
    <action name="hello" class = "com.bugManager.registerAction" method="execute"> 
     <result name="success">helloWorld.jsp</result> 
    </action> 
    </package> 
</struts> 
+0

Wo möchten Sie den Wert bekommen und wie haben Sie das gemacht? –

+0

Ich möchte den Wert von firstName abrufen und in der Actionclass für die Speicherung in einer Datenbank verwenden. – hulkinBrain

+0

Welche Aktionsklasse, wie viele Aktionsklassen haben Sie, wie senden Sie das Formular? –

Antwort

0

Ok, ich habe es auf meinem eigenen. Wenn jemand will, materializecss Formular-Tags in struts2 verwenden, gehen Sie wie folgt:

1. Sie diese Zeile hinzufügen in den struts.xml

<constant name="struts.ui.theme" value="simple" /> 

meine aktualisiert und Arbeits struts.xml

<struts> 
    <constant name="struts.devMode" value="true"/> 
    <constant name="struts.ui.theme" value="simple" /> <!-- this is the line which helps in applying materializecss's styling to the form elements --> 
    <package name="com.bugManager" extends="struts-default"> 
     <action name="register" class = "com.bugManager.registerAction" method="execute"> 
      <result name="success">helloWorld.jsp</result> 
     </action> 
    </package> 
</struts> 

mein halloWorld.jsp: (jede Umleitungsseite, ich habe diese Seite gemacht, um zu testen, ob die Werte korrekt zugeordnet und gespeichert werden)

<html> 
    <head> 
     <title>Hello World Strut</title> 
    </head> 
    <body> 
     Hello from <s:property value = "firstName"/> <s:property value = "lastName"/> <s:property value = "username"/> <s:property value = "password"/> 
     <s:property value="email"/> 

    </body> 
</html> 

2. Ändern Sie dann

Formular-Tags

<form></form> to <s:form></s:form> 

Text oder E-Mail-Eingangs-Tags zu

<s:textfield /> 

Passwort Felder

<s:password /> 

einreichen können die gleichen bleiben.

Schauen Sie sich die Codebeispiel Form gegeben unter

<s:form class = "col l12 s12" action = "register"> 
    <div class = "row"> 
     <div class="input-field col l6 s6"> 

      <s:textfield name = "firstName" id="firstName" type="text" class="validate"/> 
      <label for="firstName">First Name</label> 
     </div> 
     <div class="input-field col l6 s6"> 
      <s:textfield name = "lastName" id="lastName" type="text" class="validate"/> 
      <label for="lastName">Last Name</label> 
     </div> 
    </div> 
    <div class = "row"> 
     <div class="input-field col l12 s12"> 
      <s:textfield name = "username" id="username" type="text" class="validate"/> 
      <label for="username">Username</label> 
     </div> 
    </div> 
    <div class = "row"> 
     <div class="input-field col s6"> 
      <s:password name = "password" id="password" type="password" class="validate"/> 
      <label for="password">Enter password</label> 
     </div> 
     <div class = 'input-field col s6'> 
      <s:password name = "password" id="repeatPassword" type="password" class="validate"/> 
      <label for="repeatPassword">Repeat password</label> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="input-field col s12"> 
      <s:textfield name = "email" id="email" type="email"/> 
      <label for="email">Email</label> 
     </div> 
    </div> 
    <div class = "row formButtons"> 
     <button class="btn waves-effect waves-light right submitButton purple accent-4" type="submit" name="submit">Submit</button> 
     <a href = 'index.jsp' class = 'btn waves-effect waves-dark right cancelButton purple accent-1'>Cancel</a> 
    </div> 
</s:form> 

Werfen Sie einen Blick auf all die Streben Tags bilden hier:http://www.tutorialspoint.com/struts_2/struts_form_tags.htm

Ich hoffe, das jeder hilft, der versucht, zu implementieren, Materialisierung in Struts2!

+0

Falscher Code und nichts in Bezug auf miterializecss –

+0

Dies ist ein funktionierendes Beispiel für meinen Code, ich glaube nicht, dass etwas mit meinem Code nicht stimmt. Ich konnte Materializscss nicht direkt mit Struts verwenden, um Benutzereingaben (Textfelder und Passwortfelder) Java-Klassenvariablen zuzuweisen, also habe ich versucht, theme: simple in struts.xml hinzuzufügen. Jetzt kann ich materialise CSS verwenden und die Benutzereingaben Java-Klassenvariablen zuweisen. – hulkinBrain