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

strutsdemo

2017-01-08 10:06 447 查看

1.开发前的准备

1.下载并部署tomcat

http://jingyan.baidu.com/article/2c8c281daa77aa0008252aff.html

2.为eclipse指定浏览器



3.设置jsp页面编码



4.新建一个dynamic Web Project项目firstWeb,在web-content下新建一个index.jsp页面



5.以上说明步骤完成,说明eclipse部署tomcat成功

6.下载structs2开发包,位struts开发做准备

http://download.csdn.net/detail/hjx2710/3298699

2.创建strutsdemo

**jsp页面报错参考

http://jingyan.baidu.com/article/7f766dafbb5cf34101e1d0d8.html

1.创建Dynamic Web project —StrutsDemo



2.导入struts需要的jar包



3.编写工程配置文件

下面是文件的总目录



新建工程配置文件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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_9" version="2.4">
<display-name>Struts Blank</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.jsp</welcome-file>
</welcome-file-list>
</web-app>


4.添加struts.properties配置文件,设置常量

struts.i18n.encoding=UTF-8


5.编写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>

<!-- 配置包 包名为default,该包继承了struts2框架的默认包struts-default -->
<package name="default" namespace="/" extends="struts-default">
<!-- 定义名为helo的action,该action的处理类为com.action.TestAction的处理类,并映射到success.jsp -->
<action name="hello" class="com.action.TestAction">
<result>/success.jsp</result>
</action>
</package>
</struts>


6.开发前端页面index.jsp和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>
<s:a action="hello">hello</s:a>
</body>
</html>


<%@ 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>

<s:property value="helo"/>

</body>
</html>


7.编写后台处理程序

package com.action;

import com.opensymphony.xwork2.ActionSupport;

public class TestAction extends ActionSupport{

private static final long serialVersionUID=1L;
private String helo;

public String getHelo() {
return helo;
}

public void setHelo(String helo) {
this.helo = helo;
}
@Override
public String execute() throws Exception {
helo="hello,world";
return SUCCESS;
}
}


8.测试工程



9.结果如下





10.案例下载地址

http://download.csdn.net/detail/myvanguard/9731809
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  strutsdemo