Strut2------配置环境
Posted 玉天恒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Strut2------配置环境相关的知识,希望对你有一定的参考价值。
1.导入必要的包
2.在src目录下新建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> <!-- 修改后缀 --> <constant name="struts.action.extension" value="do,,"/> <!-- 注意:后面include进来的xml是无效的 --> <include file="servlet.xml"/> </struts>
3.在src目录下新建servlet.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> <package name="other" namespace="/" extends="struts-default"> <action name="servlet2" class="context.ServletActionDemo2"> <result name="success" type="redirect">/index.jsp</result> </action> </package> <package name="default" namespace="/" extends="struts-default"> <!-- 没有定义<result>的<action>使用这个 --> <global-results> <result name="none" type="dispatcher">/index.jsp</result> </global-results> <!-- 局部结果界面 --> <action name="servlet" class="context.ServletActionDemo"> <result name="success">/index.jsp</result> </action> <!-- <action name="servlet2" class="context.ServletActionDemo2"> </action> --> <action name="servlet3" class="context.ServletActionDemo3"> </action> <action name="servlet3" class="context.ServletActionDemo3"> <!-- type的默认值是dispatcher:请求转发,redirect:重定向,url地址显示的是界面地址 --> <!-- type="chain"时是action跳转action,而且跳转的action需要写在另一个package里面,如servlet2 --> <result name="none" type="chain">servlet2</result> </action>
<!-- 这里只测试了这个action,其余的没有测 --> <action name="login" class="context.LoginAction"> <result name="success" type="dispatcher">/index.jsp</result> </action> <action name="login2" class="context.LoginAction2"> <result name="success" type="redirect">/index.jsp</result> </action> </package> </struts>
4.配置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>MyFirst</display-name> <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> <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> </web-app>
5.在src目录下新建context包,里面新建类文件LoginAction.java(其他的没有一一列出来喔)
public class LoginAction extends ActionSupport{ @Override public String execute(){ //HttpServletRequest request = ServletActionContext.getRequest(); return SUCCESS; } }
6.新建index.jsp文件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>主界面</title> </head> <body> <a href="${pageContext.request.contextPath}/login">跳转login Servlet</a> <h1>${pageContext.request.contextPath}</h1> </body> </html>
以上是关于Strut2------配置环境的主要内容,如果未能解决你的问题,请参考以下文章
Strut2的配置文件strust.xml报错:Package Action extends undefined package struts-default
Strut2在Action-Result的配置文件内转到jsp页面时用URL传递参数