Struts2 入門 hellow word

1.添加相應的jar 

  • commons-fileupload-x.y.z.jar

  • commons-io-x.y.z.jar

  • commons-lang-x.y.jar

  • commons-logging-x.y.z.jar

  • commons-logging-api-x.y.jar

  • freemarker-x.y.z.jar

  • javassist-.xy.z.GA

  • ognl-x.y.z.jar

  • struts2-core-x.y.z.jar

  • xwork-core.x.y.z.jar

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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>struts2</display-name>
	<filter>
		<filter-name>struts</filter-name>   //過濾器名稱
		<filter-class>    
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<welcome-file-list>
		<welcome-file>result.jsp</welcome-file>
	</welcome-file-list>
</web-app>
3.java項目下的.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="default" namespace="/" extends="struts-default">  //都爲默認的   namespac="/" 這邊爲顯示的爲最後面/ 例如http://localhost:8080/struts2/
  <action name="hellowrod"  class="com.action.helloword">  //這邊的name 爲訪問時的名稱
  <result>/result.jsp</result>// 返回的JSP頁面爲
  <result name="add">/add.jsp</result>
  </action>
</package>

</struts>
4.類文件編寫

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class hellowrod  extends ActionSupport{   //繼承ActionSupprot類


	@Override
	public String execute() throws Exception {
		System.out.println("hello  wrod");    //測試打印
		return SUCCESS;      //這邊默認反悔SUCCESS  也可以直接放回.XML文件裏面定義的name名字
	}
	
	public  String add(){
		return "add";
	}
	
	public  String updata(){
		return "updata";
	}
}
5.jsp頁面的編寫 result.jsp

<%@ 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>
   this is  result;
</body>
</html>
測試


通過hellowrod.action 訪問

 也可以不用action都可以訪問的到


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章