解决struts2初学者配置问题
刚解决了一个struts环境配置的问题,我用的是netbeans6.1+tomcat6.0 好不容易才把他搞定了,不过搞定之后,我现在都不知道为什么会这样,真的有点莫名其妙
我看到网上有许多兄弟姐妹都遇到这样的问题,这个问题不解决,struts2就永远卡在哪里(我真的卡了一个星期,想看把我这破电脑摔了,火大)
1,我先用netbeans建一个jps页面 [login.jsp]
<%@ page 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>JSP Page</title>
</head>
<body>
<form action="login.action" method=post>
username<input type="text" name="username" /><br>
password<input type="password" name="password" /><br>
<input type="submit" value="submit" />
</form>
</body>
</html>
--------------------------------------------------------------------------------------------------
2,导入struts2库---不要全部导入,(全部导入真的怕出错),就导入最基本的哪五个
---------------------------------------------------------------------------------------------------
3,配置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">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<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>*.action</url-pattern>
</filter-mapping>
</web-app>
----------------------------------------------------------------------------------------
注意上面哪能个*.action 我开始是写成 /* 发生错误,打开index.jsp这个页面的时候就报错 我后来改成*.action就正确了
上面搞好了,你先打开浏览器看一下,看能不能正确的看到,index.jsp哪个页面,如果能正确的看到,应该配置正确了,如果不能看到,哪你检查有没有导入多余的struts2包,再在web.xml里面调整一下filter哪个过滤器放的位置,
-------------------------------------------------------------------------------------------------------------------
4,建一个名叫com的包,在com时面建一个action处理类 LoginAction.java
package com;
public class LoginAction {
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;
}
public String execute() throws Exception
{
if(this.getUsername().equals("hello") && this.getPassword().equals("world"))
return "success";
return "error";
}
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>
<package name="qs" extends="struts-default">
<action name="login" class="com.LoginAction">
<result name="error">/error.jsp</result>
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
注意 struts.xml 的位置
他是在 web-inf/classes/下面,netbeans项目-->源包--->中建立struts.xml
还有注意一个struts这个名字,有的教程上面都没struts.xml这个文件名搞错了
struts.xml DTD定义,最好不要直接去写,你下载struts压缩文件里面,有几个案例,里面有struts.xml源码,你最好直接复制过来
<action name="login" class="com.LoginAction" >这里面哪个name="login" login一定要跟表单提交action属性对应
我是一步一步,这样配置出来的,其中出现过n次错误,烦死了,有时候真的想放弃struts2,但还是坚持下来了,坚持就是胜利呀!!