您的位置:首页 > 编程语言 > Java开发

误打误撞(二)用Struts的Action来控制jsp/servlet,又一入门。

2011-04-08 11:11 337 查看
误打误撞(二)用Struts的Action来控制jsp/servlet

接着误打误撞(一),我们撞到了Struts。

Struts是什么?
好像是一个MVC框架,详细去baidu一下就知道了。

我们接着(一)的TestWeb工程。

步骤:

一、没有Struts,就去下载一个最新版本的2.2.1.1。
解压后把下列文件拷贝到TestWeb/WebContent/WEB-INF/lib目录
commons-fileupload-X.X.X.jar
commons-io-X.X.X.jar
commons-logging-X.X.X.jar
commons-logging-api.X.X.jar
freemarker-X.X.X.jar
ognl-X.X.X.jar
struts2-core-X.X.X.X.jar
xwork-core-X.X.X.jar
javassist-3.7.ga.jar

二、拷贝日志组件log4j.jar到TestWeb/WebContent/WEB-INF/lib

三、log4j日志配置

在TestWeb/src/目录下新建log4j.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/>
</layout>
</appender>

<!-- specify the logging level for loggers from other libraries -->
<logger name="com.opensymphony">
<level value="DEBUG" />
</logger>

<logger name="org.apache.struts2">
<level value="DEBUG" />
</logger>

<!-- for all other loggers log only debug and above log messages -->
<root>
<priority value="INFO"/>
<appender-ref ref="STDOUT" />
</root>

</log4j:configuration>

四、struts配置

在TestWeb/src/目录下新建struts.xml文件
文件其中的<action>配置就是对jsp/servlet的映射。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<constant name="struts.devMode" value="true" />

<package name="basicstruts2" extends="struts-default">

<action name="index">
<result>/index.jsp</result>
</action>

<action name="HelloServlet">
<result>/HelloServlet</result>
</action>

<action name="TestServlet">
<result>/TestServlet</result>
</action>

</package>

</struts>

五、修改TestWeb/WebContent/WEB-INF/web.xml文件

添加filter以及filter-mapping

<?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>TestWeb</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>

<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>/*</url-pattern>
</filter-mapping>
</web-app>

至此,搞定,再去运行一下Run as。启动tomcat7.
就可以看到
http://localhost:8080/TestWeb/index
内容
也可以这样访问
http://localhost:8080/TestWeb/index.action
http://localhost:8080/TestWeb/TestServlet.action

六、部署到Tomcat7
把新增的log4j.xml、struts.xml拷贝到/tomcat/webapps/TestWeb/WEB-INF/classes/目录下
把web.xml拷贝到tomcat/webapps/TestWeb/WEB-INF覆盖原先的web.xml
拷贝lib中全部内容到tomcat/webapps/TestWeb/WEB-INF/lib覆盖原先的。

好了 ,关掉eclipse,重启tomcat7,访问
http://localhost:8080/TestWeb/index.action
http://localhost:8080/TestWeb/TestServlet.action
就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐