标签:
在添加环境之前,先在MyEclipse中建立一个web项目,且项目目录如下:
说明:项目分三层:domain层,service层,view层,src下放java代码,config下放框架配置文件,lib文件下放项目所需jar包。
1.首先搭建的是Struts2的环境:
版本:Struts2.3.16.3
所用到的jar包:
所用到的配置文件:
在web.xml中添加struts所需的过滤器:
1 <filter> 2 <filter-name>struts2</filter-name> 3 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 4 </filter> 5 6 <filter-mapping> 7 <filter-name>struts2</filter-name> 8 <url-pattern>/*</url-pattern> 9 </filter-mapping>
struts.xml:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <!-- 开发者模式 --> 10 <constant name="struts.devMode" value="true" /> 11 <!-- 扩展名为.action --> 12 <constant name="struts.action.extension" value="action"></constant> 13 <!-- 前台设为simple模式 --> 14 <constant name="struts.ui.theme" value="simple"></constant> 15 <package name="default" namespace="/" extends="struts-default"> 16 17 18 </package> 19 20 </struts>
log4j.properties:
# # Log4J Settings for log4j 1.2.x (via jakarta-commons-logging) # # The five logging levels used by Log are (in order): # # 1. DEBUG (the least serious) # 2. INFO # 3. WARN # 4. ERROR # 5. FATAL (the most serious) # Set root logger level to WARN and append to stdout log4j.rootLogger=INFO, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller‘s file name and line number. log4j.appender.stdout.layout.ConversionPattern=%d %5p (%c:%L) - %m%n # Print only messages of level ERROR or above in the package noModule. log4j.logger.noModule=FATAL log4j.logger.com.opensymphony.xwork2=DEBUG log4j.logger.org.apache.struts2=DEBUG
创建测试ActionTest.java:
1 package cn.clear.web.test; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 5 public class ActionTest extends ActionSupport{ 6 7 8 /** 9 * @fieldName: serialVersionUID 10 * @fieldType: long 11 * @Description: TODO 12 */ 13 14 private static final long serialVersionUID = 3248237427179914097L; 15 16 public String test() throws Exception { 17 18 19 return "success"; 20 } 21 }
创建测试页面test.jsp:
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>测试页面</title> 7 </head> 8 9 <body> 10 struts2环境添加成功! 11 </body> 12 </html>
在struts.xml中添加Action配置代码:
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" 4 "http://struts.apache.org/dtds/struts-2.3.dtd"> 5 6 <struts> 7 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <!-- 开发者模式 --> 10 <constant name="struts.devMode" value="true" /> 11 <!-- 扩展名为.action --> 12 <constant name="struts.action.extension" value="action"></constant> 13 <!-- 前台设为simple模式 --> 14 <constant name="struts.ui.theme" value="simple"></constant> 15 <package name="default" namespace="/" extends="struts-default"> 16 <!-- 测试action --> 17 <action name="test" class="cn.clear.web.test.ActionTest"> 18 <result name="success">test.jsp</result> 19 </action> 20 21 </package> 22 23 </struts>
启动服务器,在浏览器中输入:http://localhost:8080/SSH/test.action,如果成功转到以下页面:
标签:
原文地址:http://www.cnblogs.com/clear5/p/4353567.html