struts搭建基本配置
一、步骤
下载网址:http://struts.apache.org/
开发:struts-2.3.24-apps.zip
lib包:struts-2.3.24-lib.zip
源码包:struts-2.3.24-src.zip
1. 导入struts2 jar包.
struts2-core-2.3.24.jar ----struts2核心类库
commons-fileupload-1.3.1.jar ----支持文件上传类库
commons-io-2.2.jar
----IO操作类库
commons-lang-2.4.jar
----Apache类库
commons-lang3-3.2.jar ----Apache类库
commons-logging-1.1.3.jar ----Log4j支持类库
freemarker-2.3.22.jar ----Freemarker支持类库
ognl-3.0.6.jar ----Ognl表达式类库
xwork-core-2.3.24.jar ----Xwork核心类库
javassist-3.11.0.GA.jar ----必须导入包
2.在web.xml配置Filter过虑器.
FilterDispathcer (org.apache.struts2.dispatcher.FilterDispatcher)
在早期的Struts2开发中使用,从Struts 2.1.3开始,它已不推荐使用
如 果你使用的Struts的版本 >= 2.1.3,推荐升级到新的Filter-StrutsPrepareAndExecuteFilter
(org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter)。
3.配置struts.xml
在classpath创建struts.xml,classpath就是工程资源文件下,编译后就是tomcat WEB-INF/classes/下
从struts2-core-2.3.24.jar 复制struts-default.xml,去掉所有的信息,留头部与package
4.创建java-action控制类.
5.注入action name与method到struts.xml
6.创建jsp页面,调用控制类。
二、代码区。
1.web.xml
<?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>
<!-- 设置默认字符集编码为UTF-8 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 设置开发模式为true,打印出更详细的错误信息,默认值为false -->
<constant name="struts.devMode" value="true"></constant>
<!-- 当 struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false-->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 定义一个struts包名为strutsALL,并继承 struts-default-->
<package name="strutsAll" extends="struts-default" >
<action name="login" class="com.cn.action.LoginAction">
<result name="success">/longinSeccess.jsp</result>
</action>
</package>
</struts>
2.strus.xml
<?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>
<!-- 设置默认字符集编码为UTF-8 -->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!-- 设置开发模式为true,打印出更详细的错误信息,默认值为false -->
<constant name="struts.devMode" value="true"></constant>
<!-- 当 struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false-->
<constant name="struts.configuration.xml.reload" value="true"></constant>
<!-- 定义一个struts包名为strutsALL,并继承 struts-default-->
<package name="strutsAll" extends="struts-default" >
<action name="login" class="com.cn.action.LoginAction">
<result name="success">/longinSeccess.jsp</result>
</action>
</package>
</struts>
3.java-action类
package com.cn.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String uname;//用户
private String pwd;//密码
/**
* 这是struts默认的方法
*/
public String execute(){
System.out.println(uname+"======"+pwd);
return SUCCESS;
}
/***********************GET-SET************************/
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
4..index.jsp登陆页面
<html>
<head>
<title>登陆页面</title>
<style type="text/css">
*{padding:0px;margin:0px;}
.input{width:150px;}
fieldset{width:600px;padding:10px;text-align: center;}
</style>
</head>
<body>
<div align="center">
<fieldset>
<legend>登陆</legend>
<form action="login.action" method="post">
<input class="input" type="text" name="uname" /><br><br>
<input class="input" type="password" name="pwd" /><br><br>
<input type="submit" value="提交" />
</form>
</fieldset>
</div>
</body>
</html>
5.登陆成功页面
<html>
<head>
<title>登陆成功</title>
</head>
<body>
<h1>登陆成功页面</h1>
</body>
</html>