STRUTS2フレームワークレッスン1-HELLOSTRUTS2

STRUTS2フレームワークレッスン1-HELLOSTRUTS2

ステップを作成

1.Eclipse-new-Dynamic Webプロジェクト 選択肢がない場合

  1. Webモジュールの2.5バージョンを選択しない場合は、次の操作を行う必要があります。プロジェクト名を右クリックして、Java EEツール-デプロイメント記述子スタブを生成します。

3. jarパッケージをWebContentの下のlibに導入します ここに画像の説明を挿入

  1. 実行クラスHelloStruts2とメソッドsayHello()を書き込みます ここに画像の説明を挿入
package com.chyod.action;

public class HelloStruts2 {
		public String sayHello() {
		return "helloStruts2";
	}
}

5. struts.xmlを作成します。srcディレクトリに配置する必要があります。名前を間違えたり、変更したりすることはできません
。dtdルールを編集します。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
 <package name="" namespace="/" extends="struts-default" >
 
 <action name="hello" class="com.chyod.action.HelloStruts2" method="sayHello">
  <result name="helloStruts2">/HelloStruts2.jsp</result>
 </action>
 
 </package>
 
</struts>

  1. 単純な応答のjspファイルを書きます-HelloStruts2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello Struts2</title>
</head>
<body>
 <h1>Hello Struts2</h1>
 <h2>This is my first demo of Struts2</h2>
 <h3>The End!</h3>
</body>
</html>

  1. web.xmlでフィルターを構成する
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>hello_struts2</display-name>
<filter>
	<filter-name>myfilter</filter-name>
	<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>myfilter</filter-name>
	<url-pattern>*.action</url-pattern>  
</filter-mapping>
</web-app>

8.操作結果:tomcatで構成されたローカルサーバーを使用していることに注意してください。ポート番号は8888です。プロジェクト名:hello_struts2フィルタリングアクションは次のとおりです:*。アクション
ここに画像の説明を挿入
不十分な場合は、お知らせください!

ありがとう!

元の記事を22件公開 いいね3 ビジター3442

おすすめ

転載: blog.csdn.net/ChyoD1811/article/details/90111004