1、右键工程文件夹;
2、在菜单中选择MyEclipse;
3、点击Add Struts Capabilities;
4、选择相应的Struts版本,推荐2.1,下一步
5、完成.
一、需要导入的包:commons-logging-1.0.4.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar
二、配置文件
1。web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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_4.xsd">
<display-name>Struts 2.0 Hello World</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2。struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="struts-default.xml"/>
<package extends="struts-default">
<action >
<result >/success.jsp</result>
<result >/error.jsp</result>
<result type="redirect">Login!error.action?key=${}</result>
</action>
</package>
</struts>
注:<result type="redirect">Login!error.action?key=${}</result>
其中type="redirect"指的是可以按url重定向,其中当然可以写入带参数的URL了。
key必须在action中定义变量,${}是传参数的格式,可以写入变量的名字也可不写。
3。struts.properties
struts.devMode=true
struts.enable.DynamicMethodInvocation=true //允许使用动态方法
struts.custom.i18n.resources=label
4。Login.java
package com.pac.struts2
import javax.servlet.http.HttpServletRequest
import org.apache.struts2.ServletActionContext
import com.opensymphony.xwork2.ActionSupport
public class Login extends ActionSupport{
private String message
private String userName
private String key
public String getMessage() {
return message
}
public void setMessage(String message) {
this.message = message
}
public String getUserName() {
return userName
}
public void setUserName(String userName) {
this.userName = userName
}
public String getKey() {
return key
}
public void setKey(String key) {
this.key = key
}
public String login(){
String message1 = " Hello, 请填写用户名!"
HttpServletRequest request = ServletActionContext.getRequest()
if(getUserName()==null || "".equals(getUserName())){
return LOGIN
}
message = getUserName()+"你好啊!!"
return SUCCESS
}
public String error() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest()
String key = request.getParameter("key")
if(key!=null &&!"".equals(key)){
setKey(key+" 你好啊!!")
}else{
setKey("mmm 你好啊!!")
}
message = getKey()
return SUCCESS
}
}
5。页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ 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 'success.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">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<font color="green">This is my success page.</font><br>
<s:property value="message"/>
</body>
</html>
为什么把struts2的配置文件,引入web.xml呢
<!-- 定义Struts2的FilterDispatcher的Filter --><filter>
<!-- 定义核心Filter的名字 -->
<filter-name>struts2</filter-name>
<!-- 定义核心Filter的实现类 -->
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<!-- FilterDispatcher用来初始化Struts2并且处理所有的Web请求 -->
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
这样写不就行了吗?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)