1. 程式人生 > >第01講:1 struts2的介紹和helloworld實現

第01講:1 struts2的介紹和helloworld實現

struts2介紹:
主頁:http://struts.apache.org/ 在使用者請求和模組化處理方面以及頁面的展現這塊,Struts2 發揮的屌炸天作用; 相對於傳統的 Jsp+Servlet 模式,Struts2 更適合企業級團隊開發,方便系統的維護; 最新版本:2.3.16

1,新建專案HeadFirstStrust2chapter1,tomcat7,2.5,新增web.xml;匯入核心包(共8個,下載地址:連結:https://pan.baidu.com/s/19y5_AwxcePxVyDLMba-YAw 密碼:s9qr),
2,在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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>HeadFirstStruts2chapter01</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
        <filter-name>Struts2</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>Struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
3,在src下,新增struts2.xml配置檔案,寫package標籤,

<?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="suibiandingyi"
 extends="struts-default">
   
</package>
</struts>
4,建包www.cruise.action包,建類:HelloWorldAction,實現Action
package com.cruise.action;

import com.opensymphony.xwork2.Action;

public class HelloWorldAction implements Action{

    @Override
    public String execute() throws Exception {

       System.out.println("struts2的預設的方法執行了");
       return "success";
    }

}
5,在struts2中,繼續寫標籤<action>,
<?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="suibiandingyi" extends="struts-default">
    <action name="hello" class="com.cruise.action.HelloWorldAction">
       <result name="success">helloworld.jsp</result>
    </action>
</package>

</struts>
6,WebContent下寫helloworld.jsp,修改編碼格式為UTF-8
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
你好:歡迎來到Cruise技能學習網!
</body>
</html>