使用springMVC注解方式 怎么都调不进action。。又卡死配置文件这了,。,。
首先 applicationContext.xml
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<!-- 自动扫描dao和service包(自动注入) -->
<context:component-scan base-package="com.syyx.officemanage" />
<!-- *******************************动态代理事务*********************************** -->
<!-- 配置事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 注解方式事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--SpringMvc启用注解映射的支持 -->
<context:annotation-config />
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<!-- 把逻辑视图生成真实的视图 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 静态文件的处理如 -->
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/*.html" mapping="/*.html" />
<!-- 初始化ioc容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 解决延迟加载的问题 添加拦截器在 session用完后才关闭 -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- end -->
<!-- 自定义拦截器 -->
<!-- <listener> -->
<!-- <listener-class>com.syyx.officemanage.common.utils.ContextLoaderListener</listener-class> -->
<!-- </listener> -->
<!-- springMVC配置 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- /springMVC -->
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<!-- 自定义过滤器 -->
<filter>
<filter-name>MyFilter</filter-name>
<filter-class>com.syyx.officemanage.common.utils.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>MyFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<!-- 上面是 过滤类型 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
最后是 action
@Controller("userAction")
@RequestMapping(value={"/user"})
public class UserAction{
@Resource
private UserService userService;
protected Logger logger = LoggerFactory.getLogger(getClass());
public UserAction(){
System.out.println("UserAction()==============>");
//ApplicationContext context =new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//userService =(UserService)context.getBean("userService");
}
@InitBinder
public void initBinder(ServletRequestDataBinder binder) {
/**
*
* 自动转换日期类型的字段格式
*/
System.out.println("自动转换日期类型的字段格式=======>" );
binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
}
/**
* 验证验证码
* HttpSession session = request.getSession();
String certCode=request.getParameter("certCode");
if(certCode.equals((String)session.getAttribute("certCode")))
out.write("true");
else
out.write("false");
*/
@RequestMapping(value={"/login"},method=RequestMethod.POST)
public String login(HttpServletRequest request,SysEmployee employee){
try {
HttpSession session = request.getSession();
employee = userService.find(employee);
if(employee!=null){
session.setAttribute("user",employee);
return "index";
}
session.setAttribute("hint","用户名或密码错误!");
} catch (BeansException e) {
System.out.println("出现异常!");
throw new RuntimeException(e);
}
return "login";
}