Strut2------配置环境

1.导入必要的包

Strut2------配置环境

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>
上一篇:C语言 数组 列优先 实现


下一篇:python代码中碰到的问题及解决