过程以下: 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; } }