关闭

Struts2的简单配置

239人阅读 评论(0) 收藏 举报

首先我们得配置web.xml,话不多说,一切都在代码中

<?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" 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>SSH_E</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-list>
</web-app>

就下来我们写一个名为的Helloworld的action,看代码:

package com.struts.action;

import com.opensymphony.xwork2.ActionSupport;

public class Helloworld extends ActionSupport{

    /**
     * Helloworld必须继承自ActionSupport
     */
    private static final long serialVersionUID = 1L;
    @Override
    public String execute() throws Exception {
        System.out.println("execute action succeed");
        return "helloworld";
    }
}

就下来就可以配置Struts.xml文件了

<?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>
    <package name="default" namespace="/" extends="struts-default">  

          <default-action-ref name="index"></default-action-ref><!--默认显示的页面 -->
        <action name="index"><result >/error.jsp</result></action>
        <action name="helloworld" class="com.struts.action.Helloworld" method="execute">   
             <result name="helloworld">/welcome.jsp</result>       
        </action>     
    </package>
    <constant name="struts.i18n.encoding" value="utf-8" /><!-- 解决汉字的编码问题 -->
</struts>

最后我们提供相关的页面代码:
index.jsp的代码:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>

  <body>
  <h1>My Form</h1>
    <form action="helloworld.action" method="post">
        <P><input type="submit" name="submit" value="submit">
    </form>
  </body>
</html>

error.jsp的代码:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'error.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">

  </head>

  <body>
  <h1>My Form</h1>
    <h1>error</h1>
  </body>
</html>

好了,就这样,慢慢看。

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:33113次
    • 积分:703
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:1篇
    • 译文:16篇
    • 评论:8条
    与我相关
    最新评论