首页 > 代码库 > Struts2入门

Struts2入门

结构:

技术分享

 

HelloWorldAction.java

package com.struts2.test;

public class HelloWorldAction {
       private String name;

       public String execute() throws Exception {
          return "success";
       }
       
       public String getName() {
          return name;
       }

       public void setName(String name) {
          this.name = name;
       }
    }

web.xml提供任何Web应用程序的入口点:

<display-name>Struts 2</display-name>
   <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>//不写,默认为index.jsp
   </welcome-file-list>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name></display-name>

    <!-- Struts2配置 -->
    <filter>
        <filter-name>struts2</filter-name>
        <!-- Filter的实现类 和struts2.5以前的可能有所不同 -->
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

</web-app>  

index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ 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=UTF-8">
<title>Hello World</title>
</head>
<body>
     <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="Say Hello"/>
   </form>
</body>
</html>     

helloWorld.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ taglib prefix="s" uri="/struts-tags"%>
   <!--  taglib指令告诉Servlet容器中,这个页面将使用Struts2标签,这些标签之前,
    将通过s:property标签中显示的动作类的属性名称>HelloWorldAction类的getName()方法返回的值。 -->
<!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=UTF-8">
<title>Hello World</title>
</head>
<body>
    Hello World, <s:property value="name"/>
</body>
</html>

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>
    <package name="struts2" namespace="/" extends="struts-default">
        <!-- <action name="index">
            <result >/index.jsp</result>
         </action> -->
        <action name="hello" class="com.struts2.test.HelloWorldAction">
            <result>/helloWorld.jsp</result>
        </action>
    
<-- more actions can be listed here -->
    </package>
    <-- more packages can be listed here -->
</struts>  
插播一条:struts.xml可以包含多个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>
     <include file="struts1.xml"/>
     <include file="struts2.xml"/>
     <include file="struts3.xml"/>
</struts>

 

logging.properties:

org.apache.catalina.core.ContainerBase.[Catalina].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].handlers = \java.util.logging.ConsoleHandler

浏览器访问http://localhost:8080/Struts2/

技术分享

点击say hello 跳转:

技术分享

 

Struts2入门