struts2-core.jar Struts2的核心;
xwork.jar 前面提到的xwork;
ognl.jar 用于页面输出的表达式语言;
freemarker.jar 用户界面标签模版;
commons-logging.jar Apache的公用包,用于输出日志。
struts2- 2.1.6 版本需要添加(加强了上传文件功能)
commons-fileupload-1.2.1.jar
struts2- 2.1.6 版本将datetimepicker一些移入 dojo 包内
struts2-dojo-plugin-2.1.6.jar
<%@ taglib prefix="sx" uri="/struts-dojo-tags" %>
<sx:head parseContent="true"/>
<sx:datetimepicker ></sx:datetimepicker>
<%@ taglib prefix="c" uri="/struts-tags" %>
引用标签,在file:///F:/5.51workspace/strutsTest/WebRoot/WEB-INF/lib/struts2-core-2.0.11.jar包中struts-2.0.dtd
<c:form action="login.action" method="post">
<c:textfield name="userName" id="userName" label="userName"></c:textfield>
<c:textfield name="userPwd" id="userPwd" label="userPwd"></c:textfield>
<c:submit value="commit"></c:submit>
</c:form>
---web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
---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>
<package name="strust2" extends="struts-default">
<action name="login" class="com.struts.action.LoginAction" method = "执行指定方法名,默认方法不执行">
<result name="success">/result.jsp</result> ---可以不写,默认成功页
<result name="input">/index.jsp</result> ---默认错误页
<result name="failer">/index.jsp</result> ---自定义返回字符串
</action>
</package>
</struts>
通配符表示方法
<struts>
<package name="strust2" extends="struts-default">
<action name="*"> --传入什么 x.action
<result>/{1}.jsp</result> -- 调用 x.jsp 页面
</action>
</package>
</struts>
--java action 类
package com.struts.action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String userName;
private String userPwd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String execute(){
if("abc".equals(userName.trim())){
return "success";
}else{
addFieldError("userName","userName is abc");
return "failer";
}
}
@Override
public void validate() {
// TODO Auto-generated method stub
if(null==userName||"".equals(userName.trim())){
this.addFieldError("userName", "userName is null");
}
if(null==userPwd||"".equals(userPwd.trim())){
this.addFieldError("userPwd", "userPwd is null");
}
}
}
action="login.action"的login对应struts.xml里的name :<action name="login" class="com.struts.action.LoginAction">
login 把 action 类 关联起来 com.struts.action.LoginAction
继承 ActionSupport 类 有验证的方法 public void validate() ,在里面重写验证条件,用 ActionSupport 类的 this.addFieldError("userPwd", "userPwd is null");方法加入错误信息,返回页面 userPwd为<c:textfield name="userPwd"
默认的返回错误name为 input
默认的成功字符串为 "success"
action 警告解决方法
警告: No configuration found for the specified action: 'ShowMessage' in namespace: ''. Form action defaulting to 'action' attribute's literal value.
<s:form action="user" namespace="/strust2">
对应
<package name="strust2" extends="struts-default">
找寻 action 命名空间:
因为开始使用的struts2标签(form)并未指定namespace属性。所以struts2会默认从根命名空间"/" 搜索action '/hello/ShowMessage.action',如搜索不到则进入默认命名空间''搜索action串,在默认命名空间中是肯定找不到自己定义的action的,所以,struts2抛出一个警告信息。
现在我们指定了namespace为/hello,则struts2会直接在/hello命名空间寻找。可以想象,如果在这么命名空间里找不到请求的action,也会抛出一条类似的信息。
注意,<s:form name="ShowMessage" method="post" action="ShowMessage" namespace="/hello" >
不能写成<s:form name="ShowMessage" method="post" action="ShowMessage.action" namespace="/hello" >
这样仍然有对应的警告信息,并且提交后出现无法访问的结果。因为没有ShowMessage.action这样一个action,这个.action不能由我们手工添加,Struts2会自动为你完成这个工作,并且手工添加是不行的,就不必多此一举了。但是在其他的场合,比如使用超级链结,则可以加上这个.action。
第一,在web.xml中添加配制
- <filter>
- <!-- 定义核心 Filter 名字 -->
- <filter-name>Struts 2</filter-name>
- <!-- 定义核心 Filter 的实现类 -->
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- </filter>
- <!-- FilterDispatcher 用来初始化 Struts 2 并且处理所有请求 -->
- <filter-mapping>
- <filter-name>Struts 2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
添加到web-app中
第二,添加struts2配制
- <?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>
- <!-- 指定全局国际化资源文件base名 -->
- <constant name="struts.custom.i18n.resources" value="messageResource"></constant>
- <!-- 指定国际化编码所使用的字符集 -->
- <constant name="struts.i18n.encoding" value="utf-8"></constant>
- <!-- 指定需要Struts2处理的请求后缀,默认值是action,也可配制多个,用","分开 -->
- <constant name="struts.action.extension" value="do"></constant>
- <!-- 指定是否允许在Struts2中使用表达式语法,默认是true -->
- <constant name="struts.tag.altSyntax" value="true"></constant>
- <!-- 指定是否使用开发模式,默认为false,设置true时,可在应用出错时显示更多更友好的出错信息,通常开发设置true,产品发布后设置false -->
- <constant name="struts.devMode" value="true"></constant>
- <!-- 指定是否每次HTTP请求时都重新加载资源文件,默认为false,设置true时,通常开发设置true,产品发布后设置false,但不知道为什么,设置一直都不成功 -->
- <constant name="struts.i18n.reload" value="true"></constant>
- <!-- 指定struts.xml文件改变后是否自动重新加载该文件,默认为false,设置true时,通常开发设置true,产品发布后设置false -->
- <constant name="struts.configuration.xml.reload" value="true"></constant>
- <!--
- 定义包,每个Action都必须定义在包内,且包与包可以进行继承,继承后父包中所有的Action
- name 包名,唯一标识附
- extends 继承,需要继承包的name属性,如无自己手动定义的包,哪么必须要继承struts-default包
- namespace 命名空间,访问时的命名空间名,以“/”开头
- -->
- <package name="semcolon" extends="struts-default" namespace="/login">
- <!-- 定义Action
- name Action名,同时也是Action访问路径的前缀
- class 实现Action的java类
- -->
- <action name="loginAction" class="semcolon.login.action.LoginAction">
- <!-- Action返回路径,如不设置name属性,将默认为success,路径为全路径 -->
- <result name="login">/WEB-INF/jsp/login/login.jsp</result>
- <result>/WEB-INF/jsp/comm/main.jsp</result>
- </action>
- </package>
- </struts>
第三,实现Action类
- package semcolon.login.action;
- import semcolon.bean.UserBean;
- import semcolon.comm.action.BaseAction;
- import semcolon.comm.constants.ExceptionInfo;
- public class LoginAction extends ActionSupport{
- // 用户信息Bean
- private UserBean user = null;
- public UserBean getUser() {
- return user;
- }
- public void setUser(UserBean user) {
- this.user = user;
- }
- // 定义处理用户请求的execuit方法
- public String execute() throws Exception {
- if (getUser().getUsername().equals("semcolon")
- && getUser().getPassword().equals("123")) {
- return SUCCESS;
- }
- return LOGIN;
- }
- }
package semcolon.login.action;
import semcolon.bean.UserBean;
import semcolon.comm.action.BaseAction;
import semcolon.comm.constants.ExceptionInfo;
public class LoginAction extends ActionSupport{
// 用户信息Bean
private UserBean user = null;
public UserBean getUser() {
return user;
}
public void setUser(UserBean user) {
this.user = user;
}
// 定义处理用户请求的execuit方法
public String execute() throws Exception {
if (getUser().getUsername().equals("semcolon")
&& getUser().getPassword().equals("123")) {
return SUCCESS;
}
return LOGIN;
}
}
继承ActionSupport类,且实现execute方法就可以了
最后为两个JSP页面
login.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>
- <title>login</title>
- </head>
- <body>
- <s:form action="loginAction.do" method="post" >
- <s:textfield name="user.username" label="姓名"/> <br>
- <s:textfield name="user.password" label="密码"/>
- <s:submit/>
- </s:form>
- </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>
<title>login</title>
</head>
<body>
<s:form action="loginAction.do" method="post" >
<s:textfield name="user.username" label="姓名"/> <br>
<s:textfield name="user.password" label="密码"/>
<s:submit/>
</s:form>
</body>
</html>