如何使用Struts获得简单的用户输入?

时间:2015-01-09 18:26:19

标签: java jsp struts2

我正在尝试使用NetBeans 7.4在Struts中获取用户输入。有两个.jsp文件,welcomeStruts.jsp用户输入文本,另一个文件index.jsp应显示此文本。问题是:我需要使用哪个标签库来处理用户输入?在Struts1中,当taglib prefix =“s”uri =“/ struts-tags”被设置然后像这段代码那样处理用户输入时它起作用了:

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>

1 个答案:

答案 0 :(得分:0)

taglib是<%@ 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"/>

并且不要忘记为struts2

添加jar文件