前几天看了一本JSP-Struts2相关的书,按照步骤来还是出现了几个问题
二月 16, 2017 12:33:07 上午 org.apache.catalina.core.AprLifecycleListener init 信息: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin 二月 16, 2017 12:33:07 上午 org.apache.coyote.http11.Http11Protocol init 信息: Initializing Coyote HTTP/1.1 on http-8080 二月 16, 2017 12:33:07 上午 org.apache.catalina.startup.Catalina load 信息: Initialization processed in 352 ms 二月 16, 2017 12:33:07 上午 org.apache.catalina.core.StandardService start 信息: Starting service Catalina 二月 16, 2017 12:33:07 上午 org.apache.catalina.core.StandardEngine start 信息: Starting Servlet Engine: Apache Tomcat/6.0.16 二月 16, 2017 12:33:09 上午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info 信息: Parsing configuration file [struts-default.xml] 二月 16, 2017 12:33:09 上午 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error 严重: Dispatcher initialization failed Unable to load configuration. - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.3.31.jar!/struts-default.xml:65:72 at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418) at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4356) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:924) at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:887) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053) at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at org.apache.catalina.core.StandardService.start(StandardService.java:516) at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at org.apache.catalina.startup.Catalina.start(Catalina.java:578) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) Caused by: Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.3.31.jar!/struts-default.xml:65:72 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:221) at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:101) at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:169) at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55) ... 30 more Caused by: Bean type class com.opensymphony.xwork2.ObjectFactory with the name struts has already been loaded by bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:30:124 - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.3.31.jar!/struts-default.xml:65:72 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:205) ... 33 more 二月 16, 2017 12:33:09 上午 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting filter struts2 Unable to load configuration. - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.3.31.jar!/struts-default.xml:65:72 at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:431) at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:190) at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275) at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397) at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108) at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709) at org.apache.catalina.core.StandardContext.start(StandardContext.java:4356) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:924) at org.apache.catalina.startup.HostConfig.deployDirectories(HostConfig.java:887) at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:492) at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1147) at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:311) at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053) at org.apache.catalina.core.StandardHost.start(StandardHost.java:719) at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045) at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443) at org.apache.catalina.core.StandardService.start(StandardService.java:516) at org.apache.catalina.core.StandardServer.start(StandardServer.java:710) at org.apache.catalina.startup.Catalina.start(Catalina.java:578) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288) at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413) Caused by: Unable to load configuration. - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.3.31.jar!/struts-default.xml:65:72 at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:58) at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:374) at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:418) ... 28 more Caused by: Unable to load bean: type: class:com.opensymphony.xwork2.ObjectFactory - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.3.31.jar!/struts-default.xml:65:72 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:221) at org.apache.struts2.config.StrutsXmlConfigurationProvider.register(StrutsXmlConfigurationProvider.java:101) at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reloadContainer(DefaultConfiguration.java:169) at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:55) ... 30 more Caused by: Bean type class com.opensymphony.xwork2.ObjectFactory with the name struts has already been loaded by bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.1.8.1.jar!/struts-default.xml:30:124 - bean - jar:file:/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/sshTest/WEB-INF/lib/struts2-core-2.3.31.jar!/struts-default.xml:65:72 at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.register(XmlConfigurationProvider.java:205) ... 33 more 二月 16, 2017 12:33:09 上午 org.apache.catalina.core.StandardContext start 严重: Error filterStart 二月 16, 2017 12:33:09 上午 org.apache.catalina.core.StandardContext start 严重: Context [/sshTest] startup failed due to previous errors 二月 16, 2017 12:33:10 上午 org.apache.coyote.http11.Http11Protocol start 信息: Starting Coyote HTTP/1.1 on http-8080 二月 16, 2017 12:33:10 上午 org.apache.jk.common.ChannelSocket init 信息: JK: ajp13 listening on /0.0.0.0:8009 二月 16, 2017 12:33:10 上午 org.apache.jk.server.JkMain start 信息: Jk running ID=0 time=0/46 config=null 二月 16, 2017 12:33:10 上午 org.apache.catalina.startup.Catalina start 信息: Server startup in 2658 ms
新手处理问题、老鸟或者大神可以忽略喔
问题1:Tomcat启动时出现的Error filterStart错误
出现这个问题,大家都是出找g女或者b仔、但是来来回回都是国人那些复杂的问题
我也是先按照网上的答案1,检查配置文件,反复检查若干遍之后,确认的确没有问题。
再按照网上的答案2,把“commons-fileupload-1.2.2.jar”和“commons-io-2.0.1.jar”包放到lib下,不过启动时还是出现相同的错误。
其实这需要去看报错的代码去分析!因为Error filterStart报错的原因实在太多了
1.xml配置失误
2.filter中某段代码未实例化
3.相应的包重复出现/相应的包包在部署是没有被包含/或者包对应的版本不对!(我就是出现struts2包和xwork包的版本不对)
4.类文件没有部署到tomcat下面,clean掉整个工程,重新编译,部署,启动tomcat.
问题2:关于Context []startup failed due to previous errors
可能出错的地方:
1.web.xml文件 web应用部署描述符,里面的部署的xml文件或者类,如果这些找不到就会发生startup failed due to previous errors错误。
2.如果在应用spring的话,在配置文件applicationContext.xml中定义的类、xml文件找不到也会报这个错误。
3.在web.xml,struts.xml,applicationContext.xml文件中自身有任何一点错误都可能引起上面的这个问题,而不仅仅是附带的文件错误导致。
4.如果使用ibatis的话,在SqlMapConfig.xml中定义的xml文件找不到也会报这个错误。(hibernate的配置在整合spring的时候使用spring的配置文件)
5.JDK的版本问题,最好使用JDK5.0 或者更高的版本。
6.Eclipse和tomcat的版本兼容问题
7.框架整合的过程中在导入到lib下的jar包冲突也可能产生该错误。
8.jar包的缺少以及jar包的版本也可产生该错误。(我的出现的问题)
9.其他的原因
所以为了解决Struts2 jar包的的重复或者出错。
还是直接用myeclipse配置的包使用 ->?myeclipse 如何配置struts2
自带已经包含了Struts2所需要的包
下面是一个简单利用Struts2实现用户登录的小程序
记得先把 这个 ->myeclipse 如何配置struts2?配置好才进行下面的
配置完成后在src中出现struts.xml的文件
然后需要建两个JSP页面:login.jsp 、loginResult.jsp,一个action类、
配置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> <!-- Starts配置名 --> <display-name>Starts info</display-name> <!-- Starts注册 --> <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>
Jsp页面代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'login.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>
<br/>
<br/>
<br/>
<div>
${requestScope.message}
<s:form action="Login" method="post">
<s:textfield name="adminUserName" label="用户名" />
<s:password name="adminUserPassword" size="21" label="密码"/>
<s:submit value="提 交"/>
</s:form>
</div>
</body>
</html>
由于文件中用到了strut的标签,所以需要在文件头部加上下面语句:
<%@taglib prefix="s" uri="/struts-tags"%>
而:
${requestScope.message}
是我使用了EL表达式,接收失败的报错信息显示出来
loginResult.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
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>通过</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>
${message}
</body>
</html>
这个文件是显示验证结果!成功到这个页面
Struts.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="example" namespace="" extends="struts-default"> <action name="Login" class="action.LoginAction"> <result>/loginResult.jsp</result> <result name="input">/login.jsp</result> </action> </package> </struts>
<struts></struts>之间是actionMapper相关的配置内容、一个WEB应用系统会有很多配置,所以用到了<package>来进行分类,这个相当于Java开发的包
最后是在loginAction.java
package action;
import com.opensymphony.xwork2.ActionSupport;
/**
* Struts2实现用户登录
* @author Administrator
*
*/
public class LoginAction extends ActionSupport{
// 执行用户验证的方法
public String execute() throws Exception{
if("admin".equals(adminUserName)&&"admin".equals(adminUserPassword)){
message=adminUserName+"登录成功!";
}else{
message=adminUserName+"登录失败!";
return INPUT;
}
return SUCCESS;
}
public String adminUserName;//用户名
public String adminUserPassword;//密码
public String message;//执行后返回的信息
public String getAdminUserName() {
return adminUserName;
}
public void setAdminUserName(String adminUserName) {
this.adminUserName = adminUserName;
}
public String getAdminUserPassword() {
return adminUserPassword;
}
public void setAdminUserPassword(String adminUserPassword) {
this.adminUserPassword = adminUserPassword;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
这个类有山歌属性:adminUserNmae、adminUserPassword、和message。
执行完成会返回execute()方法。
程序开发完毕后:

运行结果图:

成功图:

失败图:

写的很烂、也很长气、新手学习处理的问题。
测身材体型的应用
index.jsp页面
<%@ 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 'index.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">
-->
<style type="text/css">
div{font-size: 50px;
font-weight: bold;
color: #f55;
}
#t{
font-weight: bold;
color: #f55;
}
</style>
</head>
<body>
<center>
<s:div>炸鸡专用测试</s:div>
<s:form action="Body" method="post">
<s:textfield name="height" label="身高(cm)" id="t"/>
<s:textfield name="weight" label="体重(kg)" id="t" />
<s:submit value="提交"/>
</s:form>
</center>
</body>
</html>
normal.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
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 'normal.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>
<h1>正常!</h1>
</body>
</html>
thin.,jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
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 'normal.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>
<h1>你的体型偏瘦!</h1>
</body>
</html>
little_thin..jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
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 'normal.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>
<h1>你的体型:苗条!</h1>
</body>
</html>
little_fat.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
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 'normal.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>
<h1>你的体型:轻度肥胖!</h1>
</body>
</html>
fat.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%>
<%
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 'normal.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>
<h1>你的体型:重度肥胖!</h1>
</body>
</html>
BodyAction.java
package com.glj;
import com.opensymphony.xwork2.ActionSupport;
public class BodyAction extends ActionSupport {
private String height;
private String weight;
public String execute(){
double bodyHeight = Double.parseDouble(height);
double bodyWeight = Double.parseDouble(weight);
double standardBody = (bodyHeight-100)*0.9;
double body = bodyWeight/standardBody;
if(body<0.8){
return "thin";
}else if(body>=0.8&&body<0.9){
return "little_thin";
}else if(body>=0.9&&body<0.9){
return "normal";
}else if(body>=1.1&&body<=1.3){
return "little_fat";
}else if(body>1.3){
return "fat";
}else{
return "error";
}
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="bodyTest" namespace="" extends="struts-default"> <action name="Body" class="com.glj.BodyAction"> <result name="thin">/thin.jsp</result> <result name="little_fat">/little_fat.jsp</result> <result name="fat">/fat.jsp</result> <result name="little_thin">/little_thin.jsp</result> <result name="normal">/normal.jsp</result> <result name="error">/index.jsp</result> </action> </package> </struts>
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>
带有数据验证(Validation)
去掉strut2标签自动生成html中的table/tr/td代码,只需要在配置文件struts.xml中加入以下代码即可
<constant name="struts.ui.theme" value="simple"></constant>
package com.glj;
import com.opensymphony.xwork2.ActionSupport;
public class userAction extends ActionSupport{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void validate() {
// TODO Auto-generated method stub
super.validate();
if("".equals(this.getUsername())||null==this.getUsername()){
this.addFieldError("username", "对不起,用户名不能为空");
}
if("".equals(this.getPassword())||null==this.getPassword()){
this.addFieldError("password", "对不起,密码不能为空");
}
}
@Override
public String execute() throws Exception {
System.out.println("执行了userAction");
if ("admin".equals(this.getUsername())&&"admin".equals(this.getPassword())) {
return SUCCESS;
}
else{
return ERROR;
}
}
}
<%@ page language="java" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<form action="login.action" method="post" focus="login">
<h2>系统登录:</h2>
账号:
<input type="text" name="username">
${errors.username[0]}
<br />
密码:
<input type="password" name="password">
${errors.password[0]}
<br />
<input type="submit" value="提交">
</form>
</center>
</body>
</html>
用<s:actionerror>标签显示出全部错误信息/<s:actionmessage /> 提示信息
package com.glj;
import com.opensymphony.xwork2.ActionSupport;
public class userAction extends ActionSupport{
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public void validate() {
// TODO Auto-generated method stub
super.validate();
if("".equals(this.getUsername())||null==this.getUsername()){
this.addActionError( "对不起,用户名不能为空");
}
if("".equals(this.getPassword())||null==this.getPassword()){
this.addActionError("对不起,密码不能为空");
}
}
@Override
public String execute() throws Exception {
System.out.println("执行了userAction");
if ("admin".equals(this.getUsername())&&"admin".equals(this.getPassword())) {
return SUCCESS;
}
else{
return ERROR;
}
}
}
在jsp页面加入<s:actionerror />
<s:actionmessage /> 和
this.addActionError("对不起,密码不能为空");同理
一直是想骇进学校的U.R.P系统的,无奈。。。
用的是 resin。
厉害…