Struts2示例

来源:互联网 发布:des算法 vb 编辑:程序博客网 时间:2024/05/18 02:13
NameCollector.jsp
<%@ 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>Insert title here</title></head><body>  <s:form action="hello">   <s:textfield name="name" label="Your Name:"/>   <s:submit/>  </s:form></body></html>


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/strurs-2.3.dtd"><struts>  <package name="GreetingName" namespace="/Greeting" extends="struts-default">     <action name="Name">        <result>/jsp/NameCollector.jsp</result>     </action>          <action name="hello" class="mypack.Hello">        <result name="SUCCESS">/jsp/Greeting.jsp</result>     </action>  </package></struts>


Hello.java

package mypack;public class Hello {private final String hello = "Hello:";private String name;private String greeting;public String getName() {return name;}public void setName(String name) {this.name = name;}public String getGreeting() {return greeting;}public void setGreeting(String greeting) {this.greeting = greeting;}    public String execute(){setGreeting(hello+name);return "SUCCESS";}}

Greeting.jsp

<%@ 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>Greeting </title></head><body>  <h3>Greeting Page</h3>  <h4></h4><s:property value="greeting"/></h4></body></html>



web.xml

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" ><web-app>  <display-name>Archetype Created Web Application</display-name>  <filter>    <filter-name>Hello</filter-name>    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter    </filter-class>  </filter>  <filter-mapping>    <filter-name>Hello</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping></web-app>


0 0