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

Struts2的简单配置

2015-11-20 19:47 393 查看
首先我们得配置web.xml,话不多说,一切都在代码中

[code]<?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,看代码:

[code]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文件了

[code]<?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的代码:

[code]<%@ 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的代码:

[code]<%@ 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>


好了,就这样,慢慢看。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: