配置struts1项目

1. 打开IDE(如Eclipse),新建一个Web Project(在Eclipse里是Dynamic Web Project)。

2. 把下面的jar包放到WebContent/WEB-INF/lib目录中。因为安全原因,tomcat默认会从该目录加载项目的jar资源:

antlr.jar
commons-beanutils.jar
commons-digester.jar
commons-fileupload.jar
commons-logging.jar
commons-validator.jar
jakarta-oro.jar
jstl.jar
standard.jar
struts.jar

3. 因为struts1是通过servlet启动的,所以在web.xml加入下面的内容:
	<servlet>
		<servlet-name>struts</servlet-name>
		<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
		<init-param>
			<param-name>config</param-name>
			<param-value>/WEB-INF/struts-config.xml</param-value>
		</init-param>
		<init-param>
			<param-name>debug</param-name>
			<param-value>2</param-value>
		</init-param>
		<init-param>
			<param-name>detail</param-name>
			<param-value>2</param-value>
		</init-param>
		<load-on-startup>2</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>struts</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>struts</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

在WebContent目录下创建index.jsp,以方便后面的测试。

4. 在WebContent/WEB-INF目录下创建struts-config.xml:
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">

<struts-config>
	<form-beans />
	<global-forwards />
	<action-mappings />
</struts-config>


5. 在web server上运行,查看控制台是否显示正常启动,并打开浏览器访问http://localhost:8080/encryption (端口号在web server里面配置)。

6. 新建一个Action:

package com.john.encryption.web.struts.action;

public class ListAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("success");
	}
}


7. 在struts-config.xml加入action的配置:
	<action-mappings>
		<action path="/list" type="com.john.encryption.web.struts.action.ListAction">
			<forward name="success" path="/WEB-INF/pages/cipher/list.jsp"></forward>
		</action>
	</action-mappings>

在/WEB-INF/pages/cipher下新建list.jsp。

8. 浏览器输入http://localhost:8080/encryption/list.do,看是否正确跳转到list.jsp页面。

你可能感兴趣的:(java,Web,server,struts1)