spring集成struts的HelloWorld

本文介绍了一个使用Struts2与Spring框架整合的例子,包括登录页面的创建、配置文件的设置、Action及Service层的实现等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先把spring和struts的jar包加进去,注意看jar包有没有冲突,否则tomcat可能启不来,在做本例的过程中,借鉴了一些网友的代码,在此表示感谢,本人开通这个博客,是用来做笔记,有不当之处,请踊跃提意见。

第一步:创建登陆页面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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登陆</title>
</head>
<body>
<s:form action="Login" method="post">
<s:textfield name="userName" label="userName"></s:textfield>
<s:password name="password" label="password"></s:password>
<s:submit label="submit"></s:submit>
</s:form>
</body>
</html>


第二步:创建返回页面result.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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
您的用户名是:<s:property value="userName"/><br/>
您的密码是:<s:property value="password"/>
</body>
</html>


第三步:配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext*.xml</param-value>
</context-param>

<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>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>


第四步:配置struts.xml文件

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<package name="struts2" extends="struts-default">
<action name="Login" class="loginAction">
<result name="success">/s2/result.jsp</result>
<result name="input">/s2/login.jsp</result>
</action>
</package>

</struts>


第五步:配置application-hello.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<import resource="applicationContext-jdbc.xml"/>

<bean id="loginService" class="cleversoft.struts2.s2s2.LoginServiceImpl"></bean>
<bean id="loginAction" class="cleversoft.struts2.s2s2.LoginAction" scope="prototype">
<property name="loginService" ref="loginService"></property>
</bean>

</beans>


第六步:创建action文件LoginAction.java

package cleversoft.struts2.s2s2;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class LoginAction extends ActionSupport {
private LoginService loginService;
private String userName;
private String password;
public void setLoginService(LoginService loginService) {
this.loginService = loginService;
}
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 String execute() throws Exception {
if(loginService.isLogin(userName, password))
return SUCCESS;
else
return INPUT;
}
}


第七步:创建service接口类LoginService.java

package cleversoft.struts2.s2s2;

public interface LoginService {
boolean isLogin(String userName, String password);
}


第八步:创建service实现类LoginServiceImpl.java

package cleversoft.struts2.s2s2;

public class LoginServiceImpl implements LoginService {

public boolean isLogin(String userName, String password) {
if("marry".equals(userName) && "123".equals(password))
return true;
else
return false;
}
}


第九步:运行tomcat,输入marry和123,返回如下页面表示成功

您的用户名是:marry
您的密码是:123
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值