今天学习SS的整合时,碰到不少麻烦,要么是配置文件的位置不对,要么是导入的jar包冲突,要么是把配置文件的DTD写错。总之,费了九牛二虎之力才把这两个东西整合在一起。所以,有必要在这里把它记录下来,以备后用。
(1)各文件的放置位置,以及导入的jar包,如下图
(2)各文件源代码:
web.xml
< 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 > login.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 >
<!-- 根据默认配置文件来初始化Spring容器 -->
< listener >
< listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
</ listener >
</ web-app >
<! DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
"http://struts.apache.org/dtds/struts-2.1.dtd" >
< struts >
<!-- 设置全局编码格式 -->
< constant name ="struts.i18n.encoding" value ="UTF-8" ></ constant >
< package name ="jp" extends ="struts-default" >
< action name ="loginAction" class ="loginActionSpring" >
< result name ="success" > /welcome.jsp </ result >
< result name ="error" > /error.jsp </ result >
</ action >
< action name ="" >
< result > . </ result >
</ action >
</ package >
</ struts >
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<!-- Spring配置文件的根元素 -->
< beans >
<!-- 部署一个业务逻辑组件 -->
< bean id ="myService" class ="com.jp.model.MyServiceImpl" >
</ bean >
<!-- 配置Struts2控制器对应的Bean实例 -->
< bean id ="loginActionSpring" class ="com.jp.action.LoginAction" >
<!-- 依赖注入业务逻辑组件 -->
< property name ="ims" ref ="myService" ></ property >
</ bean >
</ beans >
import com.jp.model.IMyService;
import com.opensymphony.xwork2.Action;
public class LoginAction implements Action {
private String username;
private String password;
private String tip;
private IMyService ims;
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 getTip() {
return tip;
}
public void setTip(String tip) {
this .tip = tip;
}
public void setIms(IMyService ims) {
this .ims = ims;
}
public String execute() throws Exception{
if (ims.valid(getUsername(), getPassword())){
setTip( " 哈哈,整合成功! " );
return SUCCESS;
}
else
return ERROR;
}
}
public interface IMyService {
boolean valid(String username,String password);
}
MyServiceImpl.java
public class MyServiceImpl implements IMyService {
public boolean valid(String username, String password) {
// TODO Auto-generated method stub
if (username.equals( " alex " ) && password.equals( " jp " ))
return true ;
else
return false ;
}
}
login.jsp
这个页面很简单,就不贴出全部源代码了,关键部分如下:
<%@taglib prefix="s" uri="/struts-tags" %>
<body>
<h3>用户登录</h3><br/>
<s:form action="loginAction">
<s:textfield name="username" label="用户名"></s:textfield>
<s:password name="password" label="密码"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
</body>
welcome.jsp关键代码如下:
<%@taglib prefix="s" uri="/struts-tags"%>
<body>
<s:property value="tip" />
<br/>
欢迎你,
<s:property value="username" />
<br />
你的密码是:
<s:property value="password" />
</body>
运行结果如下: