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>
贴下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>
相关文章推荐
- Eclipse Java注释模板设置详解
- SSM框架(多模块)——Spring+Mybatis
- 身份证号码验证算法
- java抓取网页源代码《转载》
- JAVA WordVec 的一个实现
- java计时器
- java 并发编程学习之三 ---- ReentrantLock
- 第二章 Spring MVC入门
- spring集成quartz的集群配置实例
- 将java对象(bean)转换成json
- eclipse上传显示svn上传者名
- 修改eclipse配置文件properties编码格式
- java遇到的问题及解答
- struts.xml 配置详解
- Java基础之异常(3)
- Java项目打war包的方法
- java多线程
- Struts2验证
- eclipse无法正常启动
- the type java.util.Map$Entry cannnot be resolved 解决办法