2 wtksana wtksana 于 2015.05.28 11:49 提问

想把用户名存在session中 在方法中加request参数后运行报错 求指教~

我用的是spring+hibernate~

修改后:

public class LoginAction extends SimpleFormController{
private Login login;
private String viewPage;
private String successPage;
//实现onSubmit()方法
public ModelAndView onSubmit(Object command,HttpServletRequest request) throws Exception {
//强制转换成User
User user = (User)command;
Map model = new HashMap();
//进行登录的业务逻辑处理
getLogin().login(user);
//存放消息
request.getSession().setAttribute("userName", user.getUsername());
model.put("user", user);
model.put("userId", user.getId());
model.put("msg", getLogin().getMsg());
if ("登录成功".equals(getLogin().getMsg())) {
//返回到登录成功的页面
return new ModelAndView(getSuccessPage(), model);
}
return new ModelAndView(getViewPage(), model);
}

修改前:

public class LoginAction extends SimpleFormController{
private Login login;
private String viewPage;
private String successPage;
//实现onSubmit()方法
public ModelAndView onSubmit(Object command) throws Exception {
    //强制转换成User
    User user = (User)command;
    Map model = new HashMap(); 
    //进行登录的业务逻辑处理
    getLogin().login(user);
    //存放消息
    model.put("user", user);
    model.put("userId", user.getId());
    model.put("msg", getLogin().getMsg());
    if ("登录成功".equals(getLogin().getMsg())) {
        //返回到登录成功的页面
        return new ModelAndView(getSuccessPage(), model); 
    }
    return new ModelAndView(getViewPage(), model); 
} 

    报错:

    严重: Servlet.service() for servlet [dispatcherServlet] in context with path [/mySurvey] threw exception [successView isn't set] with root cause

javax.servlet.ServletException: successView isn't set
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:416)
at org.springframework.web.servlet.mvc.SimpleFormController.onSubmit(SimpleFormController.java:380)
at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:267)
at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:250)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:819)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:754)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:399)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:364)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:650)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2476)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2465)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:662)

3个回答

LSL1618
LSL1618   2015.05.28 12:19

可能是successView属性没有get和set方法。

LSL1618
LSL1618 回复wtksana: 要学会看异常信息,一般它会告诉你出错原因。
2 年多之前 回复
wtksana
wtksana 方法有的 修改前的代码是能正常运行的 只是在加了参数后不能运行了
2 年多之前 回复
hsj_217
hsj_217   2015.05.28 15:49

恩, 没有getset方法使用也会报错、

wtksana
wtksana 方法有的 修改前的代码是能正常运行的 只是在加了参数后不能运行了
2 年多之前 回复
z345434645
z345434645   2015.05.28 16:33

直接加个HttpSession参数,不用request.getSession

z345434645
z345434645 回复wtksana: onSubmit(Object command,HttpServletRequest request,HttpSession session)
2 年多之前 回复
z345434645
z345434645 回复wtksana: 两个参数,一个request,一个session
2 年多之前 回复
LSL1618
LSL1618 回复wtksana: 意思是把HttpServletRequest request改为HttpSession session。
2 年多之前 回复
wtksana
wtksana 请问该如何修改?
2 年多之前 回复
wtksana
wtksana 请问该如何修改?
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片