2015-01-09 72 views
0

我想在使用NetBeans 7.4的Struts中獲取用戶輸入。有兩個.jsp文件,用戶輸入文本的welcomeStruts.jsp文件和另一個用於顯示文本的index.jsp文件。問題是:我需要使用哪個標籤庫來處理用戶輸入?在Struts1的,它工作時,標籤庫前綴=「S」 URI =「/ Struts的標籤」共設置了再處理用戶輸入的是這樣的代碼:如何通過Struts獲得簡單的用戶輸入?

的Hello World,<s:property value="name"/>

它不在Struts2中工作。我在Struts2中包含哪些taglib?如何從welcomeStruts.jsp獲取用戶輸入以顯示在index.jsp中?謝謝。

welcomeStruts:

<%@page contentType="text/html"%> 
<%@page pageEncoding="UTF-8"%> 

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 

<html:html lang="true"> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title><bean:message key="welcome.title"/></title> 
     <html:base/> 
    </head> 
    <body style="background-color: white"> 

     <logic:notPresent name="org.apache.struts.action.MESSAGE" scope="application"> 
      <div style="color: red"> 
       ERROR: Application resources not loaded -- check servlet container 
       logs for error messages. 
      </div> 
     </logic:notPresent> 

     <h3><bean:message key="welcome.heading"/></h3> 
     <p><bean:message key="welcome.message"/></p> 

     <p>Hello! This is the test welcome page for a Struts Web MVC project.</p> 

     <h1>Hello World From Struts2</h1> 

     <form action="hello"> 
      <label for="name">Please enter your name</label><br/> 
      <input type="text" name="name"/> 
      <input type="submit" value="SUBMIT"/> 
     </form> 
    </body> 
</html:html> 

的index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8" %> 

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %> 
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %> 
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="f" %> 

<html> 
<body> 

    <H3>Welcome <html:text property="name"/>!</H3> 

</body> 
</html> 
+0

這是行不通的,因爲它是Struts 1代碼。從Struts1代碼遷移並不是一項簡單的任務,您必須重新編寫JSP。 Struts2標籤有不同的表示法。但是,JSTL和純html仍然有效。 – 2015-01-09 20:08:05

回答

0

的標籤庫是<%@ taglib prefix="s" uri="/struts-tags" %> 你必須寫這樣的事:

<s:form action="sendMsg"> 
<s:textfield name="msg" label="Insert message"/> 
<s:submit/> 

這是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" 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>Struts 2</display-name> 
    <welcome-file-list> 
     <welcome-file>index.jsp</welcome-file> 
    </welcome-file-list> 
    <filter> 
     <filter-name>struts2</filter-name> 
     <filter-class> 
     org.apache.struts2.dispatcher.FilterDispatcher 
     </filter-class> 
    </filter> 

    <filter-mapping> 
     <filter-name>struts2</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 
    <session-config> 
     <session-timeout> 
      600 
     </session-timeout> 
    </session-config> 
</web-app> 

這是struts.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
    "http://struts.apache.org/dtds/struts-2.0.dtd"> 
<struts> 
    <package name="model" extends="struts-default"> 
    <action name="sendMsg" class="model.Test" method="execute"> 
      <result name="success">/yourPage.jsp</result> 
     </action> 
    </package> 
</struts> 

這是你的類:

public class Test{ 
    private String msg; 
    public Test() { } 
    public String execute(){ 
     return "success"; 
    } 
    public String getMsg() { 
     return msg; 
    } 

    public void setMsg(String msg) { 
     this.msg = msg; 
    } 

} 

現在,在您的網頁添加此展現

<s:property value="msg"/> 

和Don」 t忘記爲struts2添加jar文件

+0

我想說的是,用Struts獲得簡單的用戶輸入並不簡單,但你的答案說得更好! – unigeek 2015-01-09 19:54:46

+2

'FilterDispatcher'已棄用。 – 2015-01-09 23:02:35

+0

究竟@AleksandrM使用該過濾器,而不是 struts2的 <濾波器級> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter Sourav301 2015-01-10 07:51:41