struts_spring_plugin
(1)UserDao中加入一个方法叫做loadById()
package com.zgy.registration.dao;
import java.util.List;
import com.zgy.registration.model.User;
public interface UserDao {
public void save(User u);
public boolean checkUserExistsWithName(String username);
public List<User> list();
public User loadById(int id);
}
(2)UserDaoimpl中实现loadById(int id)
package com.zgy.registration.daoImpl;
import java.util.List;
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.stereotype.Component;
import com.zgy.registration.dao.UserDao;
import com.zgy.registration.model.User;
import com.zgy.registration.util.HibernateUtil;
@Component("userDao")
public class UserDaoImpl implements UserDao {
private HibernateTemplate hibernateTemplate;
public void save(User u) {
hibernateTemplate.save(u);
}
public boolean checkUserExistsWithName(String username) {
List<User> user = (List<User>) hibernateTemplate.find("from User u where u.username=?", username);
if(user != null && user.size() > 0){
return true;
}
return false;
}
public HibernateTemplate getHibernateTemplate() {
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
@Override
public List<User> list() {
return (List<User>) this.hibernateTemplate.find("from User u");
}
@Override
public User loadById(int id) {
// TODO Auto-generated method stub
return this.hibernateTemplate.load(User.class, id);
}
}
(3)UserManager中加入一个loadById()方法
package com.zgy.registration.service;
import java.sql.SQLException;
import java.util.List;
import com.zgy.registration.model.User;
public interface UserManager {
public abstract boolean exists(User u) throws SQLException,
ClassNotFoundException;
public abstract void add(User u);
public abstract List<User> getUsers();
public abstract User loadById(int id);
}
(4)UserManagerImpl中实现loadById(int id)方法
package com.zgy.registration.serviceImpl;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;
import com.zgy.registration.dao.UserDao;
import com.zgy.registration.model.User;
import com.zgy.registration.service.UserManager;
@Component("userManager")
public class UserManagerImpl implements UserManager {
private UserDao userDao;
public UserDao getUserDao() {
return userDao;
}
@Resource
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
/* (non-Javadoc)
* @see com.zgy.registration.serviceImpl.UserManager#exists(com.zgy.registration.model.User)
*/
@Override
public boolean exists(User u) throws SQLException, ClassNotFoundException {
return userDao.checkUserExistsWithName(u.getUsername());
}
/* (non-Javadoc)
* @see com.zgy.registration.serviceImpl.UserManager#add(com.zgy.registration.model.User)
*/
@Override
public void add(User u) {
userDao.save(u);
}
@Override
public List<User> getUsers() {
// TODO Auto-generated method stub
return userDao.list();
}
@Override
public User loadById(int id) {
// TODO Auto-generated method stub
return this.userDao.loadById(id);
}
}
(5)UserAction中加入load()方法
package com.zgy.registration.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.zgy.registration.model.User;
import com.zgy.registration.service.UserManager;
import com.zgy.registration.vo.UserRegisterInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("user")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven {
/**
*
*/
private static final long serialVersionUID = 1L;
private UserRegisterInfo info = new UserRegisterInfo();
private UserManager um;
private List<User> users;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserRegisterInfo getInfo() {
return info;
}
public void setInfo(UserRegisterInfo info) {
this.info = info;
}
public UserAction(){
}
public UserManager getUm() {
return um;
}
@Resource(name="userManager")
public void setUm(UserManager um) {
this.um = um;
}
@Override
public String execute() throws Exception {
User u = new User();
u.setUsername(info.getUsername());
u.setPassword(info.getPassword());
if(um.exists(u)) {
return "fail";
}
um.add(u);
return "success";
}
@Override
public Object getModel(){
return info;
}
public String list(){
this.users = this.um.getUsers();
return "list";
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String load(){
this.user = this.um.loadById(info.getId());
return "load";
}
}
(6)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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="registration" extends="struts-default" namespace="/">
<action name="user" class="com.zgy.registration.action.UserAction">
<result name="success">/registerSuccess.jsp</result>
<result name="fail">/registerFail.jsp</result>
<result name="list">/userlist.jsp</result>
<result name="load">/user.jsp</result>
</action>
</package>
</struts>
(7)访问http://localhost:8080/32_Registration10/user!load.action?id=1
(8)报错:
Unable to instantiate Action, com.zgy.registration.action.UserAction, defined for ‘user‘ in namespace ‘/‘Failed to convert property value of type ‘com.zgy.registration.action.UserAction‘ to required type ‘com.zgy.registration.model.User‘ for property ‘user‘; nested exception is java.lang.IllegalStateException: Cannot convert value of type [com.zgy.registration.action.UserAction] to required type [com.zgy.registration.model.User] for property ‘user‘: no matching editors or conversion strategy found - action - file:/D:/study/tools/Tomcats/apache-tomcat-6.0.32/webapps/32_Registration10/WEB-INF/classes/struts.xml:9:70
(9)出错的原因是:
struts_spring_plugin中,有一个struts.objectFactory.spring.autoWire,默认值是name
struts中的Action 中的属性会自动根据名字查找对应的bean。就是测出的setUser()方法会自动找一个名叫user的bean,而此时user这个bean对应的是UserAction,所以报错。
(10)解决方法:
将UserAction的@Component改为u
UserAction中不需要注入userManager,由插件自动注入
package com.zgy.registration.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.zgy.registration.model.User;
import com.zgy.registration.service.UserManager;
import com.zgy.registration.vo.UserRegisterInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("u")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven {
/**
*
*/
private static final long serialVersionUID = 1L;
private UserRegisterInfo info = new UserRegisterInfo();
private UserManager userManager;
private List<User> users;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserRegisterInfo getInfo() {
return info;
}
public void setInfo(UserRegisterInfo info) {
this.info = info;
}
public UserAction(){
}
public UserManager getUserManager() {
return userManager;
}
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
@Override
public String execute() throws Exception {
User u = new User();
u.setUsername(info.getUsername());
u.setPassword(info.getPassword());
if(userManager.exists(u)) {
return "fail";
}
userManager.add(u);
return "success";
}
@Override
public Object getModel(){
return info;
}
public String list(){
this.users = this.userManager.getUsers();
return "list";
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String load(){
this.user = this.userManager.loadById(info.getId());
return "load";
}
}
(11)运行
org.apache.jasper.JasperException: could not initialize proxy - no Session - Class: org.hibernate.proxy.AbstractLazyInitializer
此时原来的报错已经解决了,以上报错是因为load()方法取出的数据,load()方法取得的是代理对象而产生的报错
(12)在使用了struts_spring_plugin后,Struts中的Action将有该插件产生,不再是由spring产生
(13)解决以上报错的方法:
a) 使用get()方法代替load()方法
b) 扩大session的范围
(14)扩大session的范围的方式是在web.xml中增加如下filter
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
(15)如果在spring的配置文件中,配置的取得SessionFactory的bean的名字不是sessionFactory 的话,上面的filter将会出错,因其默认的就是sessionFactory,此时可以在<filter>中添加参数来实现对应。
<init-param>
<param-name>sessionFactoryBeanName</param-name>
<param-value>sf</param-value>
</init-param>
(16)OpenSessionInViewFilter默认的事务是只读的,当没有配置spring的事务管理的时候使用了OpenSessionInViewFilter将会出错。当配置了事务后,OpenSessionInViewFilter会认为该事务的模式是read-write。
(17)配置Action中的class属性为u,在UserAction上写上@Component("u"),这样就可以在spring容器中查找对应的class
a) 修改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>
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="registration" extends="struts-default" namespace="/">
<action name="u" class="u">
<result name="success">/registerSuccess.jsp</result>
<result name="fail">/registerFail.jsp</result>
<result name="list">/userlist.jsp</result>
<result name="load">/user.jsp</result>
</action>
</package>
</struts>
b) 修改UserAction.java
package com.zgy.registration.action;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import com.zgy.registration.model.User;
import com.zgy.registration.service.UserManager;
import com.zgy.registration.vo.UserRegisterInfo;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
@Component("u")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven {
/**
*
*/
private static final long serialVersionUID = 1L;
private UserRegisterInfo info = new UserRegisterInfo();
private UserManager userManager;
private List<User> users;
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public UserRegisterInfo getInfo() {
return info;
}
public void setInfo(UserRegisterInfo info) {
this.info = info;
}
public UserAction(){
}
public UserManager getUserManager() {
return userManager;
}
@Resource
public void setUserManager(UserManager userManager) {
this.userManager = userManager;
}
@Override
public String execute() throws Exception {
User u = new User();
u.setUsername(info.getUsername());
u.setPassword(info.getPassword());
if(userManager.exists(u)) {
return "fail";
}
userManager.add(u);
return "success";
}
@Override
public Object getModel(){
return info;
}
public String list(){
this.users = this.userManager.getUsers();
return "list";
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public String load(){
this.user = this.userManager.loadById(info.getId());
return "load";
}
}
二十二、中文问题
(1)解决中文问题使用spring的filter
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
(2)使用struts的配置
<constant name="struts.i18n.encoding" value="UTF-8" />
(3)运行程序,从页面输入中文字符,数据库中存储的中文可以实现,没有出现乱码