刚接触Jsp的struts不久又要学spring然后把两者结合起来用于是我在网上找了很多的有关struts+spring的例子但基本上都是a的例子然后根据它的原理重新编写一本书上用struts实现的例子
一运行环境
eclipseSDK
MyEclipse GA
apachetomcat
Strust
Spring
二该工程所需文件
indexjsp
loginjsp
reinput_blankjsp
reinput_errorjsp
successjsp
LoginActiojava
LoginFormjava
strutsconfigxml
applicationContextxml
三步骤
建工程
创建一个名为Welcome_SS的Web Project
File > New > Project > Web Project “next” Project Name”Welcome_SS” “finish”;
导入struts包
选择Welcome_SS工程右键>Myeclipse (或选菜单栏上Myeclipse) > Add struts Capabilities……选择struts然后finish;
导入spring包
选择Welcome_SS工程右键>Myeclipse (或选菜单栏上Myeclipse) > Add spring Capabilities……在默认基础上再选择Spring AOP Libraries <MyEclipseLibrary>和Spring Web Libraries <MyEclipseLibrary>然后finish;
(为什么要选这两个我也还没有弄清楚)
注可能还要在lib文件夹下加几个spring包
springjar
springjar
springaopjar
springbeansjar
springcontextjar
springcorejar
springormjar
springremotingjar
springsupportjar
springwebjar
springwebmvcjar
编写代码(页面及其所需的类)
Ø indexjsp
选择Welcome_SS工程下的WebRoot > New > JSP取名为indexjspFinish;
输入以下代码
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
<html>
<head>
<!<link rel="stylesheet" type="text/css" href="/stylescss">>
</head>
<body><div align="center">
Welcome here!<br><a href="loginjsp">sign in</a></div>
</body>
</html>
Ø loginjsp
选择Welcome_SS工程下的WebRoot > New > JSP取名为loginjspFinish;
输入以下代码
<%@ page contentType="text/html;charset=gb"%>
<%@ page import="javautil*javasql*javatext*javaio*"%>
<html>
<body>
<div align="center"><form name="form" method="post" action="logindo">
Welcome Here! <br><br>
username:<input type="text" name="username"><br>
password:<input type="text" name="password"><br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="submit" value="reset" onclick="deleteAllMessage()"><br>
</form></div>
</body>
</html>
Ø reinput_blankjsp
选择Welcome_SS工程下的WebRoot > New > JSP取名为reinput_blankjspFinish;
输入以下代码
<%@ page language="java" import="javautil*" pageEncoding="utf"%>
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
<html>
<body>
<div align="center"><form name="form" method="post" action="logindo"><font color="#ff">*用户名和密码都不能为空!</font><br><br>
username:<input type="text" name="username" > <br>
password:<input type="text" name="password" > <br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="submit" value="reset" onclick="deleteAllMessage()">
</form></div>
</body>
</html>
Ø reinput_errorjsp
选择Welcome_SS工程下的WebRoot > New > JSP取名为reinput_errorjspFinish;
输入以下代码
<%@ page language="java" import="javautil*" pageEncoding="utf"%>
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
<html>
<body>
<div align="center"><form name="form" method="post" action="logindo"><font color="#ff">*用户名或密码不正确!</font><br><br>
username:<input type="text" name="username" > <br>
password:<input type="text" name="password" > <br>
<input type="submit" name="submit" value="submit">
<input type="reset" name="submit" value="reset" onclick="deleteAllMessage()">
</form></div>
</body>
</html>
Ø successjsp
选择Welcome_SS工程下的WebRoot > New > JSP取名为successjspFinish;
输入以下代码
<%@ page language="java" pageEncoding="utf"%>
<!DOCTYPE HTML PUBLIC "//WC//DTD HTML Transitional//EN">
<html>
<head>
<!<link rel="stylesheet" type="text/css" href="/stylescss">>
</head>
<body>
<%=requestgetAttribute("message")%>!<br>
<a href="loginjsp">sign in</a><br>
<a href="indexjsp">sign out</a>
</body>
</html>
Ø LoginActionjsp
选择Welcome_SS工程下的src > com > yourcompany > struts下New>files :action再在这个文件夹下New>Java取名为LoginActionjavaFinish;
输入以下代码
package comyourcompanystrutsaction;
import javautilLocale;
import javaxservlet
import javaxservlet
import javaxservlet
import orgapachestrutsactionAction;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionForward;
import orgapachestrutsactionActionMapping;
import orgapachestrutsutilMessageResources;
import comyourcompanystrutsformLoginForm;
public class LoginAction extends Action {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
thismessage = message;
}
public ActionForward execute(ActionMapping mapping
ActionForm form
HttpServletRequest request
HttpServletResponse response)
LoginForm userform = (LoginForm) form;
if(userformgetUsername()equals("")||(userformgetPassword()equals(""))){
return mappingfindForward("reinput_blank");
}
else if(userformgetUsername()equals("success")&&(userformgetPassword()equals("success"))){
requestsetAttribute("message"getMessage()+userformgetUsername());
return mappingfindForward("success");
}
else{
return mappingfindForward("reinput_error");
}
}
}
Ø LoginFormjsp
选择Welcome_SS工程下的src > com > yourcompany > struts下New>files :form再在这个文件夹下New>Java取名为LoginActionjavaFinish;
输入以下代码
package comyourcompanystrutsform;
import javaxservlet
import orgapachestrutsactionActionErrors;
import orgapachestrutsactionActionForm;
import orgapachestrutsactionActionMapping;
public class LoginForm extends ActionForm {
/** username property */
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
}
根据前一步的代码进行配置文件的编写(strutsconfigxml和applicationContextxml)
选择Welcome_SS工程下的WebRoot>WEBINF>strutsconfigxml
输入下面代码
<?xmlversion=""encoding="UTF"?>
<!DOCTYPEstrutsconfigPUBLIC"//ApacheSoftwareFoundation//DTDStrutsConfiguration//EN""
<strutsconfig>
<datasources/>
<formbeans>
<formbeanname="loginForm"type="comyourcompanystrutsformLoginForm"/>
</formbeans>
<globalexceptions/>
<globalforwards/>
<actionmappings>
<action
attribute="loginForm"
input="/loginjsp"
name="loginForm"
path="/login"
scope="request"
type="orgspringframeworkwebstrutsDelegatingActionProxy">
<forwardname="success"path="/successjsp"/>
<forwardname="reinput_blank"path="/reinput_blankjsp"/>
<forwardname="reinput_error"path="/reinput_errorjsp"/>
</action>
</actionmappings>
<messageresourcesparameter="comyourcompanystrutsApplicationResources"/>
<pluginclassName="orgspringframeworkwebstrutsContextLoaderPlugIn">
<setpropertyproperty="contextConfigLocation"value="/WEBINF/classes/applicationContextxml"/>
</plugin>
</strutsconfig>
选择Welcome_SS工程下的src>applicationContextxml
输入下面代码
<?xmlversion=""encoding="UTF"?>
<!DOCTYPEbeansPUBLIC"//SPRING//DTDBEAN//EN""
<beans>
<beanname="/login"class="comyourcompanystrutsactionLoginAction">
<propertyname="message">
<value>Welcome_</value>
</property>
</bean>
</beans>
WebService(这里即Tomcat)布署及启动
(全面的可以参照我的另一篇随笔Myeclipse中Tomcat的两种布署方式)
a在apachetomcat"conf下找到serverxml打开在</Host>前面添加一句
<Contextpath="/wess"debug=""docBase="C:DocumentsandSettingsttworkspaceWelcome_SSWebRoot"reloadable="true"></Context>
注C:DocumentsandSettingsttworkspaceWelcome_SS是该工程的存储路径
b在Myeclipse中的工具栏上找到一个像机箱似形状的按钮点击则打开服务器Tomcat
运行
在IE浏览器里输入http://localhost:/wess即可运行!成功登陆的条件是用户名和密码都为success的时候