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

struts2复习2 helloworld

2016-06-27 10:01 429 查看
1 先建web项目

贴下lib包

2 写HelloWorldAction  类。

package mdd.study;

import com.opensymphony.xwork2.ActionSupport;

//ActionSupport是Action接口的实现类,所以继承ActionSupport和实现Action没什么差来着。

public class HelloWorldAction  extends  ActionSupport

//public class HelloWorldAction implements Action

{
private String message;

public String getMessage()
{
return message;
}

public String execute() throws Exception
{
message = "Hello World!";
return "success";
}

}

3 struts.xml

alt+/没得提示的话可以自行配置url,= =我自己都犯懒没配,想又提示的可以下个myeclipse或者百度一下配置方法。

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
<package name="HelloWorld" extends="struts-default" namespace="">
<action name="HelloWorld" class="mdd.study.HelloWorldAction">

            <result name="success">/HelloWorld.jsp</result>

        </action>
</package>

</struts>

4web.xml

我用的是2.3,有用2.0的请用早期的分发器

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee  http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" id="WebApp_ID">
<display-name>struts_study1</display-name>

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

<!-- org.apache.struts2.dispatcher.FilterDispatcher  早期的分发器 -->
<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>

</web-app>

5index.jsp

= =真心没什么写头,但我很实诚地都贴了

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%  

 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>

    

    

    <title>My JSP 'index.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>

  

  <body>

    <a href="<%=basePath %>HelloWorld.action">欢迎</a>

  </body>

</html>

6HelloWorld.jsp

<%@ page contentType="text/html;charset=UTF-8" %>

<%@ taglib prefix="s" uri="/struts-tags" %>

<html>
<head><title>欢迎页面</title></head>
<body>
<h2><s:property value="message" /></h2>
</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: