Struts2项目部署

发布时间:2014-10-23 23:23:52
来源:分享查询网

1、创建web项目 2、添加jar包 (jar包后面连同项目源码一同发布下载链接) 3、配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <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>*.htm</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>/WEB-INF/html/index.html</welcome-file> </welcome-file-list> </web-app> 4、编写请求Action public class HsunAction extends ActionSupport { @Override public String execute() throws Exception { out.println(msg); msg = "I am coming "; out.println(msg); return SUCCESS; } public String test() throws Exception{ msg = "hello test"; out.println("hello test"); return SUCCESS; } private String msg; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } 5、配置struts.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!--增加struts的扩展名--> <constant name="struts.action.extension" value="htm" /> <package name="hsun" namespace="" extends="struts-default"> <action name="hello" class="com.hsun.struts.action.HsunAction"> <result name="success">/WEB-INF/html/hello.jsp</result> </action> <action name="test" class="com.hsun.struts.action.HsunAction" method="test"> <result name="success">/WEB-INF/html/hello.jsp</result> </action> </package> </struts> 6、返回页面 hello.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Home</title> </head> <body> Hello World! <br> ${msg } </body> </html> OK!SUCCESS~~~ 项目整体结构如下 项目源码下载地址

返回顶部
查看电脑版