struts 2学习笔记——hello示例

开发环境:eclipse OXYGEN+struts-2.5.14.1+apache-tomcat-7.0.84

编译器:1.8

参照书上的描述,创建Struts工程StrutsDemo,总是出错,返回404。


后参照网上的解决方案,最后发现根本问题有两个:

(1)filter配置。书上用的struts版本较老,和新版本filter位置不一致。应去掉路径中的ng.改为:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
 </filter>

(2)jar包引入。一开始图省事,将struts/lib下所有jar包直接引入。按照网上说法,这会导致包冲突。后按照书上罗列重新引入jar包,并按照运行错误提示,增加commons-lang3-3.6和log4j-api-2.9.1两个包。


之后运行成功。


web.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xmlns:javaee="http://java.sun.com/xml/ns/javaee"
	xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  <display-name>StrutsDemo</display-name>
	 <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

   
	<welcome-file-list>
		<!--开始页面-->
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

struts.properties配置如下:

<struts.i18n.encoding value="UTF-8"/>	<!--set the constant-->

struts.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<!-- 指定struts.xml文件的根元素 -->
<struts>
	<!-- 配置包,包名为default,该包继承了struts 2框架的默认包struts-default -->
	<package name="default" namespace="/" extends="struts-default">
	<!-- 定义名为hello的Action,该Action的处理类为com.action.TestAction,并映射到success.jsp页面 -->
		<action name="hello" class="com.action.TestAction">
			<result>/success.jsp</result>
		</action>
	</package>
</struts>

TestAction:

package com.action;
import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{
	private static final long serialVersionUID=1L;
	//Action属性
	private String helo;
	public String getHelo() {
		return helo;
	}
	public void setHelo(String helo) {
		this.helo = helo;
	}
	//重载execute()方法
	public String execute() throws Exception{
		helo="hello world";
		return SUCCESS;
	}
}

index.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- a标签 -->
<s:a action="hello">hello</s:a>

</body>
</html>

success.jsp代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
	<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 输出helo值 -->
	<s:property value="helo"/>
</body>
</html>
目前欠缺还很大,加油吧


发布了4 篇原创文章 · 获赞 2 · 访问量 1114

猜你喜欢

转载自blog.csdn.net/lvge2014/article/details/79542407