過程以下: html
1, 在eclipse新建一個java web 項目,名字爲shortrent。 java
2, 從struts2官網下載strtus2-all.zip包,並解壓。從 webapps中有struts2-blank項目的lib中拷入到shortrent項目的webContent的WEB-INF下的lib文件夾下。 web
3,修改web.xml 把相應的url映射到struts2去處理, apache
4, 在src源碼目錄下新建一個struts.xml的配置文件 app
5, 編寫helloword.jsp eclipse
6, 編寫index.jsp webapp
7, 編寫HelloWorld.java jsp
8, 運行如圖: ui
仍是添加源碼吧,方便複製 this
index.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>index.jsp</title> </head> <body> <center><h2>index.jsp</h2></center> <hr> <a href="HelloWorld.action">Hello World</a> </body> </html>
hellowrold.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"> <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Hello World.jsp</title> </head> <body> <h2><s:property value="message" /></h2> </body> </html>
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" id="WebApp_ID" version="3.1"> <display-name>shortrent</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> <!-- struts2配置 --> <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> <!-- 讓struts2響應 .do和 .action的url後輟 --> <url-pattern>*.do</url-pattern> <url-pattern>*.action</url-pattern> </filter-mapping> </web-app>
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> <!-- 在struts2配置文件添加對.do 和 .action後輟的響應 --> <constant name="struts.action.extension" value="do,action" /> <package name="struts2" extends="struts-default"> <action name="HelloWorld" class="com.shortrent.test.HelloWorld"> <result>/HelloWorld.jsp</result> </action> </package> </struts>
HelloWorld.java
package com.shortrent.test; import com.opensymphony.xwork2.ActionSupport; public class HelloWorld extends ActionSupport { public final static String MESSAGE = "Struts2 is up and running ..."; private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String execute() throws Exception { setMessage(MESSAGE); return SUCCESS; } }