【问题标题】:How do I get simple user input with Struts?如何使用 Struts 获得简单的用户输入?
【发布时间】:2015-03-08 03:29:54
【问题描述】:

我正在尝试使用 NetBeans 7.4 在 Struts 中获取用户输入。有两个 .jsp 文件,welcomeStruts.jsp 用于用户输入文本,另一个文件 index.jsp 用于显示该文本。问题是:我需要使用哪个标签库来处理用户输入?在 Struts1 中,它在设置 taglib prefix="s" uri="/struts-tags" 然后像下面这样处理用户输入时工作:

世界你好,<s:property value="name"/>

它在 Struts2 中不起作用。我在 Struts2 中包含什么标签库?如何从 welcomeStruts.jsp 获取用户输入以显示在 index.jsp 中?谢谢。

欢迎Struts:

<%@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>

【问题讨论】:

  • 这行不通,因为它是 Struts 1 代码。从 Struts1 代码迁移不是一项简单的任务,您必须重写 JSP。 Struts2 标签有不同的符号。但是,JSTL 和纯 html 仍然有效。

标签: java jsp struts2


【解决方案1】:

标签库是&lt;%@ taglib prefix="s" uri="/struts-tags" %&gt; 你必须这样写:

    <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"/>

别忘了为struts2添加jar文件

【讨论】:

  • 我想说的是,使用 Struts 获得简单的用户输入并不简单,但您的回答说得更好!
  • FilterDispatcher 已弃用。
  • 正好@AleksandrM 使用这个过滤器 struts2org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter过滤器类>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
  • 2021-01-28
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多